From e213353404715d9c601d225d22218587c6b760f7 Mon Sep 17 00:00:00 2001 From: Janez Podhostnik Date: Thu, 11 Jul 2024 18:07:35 +0200 Subject: [PATCH 1/2] Changed flow account key id to uint32 --- access/grpc/convert/convert.go | 8 ++++---- access/http/convert/convert.go | 11 ++++++++--- account.go | 2 +- account_test.go | 2 +- examples/go.mod | 4 ++-- examples/go.sum | 2 ++ test/entities.go | 2 +- transaction.go | 28 ++++++++++++++-------------- transaction_test.go | 24 ++++++++++++------------ 9 files changed, 45 insertions(+), 38 deletions(-) diff --git a/access/grpc/convert/convert.go b/access/grpc/convert/convert.go index 760d81bce..41d65e5f2 100644 --- a/access/grpc/convert/convert.go +++ b/access/grpc/convert/convert.go @@ -102,7 +102,7 @@ func MessageToAccountKey(m *entities.AccountKey) (*flow.AccountKey, error) { } return &flow.AccountKey{ - Index: int(m.GetIndex()), + Index: m.GetIndex(), PublicKey: publicKey, SigAlgo: sigAlgo, HashAlgo: hashAlgo, @@ -478,7 +478,7 @@ func MessageToTransaction(m *entities.Transaction) (flow.Transaction, error) { proposalKey := m.GetProposalKey() if proposalKey != nil { proposalAddress := flow.BytesToAddress(proposalKey.GetAddress()) - t.SetProposalKey(proposalAddress, int(proposalKey.GetKeyId()), proposalKey.GetSequenceNumber()) + t.SetProposalKey(proposalAddress, proposalKey.GetKeyId(), proposalKey.GetSequenceNumber()) } payer := m.GetPayer() @@ -496,12 +496,12 @@ func MessageToTransaction(m *entities.Transaction) (flow.Transaction, error) { for _, sig := range m.GetPayloadSignatures() { addr := flow.BytesToAddress(sig.GetAddress()) - t.AddPayloadSignature(addr, int(sig.GetKeyId()), sig.GetSignature()) + t.AddPayloadSignature(addr, sig.GetKeyId(), sig.GetSignature()) } for _, sig := range m.GetEnvelopeSignatures() { addr := flow.BytesToAddress(sig.GetAddress()) - t.AddEnvelopeSignature(addr, int(sig.GetKeyId()), sig.GetSignature()) + t.AddEnvelopeSignature(addr, sig.GetKeyId(), sig.GetSignature()) } return *t, nil diff --git a/access/http/convert/convert.go b/access/http/convert/convert.go index 53f5709d4..3f029e578 100644 --- a/access/http/convert/convert.go +++ b/access/http/convert/convert.go @@ -46,7 +46,7 @@ func ToKeys(keys []models.AccountPublicKey) []*flow.AccountKey { pkey, _ := crypto.DecodePublicKeyHex(sigAlgo, strings.TrimPrefix(key.PublicKey, "0x")) // validation is done on AN accountKeys[i] = &flow.AccountKey{ - Index: MustToInt(key.Index), + Index: MustToUint32(key.Index), PublicKey: pkey, SigAlgo: sigAlgo, HashAlgo: crypto.StringToHashAlgorithm(string(*key.HashingAlgorithm)), @@ -216,6 +216,11 @@ func MustToUint(value string) uint64 { return parsed } +func MustToUint32(value string) uint32 { + parsed, _ := strconv.ParseUint(value, 10, 32) // we can ignore error since these values are validated before returned + return uint32(parsed) +} + func MustToInt(value string) int { parsed, _ := strconv.Atoi(value) // we can ignore error since these values are validated before returned return parsed @@ -248,7 +253,7 @@ func DecodeCadenceValue(value string, options []cadenceJSON.Option) (cadence.Val func ToProposalKey(key *models.ProposalKey) flow.ProposalKey { return flow.ProposalKey{ Address: flow.HexToAddress(key.Address), - KeyIndex: MustToInt(key.KeyIndex), + KeyIndex: MustToUint32(key.KeyIndex), SequenceNumber: MustToUint(key.SequenceNumber), } } @@ -259,7 +264,7 @@ func ToSignatures(signatures []models.TransactionSignature) []flow.TransactionSi signature, _ := base64.StdEncoding.DecodeString(sig.Signature) // signatures are validated and must be valid sigs[i] = flow.TransactionSignature{ Address: flow.HexToAddress(sig.Address), - KeyIndex: MustToInt(sig.KeyIndex), + KeyIndex: MustToUint32(sig.KeyIndex), Signature: signature, } } diff --git a/account.go b/account.go index 20686c88f..5b4791788 100644 --- a/account.go +++ b/account.go @@ -38,7 +38,7 @@ const AccountKeyWeightThreshold int = 1000 // An AccountKey is a public key associated with an account. type AccountKey struct { - Index int + Index uint32 PublicKey crypto.PublicKey SigAlgo crypto.SignatureAlgorithm HashAlgo crypto.HashAlgorithm diff --git a/account_test.go b/account_test.go index 190077c9a..7494c4af5 100644 --- a/account_test.go +++ b/account_test.go @@ -41,7 +41,7 @@ func TestAccountKey(t *testing.T) { t.Run("Valid", func(t *testing.T) { privateKey := generateKey() weight := 500 - index := 0 + index := uint32(0) seq := uint64(1) key := AccountKey{ diff --git a/examples/go.mod b/examples/go.mod index 25007ddcf..5eacccaac 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -7,7 +7,7 @@ toolchain go1.22.4 replace github.com/onflow/flow-go-sdk => ../ require ( - github.com/onflow/cadence v1.0.0-preview.35 + github.com/onflow/cadence v1.0.0-preview.36 github.com/onflow/flow-cli/flowkit v1.11.0 github.com/onflow/flow-go-sdk v0.41.17 github.com/spf13/afero v1.11.0 @@ -44,7 +44,7 @@ require ( github.com/onflow/atree v0.7.0-rc.2 // indirect github.com/onflow/crypto v0.25.1 // indirect github.com/onflow/flow/protobuf/go/flow v0.4.3 // indirect - github.com/onflow/sdks v0.5.1-0.20230912225508-b35402f12bba // indirect + github.com/onflow/sdks v0.6.0-preview.1 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.4.4 // indirect diff --git a/examples/go.sum b/examples/go.sum index 073b1dbf3..a7036bf6f 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -113,6 +113,7 @@ github.com/onflow/cadence v1.0.0-preview.26/go.mod h1:fGhLBbuEmv5rh48qv0ZS0tUz53 github.com/onflow/cadence v1.0.0-preview.29/go.mod h1:3LM1VgE9HkJ815whY/F0LYWULwJa8p2nJiKyIIxpGAE= github.com/onflow/cadence v1.0.0-preview.31/go.mod h1:3LM1VgE9HkJ815whY/F0LYWULwJa8p2nJiKyIIxpGAE= github.com/onflow/cadence v1.0.0-preview.35/go.mod h1:jOwvPSSLTr9TvaKMs7KKiBYMmpdpNNAFxBsjMlrqVD0= +github.com/onflow/cadence v1.0.0-preview.36/go.mod h1:jOwvPSSLTr9TvaKMs7KKiBYMmpdpNNAFxBsjMlrqVD0= github.com/onflow/crypto v0.25.0 h1:BeWbLsh3ZD13Ej+Uky6kg1PL1ZIVBDVX+2MVBNwqddg= github.com/onflow/crypto v0.25.0/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI= github.com/onflow/crypto v0.25.1/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI= @@ -125,6 +126,7 @@ github.com/onflow/flow/protobuf/go/flow v0.4.3/go.mod h1:NA2pX2nw8zuaxfKphhKsk00 github.com/onflow/sdks v0.5.0 h1:2HCRibwqDaQ1c9oUApnkZtEAhWiNY2GTpRD5+ftdkN8= github.com/onflow/sdks v0.5.0/go.mod h1:F0dj0EyHC55kknLkeD10js4mo14yTdMotnWMslPirrU= github.com/onflow/sdks v0.5.1-0.20230912225508-b35402f12bba/go.mod h1:F0dj0EyHC55kknLkeD10js4mo14yTdMotnWMslPirrU= +github.com/onflow/sdks v0.6.0-preview.1/go.mod h1:F0dj0EyHC55kknLkeD10js4mo14yTdMotnWMslPirrU= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/test/entities.go b/test/entities.go index f396747e8..88ed11094 100644 --- a/test/entities.go +++ b/test/entities.go @@ -58,7 +58,7 @@ func (g *Accounts) New() *flow.Account { } type AccountKeys struct { - count int + count uint32 ids *Identifiers } diff --git a/transaction.go b/transaction.go index 6b0e6b36b..116cceae3 100644 --- a/transaction.go +++ b/transaction.go @@ -94,7 +94,7 @@ type payloadCanonicalForm struct { ReferenceBlockID []byte GasLimit uint64 ProposalKeyAddress []byte - ProposalKeyIndex uint64 + ProposalKeyIndex uint32 ProposalKeySequenceNumber uint64 Payer []byte Authorizers [][]byte @@ -201,7 +201,7 @@ func (t *Transaction) SetComputeLimit(limit uint64) *Transaction { // // The first two arguments specify the account key to be used, and the last argument is the sequence // number being declared. -func (t *Transaction) SetProposalKey(address Address, keyIndex int, sequenceNum uint64) *Transaction { +func (t *Transaction) SetProposalKey(address Address, keyIndex uint32, sequenceNum uint64) *Transaction { proposalKey := ProposalKey{ Address: address, KeyIndex: keyIndex, @@ -299,7 +299,7 @@ func (t *Transaction) refreshSignerIndex() { // being added to the transaction. // // This function returns an error if the signature cannot be generated. -func (t *Transaction) SignPayload(address Address, keyIndex int, signer crypto.Signer) error { +func (t *Transaction) SignPayload(address Address, keyIndex uint32, signer crypto.Signer) error { message := t.PayloadMessage() message = append(TransactionDomainTag[:], message...) sig, err := signer.Sign(message) @@ -319,7 +319,7 @@ func (t *Transaction) SignPayload(address Address, keyIndex int, signer crypto.S // being added to the transaction. // // This function returns an error if the signature cannot be generated. -func (t *Transaction) SignEnvelope(address Address, keyIndex int, signer crypto.Signer) error { +func (t *Transaction) SignEnvelope(address Address, keyIndex uint32, signer crypto.Signer) error { message := t.EnvelopeMessage() message = append(TransactionDomainTag[:], message...) sig, err := signer.Sign(message) @@ -334,7 +334,7 @@ func (t *Transaction) SignEnvelope(address Address, keyIndex int, signer crypto. } // AddPayloadSignature adds a payload signature to the transaction for the given address and key index. -func (t *Transaction) AddPayloadSignature(address Address, keyIndex int, sig []byte) *Transaction { +func (t *Transaction) AddPayloadSignature(address Address, keyIndex uint32, sig []byte) *Transaction { s := t.createSignature(address, keyIndex, sig) t.PayloadSignatures = append(t.PayloadSignatures, s) @@ -344,7 +344,7 @@ func (t *Transaction) AddPayloadSignature(address Address, keyIndex int, sig []b } // AddEnvelopeSignature adds an envelope signature to the transaction for the given address and key index. -func (t *Transaction) AddEnvelopeSignature(address Address, keyIndex int, sig []byte) *Transaction { +func (t *Transaction) AddEnvelopeSignature(address Address, keyIndex uint32, sig []byte) *Transaction { s := t.createSignature(address, keyIndex, sig) t.EnvelopeSignatures = append(t.EnvelopeSignatures, s) @@ -353,7 +353,7 @@ func (t *Transaction) AddEnvelopeSignature(address Address, keyIndex int, sig [] return t } -func (t *Transaction) createSignature(address Address, keyIndex int, sig []byte) TransactionSignature { +func (t *Transaction) createSignature(address Address, keyIndex uint32, sig []byte) TransactionSignature { signerIndex, signerExists := t.signerMap()[address] if !signerExists { signerIndex = -1 @@ -391,7 +391,7 @@ func (t *Transaction) payloadCanonicalForm() payloadCanonicalForm { ReferenceBlockID: t.ReferenceBlockID[:], GasLimit: t.GasLimit, ProposalKeyAddress: t.ProposalKey.Address.Bytes(), - ProposalKeyIndex: uint64(t.ProposalKey.KeyIndex), + ProposalKeyIndex: t.ProposalKey.KeyIndex, ProposalKeySequenceNumber: t.ProposalKey.SequenceNumber, Payer: t.Payer.Bytes(), Authorizers: authorizers, @@ -448,7 +448,7 @@ func DecodeTransaction(transactionMessage []byte) (*Transaction, error) { GasLimit: temp.Payload.GasLimit, ProposalKey: ProposalKey{ Address: BytesToAddress(temp.Payload.ProposalKeyAddress), - KeyIndex: int(temp.Payload.ProposalKeyIndex), + KeyIndex: temp.Payload.ProposalKeyIndex, SequenceNumber: temp.Payload.ProposalKeySequenceNumber, }, Payer: BytesToAddress(temp.Payload.Payer), @@ -558,7 +558,7 @@ func decodeTransaction(transactionMessage []byte) (*transactionCanonicalForm, er // A ProposalKey is the key that specifies the proposal key and sequence number for a transaction. type ProposalKey struct { Address Address - KeyIndex int + KeyIndex uint32 SequenceNumber uint64 } @@ -566,20 +566,20 @@ type ProposalKey struct { type TransactionSignature struct { Address Address SignerIndex int - KeyIndex int + KeyIndex uint32 Signature []byte } type transactionSignatureCanonicalForm struct { SignerIndex uint - KeyIndex uint + KeyIndex uint32 Signature []byte } func (s TransactionSignature) canonicalForm() transactionSignatureCanonicalForm { return transactionSignatureCanonicalForm{ SignerIndex: uint(s.SignerIndex), // int is not RLP-serializable - KeyIndex: uint(s.KeyIndex), // int is not RLP-serializable + KeyIndex: s.KeyIndex, // int is not RLP-serializable Signature: s.Signature, } } @@ -587,7 +587,7 @@ func (s TransactionSignature) canonicalForm() transactionSignatureCanonicalForm func transactionSignatureFromCanonicalForm(v transactionSignatureCanonicalForm) TransactionSignature { return TransactionSignature{ SignerIndex: int(v.SignerIndex), - KeyIndex: int(v.KeyIndex), + KeyIndex: v.KeyIndex, Signature: v.Signature, } } diff --git a/transaction_test.go b/transaction_test.go index 8971302f6..9fff0c1fd 100644 --- a/transaction_test.go +++ b/transaction_test.go @@ -248,7 +248,7 @@ func TestTransaction_SetGasLimit(t *testing.T) { func TestTransaction_SetProposalKey(t *testing.T) { address := flow.ServiceAddress(flow.Mainnet) - keyIndex := 7 + keyIndex := uint32(7) var sequenceNumber uint64 = 42 tx := flow.NewTransaction(). @@ -308,7 +308,7 @@ func TestTransaction_AddPayloadSignature(t *testing.T) { addressA := addresses.New() addressB := addresses.New() - keyIndex := 7 + keyIndex := uint32(7) sig := []byte{42} tx := flow.NewTransaction(). @@ -336,7 +336,7 @@ func TestTransaction_AddPayloadSignature(t *testing.T) { addressA := addresses.New() addressB := addresses.New() - keyIndex := 7 + keyIndex := uint32(7) sig := []byte{42} tx := flow.NewTransaction(). @@ -364,10 +364,10 @@ func TestTransaction_AddPayloadSignature(t *testing.T) { t.Run("Multiple signatures", func(t *testing.T) { address := addresses.New() - keyIndexA := 7 + keyIndexA := uint32(7) sigA := []byte{42} - keyIndexB := 8 + keyIndexB := uint32(8) sigB := []byte{43} tx := flow.NewTransaction(). @@ -411,7 +411,7 @@ func TestTransaction_AddEnvelopeSignature(t *testing.T) { t.Run("Valid signer", func(t *testing.T) { address := addresses.New() - keyIndex := 7 + keyIndex := uint32(7) sig := []byte{42} tx := flow.NewTransaction(). @@ -430,10 +430,10 @@ func TestTransaction_AddEnvelopeSignature(t *testing.T) { t.Run("Multiple signatures", func(t *testing.T) { address := addresses.New() - keyIndexA := 7 + keyIndexA := uint32(7) sigA := []byte{42} - keyIndexB := 8 + keyIndexB := uint32(8) sigB := []byte{43} tx := flow.NewTransaction().AddAuthorizer(address) @@ -462,7 +462,7 @@ func TestTransaction_AbleToReconstructTransaction(t *testing.T) { addressOne := addresses.New() addressTwo := addresses.New() - keyIndex := 7 + keyIndex := uint32(7) sig := []byte{42} tx := flow.NewTransaction(). @@ -516,16 +516,16 @@ func TestTransaction_SignatureOrdering(t *testing.T) { addresses := test.AddressGenerator() proposerAddress := addresses.New() - proposerKeyIndex := 8 + proposerKeyIndex := uint32(8) proposerSequenceNumber := uint64(42) proposerSignature := []byte{1, 2, 3} authorizerAddress := addresses.New() - authorizerKeyIndex := 0 + authorizerKeyIndex := uint32(0) authorizerSignature := []byte{4, 5, 6} payerAddress := addresses.New() - payerKeyIndex := 0 + payerKeyIndex := uint32(0) payerSignature := []byte{7, 8, 9} tx.SetProposalKey(proposerAddress, proposerKeyIndex, proposerSequenceNumber) From 3f4dc11e843aec4eccb65c8649efa5a08f4796b0 Mon Sep 17 00:00:00 2001 From: Janez Podhostnik Date: Mon, 15 Jul 2024 18:02:01 +0200 Subject: [PATCH 2/2] updated cadence version --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index d9f4ce26c..6f57b95eb 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/aws/aws-sdk-go-v2 v1.27.0 github.com/aws/aws-sdk-go-v2/config v1.27.15 github.com/aws/aws-sdk-go-v2/service/kms v1.31.0 - github.com/onflow/cadence v1.0.0-preview.37 + github.com/onflow/cadence v1.0.0-preview.37.0.20240715155222-3860a034ebfa github.com/onflow/crypto v0.25.1 github.com/onflow/flow/protobuf/go/flow v0.4.3 github.com/onflow/go-ethereum v1.13.4 diff --git a/go.sum b/go.sum index a6826deb3..e624a946a 100644 --- a/go.sum +++ b/go.sum @@ -133,8 +133,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/onflow/atree v0.7.0-rc.2 h1:mZmVrl/zPlfI44EjV3FdR2QwIqT8nz1sCONUBFcML/U= github.com/onflow/atree v0.7.0-rc.2/go.mod h1:xvP61FoOs95K7IYdIYRnNcYQGf4nbF/uuJ0tHf4DRuM= -github.com/onflow/cadence v1.0.0-preview.37 h1:CclIG7GXEBJCwk5d5jumJ6cU8xj/Q1A9cfVJKIxvLH8= -github.com/onflow/cadence v1.0.0-preview.37/go.mod h1:jOwvPSSLTr9TvaKMs7KKiBYMmpdpNNAFxBsjMlrqVD0= +github.com/onflow/cadence v1.0.0-preview.37.0.20240715155222-3860a034ebfa h1:JRQJpsUXtgm9OZyFvA8o9PpqccAz/ufw32GGyqLmgLQ= +github.com/onflow/cadence v1.0.0-preview.37.0.20240715155222-3860a034ebfa/go.mod h1:jOwvPSSLTr9TvaKMs7KKiBYMmpdpNNAFxBsjMlrqVD0= github.com/onflow/crypto v0.25.1 h1:0txy2PKPMM873JbpxQNbJmuOJtD56bfs48RQfm0ts5A= github.com/onflow/crypto v0.25.1/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI= github.com/onflow/flow/protobuf/go/flow v0.4.3 h1:gdY7Ftto8dtU+0wI+6ZgW4oE+z0DSDUMIDwVx8mqae8=