Skip to content

Commit

Permalink
Merge pull request #705 from onflow/janez/change-key-id-to-uint32
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Jul 16, 2024
2 parents 13388fc + d2147fc commit 23267c8
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 38 deletions.
8 changes: 4 additions & 4 deletions access/grpc/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions access/http/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
}
}
Expand All @@ -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,
}
}
Expand Down
2 changes: 1 addition & 1 deletion account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion test/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (g *Accounts) New() *flow.Account {
}

type AccountKeys struct {
count int
count uint32
ids *Identifiers
}

Expand Down
28 changes: 14 additions & 14 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type payloadCanonicalForm struct {
ReferenceBlockID []byte
GasLimit uint64
ProposalKeyAddress []byte
ProposalKeyIndex uint64
ProposalKeyIndex uint32
ProposalKeySequenceNumber uint64
Payer []byte
Authorizers [][]byte
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -558,36 +558,36 @@ 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
}

// A TransactionSignature is a signature associated with a specific account key.
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,
}
}

func transactionSignatureFromCanonicalForm(v transactionSignatureCanonicalForm) TransactionSignature {
return TransactionSignature{
SignerIndex: int(v.SignerIndex),
KeyIndex: int(v.KeyIndex),
KeyIndex: v.KeyIndex,
Signature: v.Signature,
}
}
Expand Down
24 changes: 12 additions & 12 deletions transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -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().
Expand Down Expand Up @@ -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().
Expand Down Expand Up @@ -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().
Expand Down Expand Up @@ -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().
Expand All @@ -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)
Expand Down Expand Up @@ -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().
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 23267c8

Please sign in to comment.