Skip to content

Commit

Permalink
improve SignTx function
Browse files Browse the repository at this point in the history
  • Loading branch information
Villaquiranm committed Jan 8, 2025
1 parent e33600f commit 2a67769
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 48 deletions.
13 changes: 7 additions & 6 deletions contribs/gnogenesis/internal/txs/txs_add_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import (
)

const (
DefaultAccount_Name = "test1"
DefaultAccount_Address = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"
DefaultAccount_Seed = "source bonus chronic canvas draft south burst lottery vacant surface solve popular case indicate oppose farm nothing bullet exhibit title speed wink action roast"
defaultAccount_Name = "test1"
defaultAccount_Address = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"
defaultAccount_Seed = "source bonus chronic canvas draft south burst lottery vacant surface solve popular case indicate oppose farm nothing bullet exhibit title speed wink action roast"
defaultAccount_publicKey = "gpub1pgfj7ard9eg82cjtv4u4xetrwqer2dntxyfzxz3pq0skzdkmzu0r9h6gny6eg8c9dc303xrrudee6z4he4y7cs5rnjwmyf40yaj"
)

var errInvalidPackageDir = errors.New("invalid package directory")
Expand Down Expand Up @@ -83,7 +84,7 @@ func execTxsAddPackages(
args []string,
) error {
var (
keyname = DefaultAccount_Name
keyname = defaultAccount_Name
keybase keys.Keybase
pass string
)
Expand All @@ -110,15 +111,15 @@ func execTxsAddPackages(
}
} else {
keybase = keys.NewInMemory()
_, err := keybase.CreateAccount(DefaultAccount_Name, DefaultAccount_Seed, "", "", 0, 0)
_, err := keybase.CreateAccount(defaultAccount_Name, defaultAccount_Seed, "", "", 0, 0)
if err != nil {
return fmt.Errorf("unable to create account: %w", err)
}
}

info, err := keybase.GetByNameOrAddress(keyname)
if err != nil {
return err
return fmt.Errorf("unable to find key in keybase: %w", err)
}

creator := info.GetAddress()
Expand Down
16 changes: 9 additions & 7 deletions contribs/gnogenesis/internal/txs/txs_add_packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

func TestGenesis_Txs_Add_Packages(t *testing.T) {
t.Parallel()
const addPkgExpectedSignature = "cfe5a15d8def04cbdaf9d08e2511db7928152b26419c4577cbfa282c83118852411f3de5d045ce934555572c21bda8042ce5c64b793a01748e49cf2cff7c2983"

t.Run("invalid genesis file", func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -179,7 +180,7 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) {
// Create key
kb, err := keys.NewKeyBaseFromDir(keybaseDir)
require.NoError(t, err)
_, err = kb.CreateAccount(keyname, DefaultAccount_Seed, "", password, 0, 0)
info, err := kb.CreateAccount(keyname, defaultAccount_Seed, "", password, 0, 0)
require.NoError(t, err)

io := commands.NewTestIO()
Expand Down Expand Up @@ -224,11 +225,12 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) {

msgAddPkg, ok := state.Txs[0].Tx.Msgs[0].(vmm.MsgAddPackage)
require.True(t, ok)
require.Equal(t, "gpub1pgfj7ard9eg82cjtv4u4xetrwqer2dntxyfzxz3pq0skzdkmzu0r9h6gny6eg8c9dc303xrrudee6z4he4y7cs5rnjwmyf40yaj", state.Txs[0].Tx.Signatures[0].PubKey.String())
require.Equal(t, "cfe5a15d8def04cbdaf9d08e2511db7928152b26419c4577cbfa282c83118852411f3de5d045ce934555572c21bda8042ce5c64b793a01748e49cf2cff7c2983", hex.EncodeToString(state.Txs[0].Tx.Signatures[0].Signature))
require.Equal(t, info.GetPubKey(), state.Txs[0].Tx.Signatures[0].PubKey)
require.Equal(t, addPkgExpectedSignature, hex.EncodeToString(state.Txs[0].Tx.Signatures[0].Signature))

assert.Equal(t, packagePath, msgAddPkg.Package.Path)
})

t.Run("ok default key", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -274,8 +276,8 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) {

msgAddPkg, ok := state.Txs[0].Tx.Msgs[0].(vmm.MsgAddPackage)
require.True(t, ok)
require.Equal(t, "gpub1pgfj7ard9eg82cjtv4u4xetrwqer2dntxyfzxz3pq0skzdkmzu0r9h6gny6eg8c9dc303xrrudee6z4he4y7cs5rnjwmyf40yaj", state.Txs[0].Tx.Signatures[0].PubKey.String())
require.Equal(t, "cfe5a15d8def04cbdaf9d08e2511db7928152b26419c4577cbfa282c83118852411f3de5d045ce934555572c21bda8042ce5c64b793a01748e49cf2cff7c2983", hex.EncodeToString(state.Txs[0].Tx.Signatures[0].Signature))
require.Equal(t, defaultAccount_publicKey, state.Txs[0].Tx.Signatures[0].PubKey.String())
require.Equal(t, addPkgExpectedSignature, hex.EncodeToString(state.Txs[0].Tx.Signatures[0].Signature))

assert.Equal(t, packagePath, msgAddPkg.Package.Path)
})
Expand Down Expand Up @@ -322,8 +324,8 @@ func TestGenesis_Txs_Add_Packages(t *testing.T) {

msgAddPkg, ok := state.Txs[0].Tx.Msgs[0].(vmm.MsgAddPackage)
require.True(t, ok)
require.Equal(t, "gpub1pgfj7ard9eg82cjtv4u4xetrwqer2dntxyfzxz3pq0skzdkmzu0r9h6gny6eg8c9dc303xrrudee6z4he4y7cs5rnjwmyf40yaj", state.Txs[0].Tx.Signatures[0].PubKey.String())
require.Equal(t, "cfe5a15d8def04cbdaf9d08e2511db7928152b26419c4577cbfa282c83118852411f3de5d045ce934555572c21bda8042ce5c64b793a01748e49cf2cff7c2983", hex.EncodeToString(state.Txs[0].Tx.Signatures[0].Signature))
require.Equal(t, defaultAccount_publicKey, state.Txs[0].Tx.Signatures[0].PubKey.String())
require.Equal(t, addPkgExpectedSignature, hex.EncodeToString(state.Txs[0].Tx.Signatures[0].Signature))

assert.Equal(t, packagePath, msgAddPkg.Package.Path)
})
Expand Down
32 changes: 0 additions & 32 deletions gno.land/pkg/integration/defaultsigner.go

This file was deleted.

5 changes: 3 additions & 2 deletions gno.land/pkg/integration/pkgloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gnolang/gno/gnovm/pkg/gnomod"
"github.com/gnolang/gno/gnovm/pkg/packages"
bft "github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/std"
)

Expand Down Expand Up @@ -39,7 +40,7 @@ func (pl *PkgsLoader) SetPatch(replace, with string) {
pl.patchs[replace] = with
}

func (pl *PkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std.Coins) ([]gnoland.TxWithMetadata, error) {
func (pl *PkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std.Coins, creatorKey crypto.PrivKey) ([]gnoland.TxWithMetadata, error) {
pkgslist, err := pl.List().Sort() // sorts packages by their dependencies.
if err != nil {
return nil, fmt.Errorf("unable to sort packages: %w", err)
Expand Down Expand Up @@ -77,7 +78,7 @@ func (pl *PkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std
}
}

err = SignTxs(txs, "tendermint_test")
err = SignTxs(txs, creatorKey, "tendermint_test")
if err != nil {
return nil, fmt.Errorf("unable to sign txs: %w", err)
}
Expand Down
33 changes: 33 additions & 0 deletions gno.land/pkg/integration/signer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package integration

import (
"fmt"

"github.com/gnolang/gno/gno.land/pkg/gnoland"

"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/std"
)

// SignTxs will sign all txs passed as argument using the private key
// this signature is only valid for genesis transactions as accountNumber and sequence are 0
func SignTxs(txs []gnoland.TxWithMetadata, privKey crypto.PrivKey, chainID string) error {
for index, tx := range txs {
bytes, err := tx.Tx.GetSignBytes(chainID, 0, 0)
if err != nil {
return fmt.Errorf("unable to get sign bytes for transaction, %w", err)
}
signature, err := privKey.Sign(bytes)
if err != nil {
return fmt.Errorf("unable to sign transaction, %w", err)
}

txs[index].Tx.Signatures = []std.Signature{
{
PubKey: privKey.PubKey(),
Signature: signature,
},
}
}
return nil
}
5 changes: 4 additions & 1 deletion gno.land/pkg/integration/testscript_gnoland.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ func SetupGnolandTestscript(t *testing.T, p *testscript.Params) error {
func gnolandCmd(t *testing.T, nodesManager *NodesManager, gnoRootDir string) func(ts *testscript.TestScript, neg bool, args []string) {
t.Helper()

defaultPK, err := generatePrivKeyFromMnemonic(DefaultAccount_Seed, "", 0, 0)
require.NoError(t, err)

return func(ts *testscript.TestScript, neg bool, args []string) {
sid := getNodeSID(ts)

Expand Down Expand Up @@ -267,7 +270,7 @@ func gnolandCmd(t *testing.T, nodesManager *NodesManager, gnoRootDir string) fun
pkgs := ts.Value(envKeyPkgsLoader).(*PkgsLoader)
creator := crypto.MustAddressFromString(DefaultAccount_Address)
defaultFee := std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000)))
pkgsTxs, err := pkgs.LoadPackages(creator, defaultFee, nil)
pkgsTxs, err := pkgs.LoadPackages(creator, defaultFee, nil, defaultPK)
if err != nil {
ts.Fatalf("unable to load packages txs: %s", err)
}
Expand Down

0 comments on commit 2a67769

Please sign in to comment.