Skip to content

Commit

Permalink
commit often ™️
Browse files Browse the repository at this point in the history
  • Loading branch information
QuestofIranon committed Nov 9, 2019
1 parent c68bbf6 commit ef9d184
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 17 deletions.
43 changes: 29 additions & 14 deletions account_create_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
)

type AccountCreateTransaction struct {
builder TransactionBuilder
PublicKey *Ed25519PublicKey
InitialBalance uint64
TransactionBuilder
PublicKey *Ed25519PublicKey
InitialBalance uint64
ProxyAccountId *AccountID
ReceiverSigRequired bool
}

func NewAccountCreateTransaction(client *Client) AccountCreateTransaction {
Expand All @@ -21,16 +23,10 @@ func NewAccountCreateTransaction(client *Client) AccountCreateTransaction {
}

return AccountCreateTransaction{
builder: builder,
TransactionBuilder: builder,
}
}

func (tx AccountCreateTransaction) SetMaxTransactionFee(fee uint64) AccountCreateTransaction {
tx.builder.SetMaxTransactionFee(fee)

return tx
}

func (tx AccountCreateTransaction) SetKey(publicKey Ed25519PublicKey) AccountCreateTransaction {

tx.PublicKey = &publicKey
Expand All @@ -44,6 +40,18 @@ func (tx AccountCreateTransaction) SetInitialBalance(balance uint64) AccountCrea
return tx
}

func (tx AccountCreateTransaction) SetProxyAccountID(accountID AccountID) AccountCreateTransaction {
tx.ProxyAccountId = &accountID

return tx
}

func (tx AccountCreateTransaction) SetReceiverSignatureRequired(required bool) AccountCreateTransaction {
tx.ReceiverSigRequired = required

return tx
}

func (tx AccountCreateTransaction) Validate() error {
if tx.PublicKey == nil {
return fmt.Errorf("AccountCreateTransaction requires Public Key to be set")
Expand All @@ -60,14 +68,21 @@ func (tx AccountCreateTransaction) Build() (*Transaction, error) {

protoKey := tx.PublicKey.toProtoKey()

tx.builder.body.Data = &hedera_proto.TransactionBody_CryptoCreateAccount{
bodyData := hedera_proto.TransactionBody_CryptoCreateAccount{
CryptoCreateAccount: &hedera_proto.CryptoCreateTransactionBody{
Key: &protoKey,
InitialBalance: tx.InitialBalance,
Key: &protoKey,
InitialBalance: tx.InitialBalance,
ReceiverSigRequired: tx.ReceiverSigRequired,
},
}

return tx.builder.build(CryptoCreateAccount)
if tx.ProxyAccountId != nil {
bodyData.CryptoCreateAccount.ProxyAccountID = tx.ProxyAccountId.proto()
}

tx.body.Data = &bodyData

return tx.build()
}

func (tx AccountCreateTransaction) Execute() (*TransactionID, error) {
Expand Down
45 changes: 45 additions & 0 deletions account_create_transaction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package hedera

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestSerializeAccountCreateTransaction(t *testing.T) {
date := time.Unix(1554158542, 0)

key, err := Ed25519PrivateKeyFromString("302e020100300506032b6570042204203b054fade7a2b0869c6bd4a63b7017cbae7855d12acc357bea718e2c3e805962")
assert.NoError(t, err)

tx, err := NewAccountCreateTransaction(nil).
SetKey(key.PublicKey()).
SetInitialBalance(450).
SetProxyAccountID(AccountID{Account: 1020}).
SetReceiverSignatureRequired(true).
SetNodeAccountID(AccountID{Account: 3}).
SetTransactionID(TransactionID{
Account: AccountID{Account: 2},
ValidStartSeconds: uint64(date.Unix()),
ValidStartNanos: uint32(date.UnixNano()),
}).
SetMaxTransactionFee(100_000).
Build()

assert.NoError(t, err)

tx.Sign(key)

txString := `
sigMap {
sigPair {
pubKeyPrefix: \"\\344\\361\\300\\353L}\\315\\303\\347\\353\\021p\\263\\b\\212=\\022\\242\\227\\364\\243\\353\\342\\362\\205\\003\\375g5F\\355\\216\"
ed25519: \"\\357\\204n\\334\\222L\\305\\022\\312\\034\\021\\'\\324n\\201\\347+\\223w\\226\\261\\261I\\307\\fTj\\236\\213\\321?\\230\\2176\\326p\\232\\025\\207\\322\\244?\\230\\265R\\035\\177kp\\211\\342\\034\\316\\215\\260\\335Z\\267\\301\\2718\\362]\\b\"
}
}
bodyBytes: \"\\n\\f\\n\\006\\b\\316\\247\\212\\345\\005\\022\\002\\030\\002\\022\\002\\030\\003\\030\\240\\215\\006\\\"\\002\\bxZM\\n\\\"\\022 \\344\\361\\300\\353L}\\315\\303\\347\\353\\021p\\263\\b\\212=\\022\\242\\227\\364\\243\\353\\342\\362\\205\\003\\375g5F\\355\\216\\020\\302\\003\\032\\003\\030\\374\\a0\\377\\377\\377\\377\\377\\377\\377\\377\\1778\\377\\377\\377\\377\\377\\377\\377\\377\\177@\\001J\\005\\b\\320\\310\\341\\003R\\000Z\\000\"
`

assert.Equal(t, txString, tx.String())
}
8 changes: 8 additions & 0 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,11 @@ func (transaction Transaction) ExecuteForReceipt() (*TransactionReceipt, error)
// todo: return a real receipt
return &TransactionReceipt{}, nil
}

func (transaction Transaction) Proto() hedera_proto.Transaction {
return transaction.inner
}

func (transaction Transaction) String() string {
return transaction.inner.String()
}
11 changes: 8 additions & 3 deletions transaction_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type TransactionBuilderInterface interface {
}

type TransactionBuilder struct {
TransactionBuilderInterface
client *Client
kind TransactionKind
MaxTransactionFee uint64
Expand Down Expand Up @@ -54,7 +53,13 @@ func (tb TransactionBuilder) SetTransactionValidDuration(seconds uint64) Transac
return tb
}

func (tb TransactionBuilder) build(kind TransactionKind) (*Transaction, error) {
func (tb TransactionBuilder) SetNodeAccountID(accountID AccountID) TransactionBuilder {
tb.body.NodeAccountID = accountID.proto()

return tb
}

func (tb TransactionBuilder) build() (*Transaction, error) {
if tb.client != nil {
if tb.body.TransactionFee == 0 {
tb.body.TransactionFee = tb.client.MaxTransactionFee()
Expand Down Expand Up @@ -83,7 +88,7 @@ func (tb TransactionBuilder) build(kind TransactionKind) (*Transaction, error) {
}

tx := Transaction{
Kind: kind,
Kind: tb.kind,
client: tb.client,
inner: hedera_proto.Transaction{
BodyData: &protoBody,
Expand Down

0 comments on commit ef9d184

Please sign in to comment.