Skip to content

Commit

Permalink
Add AccountCreateTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
qtbeee committed Sep 28, 2019
1 parent b74df5e commit 10a8ada
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 8 deletions.
43 changes: 39 additions & 4 deletions Sources/Hedera/Client.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,51 @@
import Sodium

public typealias Node = (accountId: AccountId, address: String)

let defaultMaxTransactionFee = 100_000
let defaultMaxTransactionFee: UInt64 = 100_000_000

public struct Client {
let operatorId: AccountId
let operatorKey: Ed25519PrivateKey
let operatorSigner: (Bytes) -> Bytes

let nodes: [AccountId: Node]
var node: AccountId?

/// The default maximum fee for a transaction.
/// This can be overridden for an individual transaction with `.setTransactionFee()`.
let maxTransactionFee = defaultMaxTransactionFee

// TODO: once queries are implemented
// /// The maximum payment that can be automatically attached to a query.
// /// If this is not set, payments will not be made automatically for queries.
// /// This can be overridden for an individual query with `.setPaymentDefault()`.
// var maxQueryPayment: UInt64?

}

public struct ClientBuilder {
var operatorId: AccountId?
var operatorKey: Ed25519PrivateKey?
var node: Node?
var operatorSigner: ((Bytes) -> Bytes)?
var node: AccountId?

public init() {}

public mutating func setOperator(id: AccountId, secret: Ed25519PrivateKey) -> Self {
operatorId = id
operatorSigner = secret.sign

return self
}

public mutating func setOperator(id: AccountId, signer: @escaping (Bytes) -> Bytes) -> Self {
operatorId = id
operatorSigner = signer

return self
}

public mutating func setNode(_ id: AccountId) -> Self {
node = id
return self
}
}
4 changes: 2 additions & 2 deletions Sources/Hedera/TransactionBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ let maxValidDuration = TimeInterval(2 * 60)
public class TransactionBuilder {
var body = Proto_TransactionBody()

init() {
// TODO: set transactionFee to something? Client.maxTransactionFee?
init(client: Client) {
body.transactionFee = client.maxTransactionFee
body.transactionValidDuration = maxValidDuration.toProto()
}

Expand Down
61 changes: 61 additions & 0 deletions Sources/Hedera/account/AccountCreateTransaction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import SwiftProtobuf
import Foundation

class AccountCreateTransaction: TransactionBuilder {
public override init(client: Client) {
super.init(client: client)

var inner = Proto_CryptoCreateTransactionBody()
// Required fixed autorenew duration (roughly 1/4 year)
inner.autoRenewPeriod = TimeInterval(7_890_000).toProto()
// Default to maximum values for record thresholds. Without this, records
// would be auto-created whenever a send or receive transaction takes place
// for this new account. This should be an explicit ask.
inner.sendRecordThreshold = UInt64.max
inner.receiveRecordThreshold = UInt64.max

body.cryptoCreateAccount = inner
}

public func setKey(_ key: Ed25519PublicKey) -> Self {
body.cryptoCreateAccount.key = key.toProto()

return self
}

public func setInitialBalance(_ balance: UInt64) -> Self {
body.cryptoCreateAccount.initialBalance = balance

return self
}

public func setProxyAccountId(_ id: AccountId) -> Self {
body.cryptoCreateAccount.proxyAccountID = id.toProto()

return self
}

public func setSendRecordThreshold(_ threshold: UInt64) -> Self {
body.cryptoCreateAccount.sendRecordThreshold = threshold

return self
}

public func setReceiveRecordThreshold(_ threshold: UInt64) -> Self {
body.cryptoCreateAccount.receiveRecordThreshold = threshold

return self
}

public func setReceiverSignatureRequired(_ required: Bool) -> Self {
body.cryptoCreateAccount.receiverSigRequired = required

return self
}

public func setAutoRenewPeriod(_ period: TimeInterval) -> Self {
body.cryptoCreateAccount.autoRenewPeriod = period.toProto()

return self
}
}
4 changes: 2 additions & 2 deletions Sources/Hedera/account/CryptoTransferTransaction.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import SwiftProtobuf

public class CryptoTransferTransaction: TransactionBuilder {
public override init() {
super.init()
public override init(client: Client) {
super.init(client: client)

var inner = Proto_CryptoTransferTransactionBody()
inner.transfers = Proto_TransferList()
Expand Down
14 changes: 14 additions & 0 deletions Sources/Hedera/crypto/ed25519/Ed25519PublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,17 @@ extension Ed25519PublicKey: LosslessStringConvertible {
}
}
}

extension Ed25519PublicKey: ProtoConvertible {
typealias Proto = Proto_Key

func toProto() -> Proto_Key {
var proto = Proto()
proto.ed25519 = Data(bytes)
return proto
}

init?(_ proto: Proto) {
self = Ed25519PublicKey(bytes: Bytes(proto.ed25519))!
}
}

0 comments on commit 10a8ada

Please sign in to comment.