Skip to content

Commit

Permalink
Add chain ID (#856)
Browse files Browse the repository at this point in the history
* add chain id in API body

* add chain id in payload signature

* use chain id in sign for direct operation

* add fallback buildnet chain id

* update go.sum

* fix canonization of chain id

* add the chain id in the fallback network infos

* improve error handling

* rename a variable for unit test

* rename defaultChainId

* refactor prepare sign data

* add unit test for PrepareSignData

* format code
  • Loading branch information
Thykof authored Jan 2, 2024
1 parent ec65869 commit 6be1225
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 26 deletions.
17 changes: 17 additions & 0 deletions api/server/models/sign_request.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions api/server/restapi/embedded_spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions api/walletApi-V0.yml
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ definitions:
type: object
required:
- operation
- chainId
properties:
description:
type: string
Expand All @@ -569,6 +570,9 @@ definitions:
type: boolean
correlationId:
$ref: "#/definitions/CorrelationId"
chainId:
type: integer
description: The chain id of the network to which the operation will be sent.

SignMessageRequest:
type: object
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/bluele/gcache v0.0.2
github.com/go-openapi/runtime v0.25.0
github.com/jessevdk/go-flags v1.5.0
github.com/massalabs/station v0.3.9-0.20231102130013-1ab9b613fc1f
github.com/massalabs/station v0.5.1-0.20231218175551-3429dc53fea2
github.com/massalabs/station-massa-hello-world v0.0.10
github.com/pkg/errors v0.9.1
github.com/rs/cors v1.8.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE=
github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0=
github.com/massalabs/station v0.3.9-0.20231102130013-1ab9b613fc1f h1:1A42aImAmu3FGl8MWY9EQ5CeXhViWRtqF8rzxIdhniw=
github.com/massalabs/station v0.3.9-0.20231102130013-1ab9b613fc1f/go.mod h1:6VNh9PRA4uGwF6H+YGNRkbMY2pMAPK+ZFloDWHeRtJ0=
github.com/massalabs/station v0.5.1-0.20231218175551-3429dc53fea2 h1:mdOJT1dRkah27ZzdDVKDb4lu5ZoM8s2GxVoDfeQDwWM=
github.com/massalabs/station v0.5.1-0.20231218175551-3429dc53fea2/go.mod h1:6VNh9PRA4uGwF6H+YGNRkbMY2pMAPK+ZFloDWHeRtJ0=
github.com/massalabs/station-massa-hello-world v0.0.10 h1:gzsPRD8PmFsGmd1UFG/xpOFut0zm4JKEUmjGo/g/fAA=
github.com/massalabs/station-massa-hello-world v0.0.10/go.mod h1:K1mf69YpRPPZziE53KHXE5IKlTMqU3r1wuLL0MWM21k=
github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE=
Expand Down
16 changes: 7 additions & 9 deletions internal/handler/wallet/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (w *walletSign) Handle(params operations.SignParams) middleware.Responder {
}
}

operation, msgToSign, err := prepareOperation(acc, fees, params.Body.Operation.String(), promptRequest.Data.(PromptRequestSignData).OperationType)
operation, msgToSign, err := prepareOperation(acc, fees, params.Body.Operation.String(), promptRequest.Data.(PromptRequestSignData).OperationType, *params.Body.ChainID)
if err != nil {
return newErrorResponse(err.Error(), errorSignDecodeOperation, http.StatusBadRequest)
}
Expand Down Expand Up @@ -194,7 +194,7 @@ func (w *walletSign) Success(acc *account.Account, signature []byte, correlation
// prepareOperation prepares the operation to be signed.
// Returns the modified operation (fees change) and the operation to be signed (with public key).
// Returns an error if the operation cannot be decoded.
func prepareOperation(acc *account.Account, fees uint64, operationB64 string, operationType int) ([]byte, []byte, error) {
func prepareOperation(acc *account.Account, fees uint64, operationB64 string, operationType int, chainID int64) ([]byte, []byte, error) {
decodedMsg, _, expiry, err := sendoperation.DecodeMessage64(operationB64)
if err != nil {
return nil, nil, fmt.Errorf("failed to decode operation for preparing before signing: %w", err)
Expand All @@ -209,15 +209,13 @@ func prepareOperation(acc *account.Account, fees uint64, operationB64 string, op
msgToSign := make([]byte, len(operation))
copy(msgToSign, operation)

if operationType != Message {
publicKeyBytes, err := acc.PublicKey.MarshalBinary()
if err != nil {
return nil, nil, fmt.Errorf("Unable to marshal public key: %w", err)
}

msgToSign = append(publicKeyBytes, msgToSign...)
publicKey, err := acc.PublicKey.MarshalBinary()
if err != nil {
return nil, nil, fmt.Errorf("Unable to marshal public key: %w", err)
}

msgToSign = utils.PrepareSignData(uint64(chainID), append(publicKey, msgToSign...))

return operation, msgToSign, nil
}

Expand Down
13 changes: 7 additions & 6 deletions internal/handler/wallet/sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"testing"

"github.com/massalabs/station-massa-wallet/api/server/models"
Expand Down Expand Up @@ -43,19 +44,19 @@ func TestPrepareOperation(t *testing.T) {
fees := uint64(1000)
operationB64 := "AKT4CASAzuTNAqCNBgEAXBwUw39NBQYix8Ovph0TUiJuDDEnlFYUPgsbeMbrA4cLZm9yd2FyZEJ1cm7FAQDgfY7fLW7qpwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACoAAAAweGZDRERBRTI1MTAwNjIxYTViQzg4MTlkQzlEMzg0MjUzNEQ3QmY0NzYAAAAANQAAAEFTMTJUUm9TY01kd0xLOFlwdDZOQkFwcHl6Q0Z3N1FlRzVlM3hGdnhwQ0FuQW5ZTGZ1TVVUKgAAADB4NTM4NDRGOTU3N0MyMzM0ZTU0MUFlYzdEZjcxNzRFQ2U1ZEYxZkNmMKc2qgAAAAAA"

operation, msgToSign, err := prepareOperation(acc, fees, operationB64, 4)
operation, msgToSign, err := prepareOperation(acc, fees, operationB64, 4, ChainIDUnitTests)
assert.NoError(t, err)
expected := []byte{0xe8, 0x7, 0xa4, 0xf8, 0x8, 0x4, 0x80, 0xce, 0xe4, 0xcd, 0x2, 0xa0, 0x8d, 0x6, 0x1, 0x0, 0x5c, 0x1c, 0x14, 0xc3, 0x7f, 0x4d, 0x5, 0x6, 0x22, 0xc7, 0xc3, 0xaf, 0xa6, 0x1d, 0x13, 0x52, 0x22, 0x6e, 0xc, 0x31, 0x27, 0x94, 0x56, 0x14, 0x3e, 0xb, 0x1b, 0x78, 0xc6, 0xeb, 0x3, 0x87, 0xb, 0x66, 0x6f, 0x72, 0x77, 0x61, 0x72, 0x64, 0x42, 0x75, 0x72, 0x6e, 0xc5, 0x1, 0x0, 0xe0, 0x7d, 0x8e, 0xdf, 0x2d, 0x6e, 0xea, 0xa7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2a, 0x0, 0x0, 0x0, 0x30, 0x78, 0x66, 0x43, 0x44, 0x44, 0x41, 0x45, 0x32, 0x35, 0x31, 0x30, 0x30, 0x36, 0x32, 0x31, 0x61, 0x35, 0x62, 0x43, 0x38, 0x38, 0x31, 0x39, 0x64, 0x43, 0x39, 0x44, 0x33, 0x38, 0x34, 0x32, 0x35, 0x33, 0x34, 0x44, 0x37, 0x42, 0x66, 0x34, 0x37, 0x36, 0x0, 0x0, 0x0, 0x0, 0x35, 0x0, 0x0, 0x0, 0x41, 0x53, 0x31, 0x32, 0x54, 0x52, 0x6f, 0x53, 0x63, 0x4d, 0x64, 0x77, 0x4c, 0x4b, 0x38, 0x59, 0x70, 0x74, 0x36, 0x4e, 0x42, 0x41, 0x70, 0x70, 0x79, 0x7a, 0x43, 0x46, 0x77, 0x37, 0x51, 0x65, 0x47, 0x35, 0x65, 0x33, 0x78, 0x46, 0x76, 0x78, 0x70, 0x43, 0x41, 0x6e, 0x41, 0x6e, 0x59, 0x4c, 0x66, 0x75, 0x4d, 0x55, 0x54, 0x2a, 0x0, 0x0, 0x0, 0x30, 0x78, 0x35, 0x33, 0x38, 0x34, 0x34, 0x46, 0x39, 0x35, 0x37, 0x37, 0x43, 0x32, 0x33, 0x33, 0x34, 0x65, 0x35, 0x34, 0x31, 0x41, 0x65, 0x63, 0x37, 0x44, 0x66, 0x37, 0x31, 0x37, 0x34, 0x45, 0x43, 0x65, 0x35, 0x64, 0x46, 0x31, 0x66, 0x43, 0x66, 0x30, 0xa7, 0x36, 0xaa, 0x0, 0x0, 0x0, 0x0, 0x0}
assert.Equal(t, expected, operation)
expectedMsg := []byte{0x0, 0x2d, 0x96, 0xbc, 0xda, 0xcb, 0xbe, 0x41, 0x38, 0x2c, 0xa2, 0x3e, 0x52, 0xe3, 0xd2, 0x19, 0x6c, 0xba, 0x65, 0xe7, 0xa1, 0xac, 0xd2, 0x9, 0xdf, 0xc9, 0x5c, 0x6b, 0x32, 0xb6, 0xa1, 0x8a, 0x93, 0xe8, 0x7, 0xa4, 0xf8, 0x8, 0x4, 0x80, 0xce, 0xe4, 0xcd, 0x2, 0xa0, 0x8d, 0x6, 0x1, 0x0, 0x5c, 0x1c, 0x14, 0xc3, 0x7f, 0x4d, 0x5, 0x6, 0x22, 0xc7, 0xc3, 0xaf, 0xa6, 0x1d, 0x13, 0x52, 0x22, 0x6e, 0xc, 0x31, 0x27, 0x94, 0x56, 0x14, 0x3e, 0xb, 0x1b, 0x78, 0xc6, 0xeb, 0x3, 0x87, 0xb, 0x66, 0x6f, 0x72, 0x77, 0x61, 0x72, 0x64, 0x42, 0x75, 0x72, 0x6e, 0xc5, 0x1, 0x0, 0xe0, 0x7d, 0x8e, 0xdf, 0x2d, 0x6e, 0xea, 0xa7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2a, 0x0, 0x0, 0x0, 0x30, 0x78, 0x66, 0x43, 0x44, 0x44, 0x41, 0x45, 0x32, 0x35, 0x31, 0x30, 0x30, 0x36, 0x32, 0x31, 0x61, 0x35, 0x62, 0x43, 0x38, 0x38, 0x31, 0x39, 0x64, 0x43, 0x39, 0x44, 0x33, 0x38, 0x34, 0x32, 0x35, 0x33, 0x34, 0x44, 0x37, 0x42, 0x66, 0x34, 0x37, 0x36, 0x0, 0x0, 0x0, 0x0, 0x35, 0x0, 0x0, 0x0, 0x41, 0x53, 0x31, 0x32, 0x54, 0x52, 0x6f, 0x53, 0x63, 0x4d, 0x64, 0x77, 0x4c, 0x4b, 0x38, 0x59, 0x70, 0x74, 0x36, 0x4e, 0x42, 0x41, 0x70, 0x70, 0x79, 0x7a, 0x43, 0x46, 0x77, 0x37, 0x51, 0x65, 0x47, 0x35, 0x65, 0x33, 0x78, 0x46, 0x76, 0x78, 0x70, 0x43, 0x41, 0x6e, 0x41, 0x6e, 0x59, 0x4c, 0x66, 0x75, 0x4d, 0x55, 0x54, 0x2a, 0x0, 0x0, 0x0, 0x30, 0x78, 0x35, 0x33, 0x38, 0x34, 0x34, 0x46, 0x39, 0x35, 0x37, 0x37, 0x43, 0x32, 0x33, 0x33, 0x34, 0x65, 0x35, 0x34, 0x31, 0x41, 0x65, 0x63, 0x37, 0x44, 0x66, 0x37, 0x31, 0x37, 0x34, 0x45, 0x43, 0x65, 0x35, 0x64, 0x46, 0x31, 0x66, 0x43, 0x66, 0x30, 0xa7, 0x36, 0xaa, 0x0, 0x0, 0x0, 0x0, 0x0}
expectedMsg := []byte{0x0, 0x0, 0x0, 0x0, 0x4, 0xa0, 0xf8, 0xfe, 0x0, 0x2d, 0x96, 0xbc, 0xda, 0xcb, 0xbe, 0x41, 0x38, 0x2c, 0xa2, 0x3e, 0x52, 0xe3, 0xd2, 0x19, 0x6c, 0xba, 0x65, 0xe7, 0xa1, 0xac, 0xd2, 0x9, 0xdf, 0xc9, 0x5c, 0x6b, 0x32, 0xb6, 0xa1, 0x8a, 0x93, 0xe8, 0x7, 0xa4, 0xf8, 0x8, 0x4, 0x80, 0xce, 0xe4, 0xcd, 0x2, 0xa0, 0x8d, 0x6, 0x1, 0x0, 0x5c, 0x1c, 0x14, 0xc3, 0x7f, 0x4d, 0x5, 0x6, 0x22, 0xc7, 0xc3, 0xaf, 0xa6, 0x1d, 0x13, 0x52, 0x22, 0x6e, 0xc, 0x31, 0x27, 0x94, 0x56, 0x14, 0x3e, 0xb, 0x1b, 0x78, 0xc6, 0xeb, 0x3, 0x87, 0xb, 0x66, 0x6f, 0x72, 0x77, 0x61, 0x72, 0x64, 0x42, 0x75, 0x72, 0x6e, 0xc5, 0x1, 0x0, 0xe0, 0x7d, 0x8e, 0xdf, 0x2d, 0x6e, 0xea, 0xa7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2a, 0x0, 0x0, 0x0, 0x30, 0x78, 0x66, 0x43, 0x44, 0x44, 0x41, 0x45, 0x32, 0x35, 0x31, 0x30, 0x30, 0x36, 0x32, 0x31, 0x61, 0x35, 0x62, 0x43, 0x38, 0x38, 0x31, 0x39, 0x64, 0x43, 0x39, 0x44, 0x33, 0x38, 0x34, 0x32, 0x35, 0x33, 0x34, 0x44, 0x37, 0x42, 0x66, 0x34, 0x37, 0x36, 0x0, 0x0, 0x0, 0x0, 0x35, 0x0, 0x0, 0x0, 0x41, 0x53, 0x31, 0x32, 0x54, 0x52, 0x6f, 0x53, 0x63, 0x4d, 0x64, 0x77, 0x4c, 0x4b, 0x38, 0x59, 0x70, 0x74, 0x36, 0x4e, 0x42, 0x41, 0x70, 0x70, 0x79, 0x7a, 0x43, 0x46, 0x77, 0x37, 0x51, 0x65, 0x47, 0x35, 0x65, 0x33, 0x78, 0x46, 0x76, 0x78, 0x70, 0x43, 0x41, 0x6e, 0x41, 0x6e, 0x59, 0x4c, 0x66, 0x75, 0x4d, 0x55, 0x54, 0x2a, 0x0, 0x0, 0x0, 0x30, 0x78, 0x35, 0x33, 0x38, 0x34, 0x34, 0x46, 0x39, 0x35, 0x37, 0x37, 0x43, 0x32, 0x33, 0x33, 0x34, 0x65, 0x35, 0x34, 0x31, 0x41, 0x65, 0x63, 0x37, 0x44, 0x66, 0x37, 0x31, 0x37, 0x34, 0x45, 0x43, 0x65, 0x35, 0x64, 0x46, 0x31, 0x66, 0x43, 0x66, 0x30, 0xa7, 0x36, 0xaa, 0x0, 0x0, 0x0, 0x0, 0x0}
assert.Equal(t, expectedMsg, msgToSign)
}

func Test_walletSign_Handle(t *testing.T) {
api, prompterApp, _, resChan, err := MockAPI()
assert.NoError(t, err)

transactionData := fmt.Sprintf(`{"operation":"%s"}`, callSCString)
transactionData := fmt.Sprintf(`{"chainId": `+strconv.FormatUint(ChainIDUnitTests, 10)+`, "operation":"%s"}`, callSCString)
nickname := "walletToDelete"
password := "zePassword"
createAccount(password, nickname, t, prompterApp)
Expand Down Expand Up @@ -151,7 +152,7 @@ func Test_walletSign_Handle(t *testing.T) {
})

t.Run("sign transaction batch", func(t *testing.T) {
transactionDataBatch := fmt.Sprintf(`{"operation":"%s","batch":true}`, callSCString)
transactionDataBatch := fmt.Sprintf(`{"chainId": `+strconv.FormatUint(ChainIDUnitTests, 10)+`, "operation":"%s","batch":true}`, callSCString)
testResult := make(chan walletapp.EventData)

// Send password to prompter app and wait for result
Expand All @@ -174,14 +175,14 @@ func Test_walletSign_Handle(t *testing.T) {

correlationId := base64.StdEncoding.EncodeToString(body.CorrelationID)

transactionDataBatch = fmt.Sprintf(`{"operation":"%s","correlationId":"%s"}`, callSCString, correlationId)
transactionDataBatch = fmt.Sprintf(`{"chainId": `+strconv.FormatUint(ChainIDUnitTests, 10)+`, "operation":"%s","correlationId":"%s"}`, callSCString, correlationId)
// Send new transaction without password prompt
resp = signTransaction(t, api, nickname, transactionDataBatch)
verifyStatusCode(t, resp, http.StatusOK)

// Send new transaction with incorrect correlation id
correlationId = base64.StdEncoding.EncodeToString([]byte("wrong correlation id"))
transactionDataBatch = fmt.Sprintf(`{"operation":"%s","correlationId":"%s"}`, callSCString, correlationId)
transactionDataBatch = fmt.Sprintf(`{"chainId": `+strconv.FormatUint(ChainIDUnitTests, 10)+`, "operation":"%s","correlationId":"%s"}`, callSCString, correlationId)
resp = signTransaction(t, api, nickname, transactionDataBatch)
var bodyError operations.SignInternalServerError
err = json.Unmarshal(resp.Body.Bytes(), &bodyError)
Expand Down
2 changes: 2 additions & 0 deletions internal/handler/wallet/testsUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/stretchr/testify/assert"
)

const ChainIDUnitTests = 77658366 // Buildnet

func createAccount(password, nickname string, t *testing.T, prompterApp prompt.WalletPrompterInterface) *account.Account {
acc, err := account.Generate(memguard.NewBufferFromBytes([]byte(password)), nickname)
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/network/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func NewMassaClient() (*node.Client, error) {
return nil, err
}

logger.Debugf("Connected to node URL: %s", networkInfo.URL)
logger.Debugf("Connected to node URL: %s, chain id: %d", networkInfo.URL, networkInfo.ChainID)

return node.NewClient(networkInfo.URL), nil
}
Expand Down
29 changes: 29 additions & 0 deletions pkg/network/chainid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package network

import (
"errors"

"github.com/massalabs/station/pkg/node"
)

var ErrChainIDNotInStatus = errors.New("chain id not in status")

func getChainID() (uint64, error) {
client, err := NewMassaClient()
if err != nil {
return 0, err
}

status, err := node.Status(client)
if err != nil {
return 0, err
}

chainID := status.ChainID

if chainID == nil {
return 0, ErrChainIDNotInStatus
}

return uint64(*chainID), nil
}
18 changes: 14 additions & 4 deletions pkg/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ const (
massaStationNodeEndpoint = plugin.MassaStationBaseURL + "/massa/node"
defaultNetwork = "buildnet"
defaultNodeUrl = "https://buildnet.massa.net/api/v2"
defaultChainId = 77658366
)

type NetworkInfo struct {
Network string `json:"network"`
URL string `json:"url"`
DNS string `json:"dns"`
ChainID int `json:"chainId"`
}

func logFallback(action string, err error) {
Expand All @@ -30,24 +31,33 @@ func GetNetworkInfo() (*NetworkInfo, error) {
resp, err := http.Get(massaStationNodeEndpoint)
if err != nil {
logFallback("GET massa station node endpoint", err)
return &NetworkInfo{Network: defaultNetwork, URL: defaultNodeUrl}, nil

return fallbackNetworkInfo(), nil
}

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
logFallback("read response body", err)
return &NetworkInfo{Network: defaultNetwork, URL: defaultNodeUrl}, nil
return fallbackNetworkInfo(), nil
}

var data NetworkInfo

err = json.Unmarshal(body, &data)
if err != nil {
logFallback("parse JSON", err)
return &NetworkInfo{Network: defaultNetwork, URL: defaultNodeUrl}, nil
return fallbackNetworkInfo(), nil
}

return &data, nil
}

func fallbackNetworkInfo() *NetworkInfo {
return &NetworkInfo{
Network: defaultNetwork,
URL: defaultNodeUrl,
ChainID: defaultChainId,
}
}
8 changes: 7 additions & 1 deletion pkg/network/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/awnumar/memguard"
"github.com/massalabs/station-massa-wallet/pkg/utils"
"github.com/massalabs/station-massa-wallet/pkg/wallet/account"
sendOperation "github.com/massalabs/station/pkg/node/sendoperation"
)
Expand Down Expand Up @@ -40,7 +41,12 @@ func SendOperation(
return nil, fmt.Errorf("unable to marshal public key: %w", err)
}

operationDataToSign := append(publicKey, operationData...)
chainID, err := getChainID()
if err != nil {
return nil, fmt.Errorf("unable to get chain id: %w", err)
}

operationDataToSign := utils.PrepareSignData(uint64(chainID), append(publicKey, operationData...))

// TODO: we do not implement the handling of the correlation id for now
signature, err := acc.Sign(password, operationDataToSign)
Expand Down
10 changes: 10 additions & 0 deletions pkg/utils/sign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package utils

import "encoding/binary"

func PrepareSignData(chainID uint64, data []byte) []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, uint64(chainID))

return append(buf, data...)
}
18 changes: 18 additions & 0 deletions pkg/utils/sign_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package utils

import (
"testing"

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

func TestSign(t *testing.T) {
chainID := uint64(1)
data := []byte("test")

expected := []byte{0, 0, 0, 0, 0, 0, 0, 1, 116, 101, 115, 116}
actual := PrepareSignData(chainID, data)

assert.Equal(t, len(expected), len(actual))
assert.Equal(t, expected, actual)
}

0 comments on commit 6be1225

Please sign in to comment.