From bf88ae86b54e15d03a5624017151aaf9c372cd96 Mon Sep 17 00:00:00 2001 From: Alexander Sporn Date: Mon, 4 Dec 2023 08:51:23 +0100 Subject: [PATCH 1/2] Remove mock.Account and mock.KeyManager since those were moved to iota.go wallet package --- pkg/testsuite/mock/account.go | 70 ----------------- pkg/testsuite/mock/block_params.go | 5 +- pkg/testsuite/mock/blockissuer.go | 13 ++-- pkg/testsuite/mock/keymanager.go | 75 ------------------- pkg/testsuite/mock/node.go | 5 +- pkg/testsuite/mock/wallet.go | 9 ++- pkg/testsuite/snapshotcreator/options.go | 6 +- .../snapshotcreator/snapshotcreator.go | 4 +- pkg/testsuite/testsuite.go | 7 +- 9 files changed, 27 insertions(+), 167 deletions(-) delete mode 100644 pkg/testsuite/mock/account.go delete mode 100644 pkg/testsuite/mock/keymanager.go diff --git a/pkg/testsuite/mock/account.go b/pkg/testsuite/mock/account.go deleted file mode 100644 index 693be09de..000000000 --- a/pkg/testsuite/mock/account.go +++ /dev/null @@ -1,70 +0,0 @@ -package mock - -import ( - "crypto/ed25519" - "fmt" - - "github.com/iotaledger/hive.go/crypto" - iotago "github.com/iotaledger/iota.go/v4" -) - -// Account represents an account. -type Account interface { - // ID returns the accountID. - ID() iotago.AccountID - - // Address returns the account address. - Address() iotago.Address - - // PrivateKey returns the account private key for signing. - PrivateKey() ed25519.PrivateKey -} - -var _ Account = &Ed25519Account{} - -// Ed25519Account is an account that uses an Ed25519 key pair. -type Ed25519Account struct { - accountID iotago.AccountID - privateKey ed25519.PrivateKey -} - -// NewEd25519Account creates a new Ed25519Account. -func NewEd25519Account(accountID iotago.AccountID, privateKey ed25519.PrivateKey) *Ed25519Account { - return &Ed25519Account{ - accountID: accountID, - privateKey: privateKey, - } -} - -// ID returns the accountID. -func (e *Ed25519Account) ID() iotago.AccountID { - return e.accountID -} - -// Address returns the account address. -func (e *Ed25519Account) Address() iotago.Address { - ed25519PubKey, ok := e.privateKey.Public().(ed25519.PublicKey) - if !ok { - panic("invalid public key type") - } - - return iotago.Ed25519AddressFromPubKey(ed25519PubKey) -} - -// PrivateKey returns the account private key for signing. -func (e *Ed25519Account) PrivateKey() ed25519.PrivateKey { - return e.privateKey -} - -func AccountFromParams(accountHex string, privateKey string) Account { - accountID, err := iotago.AccountIDFromHexString(accountHex) - if err != nil { - panic(fmt.Sprintln("invalid accountID hex string", err)) - } - privKey, err := crypto.ParseEd25519PrivateKeyFromString(privateKey) - if err != nil { - panic(fmt.Sprintln("invalid ed25519 private key string", err)) - } - - return NewEd25519Account(accountID, privKey) -} diff --git a/pkg/testsuite/mock/block_params.go b/pkg/testsuite/mock/block_params.go index bedf6d767..473032d6d 100644 --- a/pkg/testsuite/mock/block_params.go +++ b/pkg/testsuite/mock/block_params.go @@ -6,6 +6,7 @@ import ( "github.com/iotaledger/hive.go/runtime/options" "github.com/iotaledger/iota-core/pkg/model" iotago "github.com/iotaledger/iota.go/v4" + "github.com/iotaledger/iota.go/v4/wallet" ) type BlockHeaderParams struct { @@ -15,7 +16,7 @@ type BlockHeaderParams struct { LatestFinalizedSlot *iotago.SlotIndex IssuingTime *time.Time ProtocolVersion *iotago.Version - Issuer Account + Issuer wallet.Account SkipReferenceValidation bool } type BasicBlockParams struct { @@ -86,7 +87,7 @@ func WithProtocolVersion(version iotago.Version) func(builder *BlockHeaderParams builder.ProtocolVersion = &version } } -func WithIssuer(issuer Account) func(builder *BlockHeaderParams) { +func WithIssuer(issuer wallet.Account) func(builder *BlockHeaderParams) { return func(builder *BlockHeaderParams) { builder.Issuer = issuer } diff --git a/pkg/testsuite/mock/blockissuer.go b/pkg/testsuite/mock/blockissuer.go index ae264c008..0f87ecf00 100644 --- a/pkg/testsuite/mock/blockissuer.go +++ b/pkg/testsuite/mock/blockissuer.go @@ -25,6 +25,7 @@ import ( "github.com/iotaledger/iota-core/pkg/protocol/engine/filter/presolidfilter" iotago "github.com/iotaledger/iota.go/v4" "github.com/iotaledger/iota.go/v4/builder" + "github.com/iotaledger/iota.go/v4/wallet" ) var ( @@ -62,7 +63,7 @@ type BlockIssuer struct { optsRateSetterEnabled bool } -func NewBlockIssuer(t *testing.T, name string, keyManager *KeyManager, accountID iotago.AccountID, validator bool, opts ...options.Option[BlockIssuer]) *BlockIssuer { +func NewBlockIssuer(t *testing.T, name string, keyManager *wallet.KeyManager, accountID iotago.AccountID, validator bool, opts ...options.Option[BlockIssuer]) *BlockIssuer { priv, pub := keyManager.KeyPair() if accountID == iotago.EmptyAccountID { @@ -100,7 +101,7 @@ func (i *BlockIssuer) Shutdown() { i.workerPool.ShutdownComplete.Wait() } -func (i *BlockIssuer) CreateValidationBlock(ctx context.Context, alias string, issuerAccount Account, node *Node, opts ...options.Option[ValidatorBlockParams]) (*blocks.Block, error) { +func (i *BlockIssuer) CreateValidationBlock(ctx context.Context, alias string, issuerAccount wallet.Account, node *Node, opts ...options.Option[ValidatorBlockParams]) (*blocks.Block, error) { blockParams := options.Apply(&ValidatorBlockParams{}, opts) if blockParams.BlockHeader.IssuingTime == nil { @@ -189,7 +190,7 @@ func (i *BlockIssuer) CreateValidationBlock(ctx context.Context, alias string, i } func (i *BlockIssuer) IssueValidationBlock(ctx context.Context, alias string, node *Node, opts ...options.Option[ValidatorBlockParams]) *blocks.Block { - block, err := i.CreateValidationBlock(ctx, alias, NewEd25519Account(i.AccountID, i.privateKey), node, opts...) + block, err := i.CreateValidationBlock(ctx, alias, wallet.NewEd25519Account(i.AccountID, i.privateKey), node, opts...) require.NoError(i.Testing, err) require.NoError(i.Testing, i.IssueBlock(block.ModelBlock(), node)) @@ -369,7 +370,7 @@ func (i *BlockIssuer) IssueBlockAndAwaitEvent(ctx context.Context, block *model. } } -func (i *BlockIssuer) AttachBlock(ctx context.Context, iotaBlock *iotago.Block, node *Node, optIssuerAccount ...Account) (iotago.BlockID, error) { +func (i *BlockIssuer) AttachBlock(ctx context.Context, iotaBlock *iotago.Block, node *Node, optIssuerAccount ...wallet.Account) (iotago.BlockID, error) { // if anything changes, need to make a new signature var resign bool @@ -462,7 +463,7 @@ func (i *BlockIssuer) AttachBlock(ctx context.Context, iotaBlock *iotago.Block, issuerAccount := optIssuerAccount[0] iotaBlock.Header.IssuerID = issuerAccount.ID() - signature, signatureErr := iotaBlock.Sign(iotago.NewAddressKeysForEd25519Address(issuerAccount.Address().(*iotago.Ed25519Address), issuerAccount.PrivateKey())) + signature, signatureErr := iotaBlock.Sign(iotago.NewAddressKeysForEd25519Address(issuerAccount.OwnerAddress().(*iotago.Ed25519Address), issuerAccount.PrivateKey())) if signatureErr != nil { return iotago.EmptyBlockID, ierrors.Wrapf(ErrBlockAttacherInvalidBlock, "%w", signatureErr) } @@ -515,7 +516,7 @@ func (i *BlockIssuer) setDefaultBlockParams(blockParams *BlockHeaderParams, node } if blockParams.Issuer == nil { - blockParams.Issuer = NewEd25519Account(i.AccountID, i.privateKey) + blockParams.Issuer = wallet.NewEd25519Account(i.AccountID, i.privateKey) } else if blockParams.Issuer.ID() != i.AccountID { return ierrors.Errorf("provided issuer account %s, but issuer provided in the block params is different %s", i.AccountID, blockParams.Issuer.ID()) } diff --git a/pkg/testsuite/mock/keymanager.go b/pkg/testsuite/mock/keymanager.go deleted file mode 100644 index 236a50551..000000000 --- a/pkg/testsuite/mock/keymanager.go +++ /dev/null @@ -1,75 +0,0 @@ -package mock - -import ( - "crypto/ed25519" - "fmt" - - "github.com/wollac/iota-crypto-demo/pkg/bip32path" - "github.com/wollac/iota-crypto-demo/pkg/slip10" - "github.com/wollac/iota-crypto-demo/pkg/slip10/eddsa" - - "github.com/iotaledger/hive.go/ierrors" - iotago "github.com/iotaledger/iota.go/v4" -) - -const ( - pathString = "44'/4218'/0'/%d'" -) - -// KeyManager is a hierarchical deterministic key manager. -type KeyManager struct { - seed []byte - index uint64 -} - -func NewKeyManager(seed []byte, index uint64) *KeyManager { - return &KeyManager{ - seed: seed, - index: index, - } -} - -// KeyPair calculates an ed25519 key pair by using slip10. -func (k *KeyManager) KeyPair() (ed25519.PrivateKey, ed25519.PublicKey) { - path, err := bip32path.ParsePath(fmt.Sprintf(pathString, k.index)) - if err != nil { - panic(err) - } - - curve := eddsa.Ed25519() - key, err := slip10.DeriveKeyFromPath(k.seed, curve, path) - if err != nil { - panic(err) - } - - pubKey, privKey := key.Key.(eddsa.Seed).Ed25519Key() - - return ed25519.PrivateKey(privKey), ed25519.PublicKey(pubKey) -} - -// AddressSigner returns an address signer. -func (k *KeyManager) AddressSigner() iotago.AddressSigner { - privKey, pubKey := k.KeyPair() - - // add both address types for simplicity in tests - ed25519Address := iotago.Ed25519AddressFromPubKey(pubKey) - ed25519AddressKey := iotago.NewAddressKeysForEd25519Address(ed25519Address, privKey) - implicitAccountCreationAddress := iotago.ImplicitAccountCreationAddressFromPubKey(pubKey) - implicitAccountCreationAddressKey := iotago.NewAddressKeysForImplicitAccountCreationAddress(implicitAccountCreationAddress, privKey) - - return iotago.NewInMemoryAddressSigner(ed25519AddressKey, implicitAccountCreationAddressKey) -} - -// Address calculates an address of the specified type. -func (k *KeyManager) Address(addressType iotago.AddressType) iotago.DirectUnlockableAddress { - _, pubKey := k.KeyPair() - - switch addressType { - case iotago.AddressEd25519: - return iotago.Ed25519AddressFromPubKey(pubKey) - case iotago.AddressImplicitAccountCreation: - return iotago.ImplicitAccountCreationAddressFromPubKey(pubKey) - default: - panic(ierrors.Wrapf(iotago.ErrUnknownAddrType, "type %d", addressType)) - } -} diff --git a/pkg/testsuite/mock/node.go b/pkg/testsuite/mock/node.go index 966e0ed72..b5b74d95b 100644 --- a/pkg/testsuite/mock/node.go +++ b/pkg/testsuite/mock/node.go @@ -29,6 +29,7 @@ import ( iotago "github.com/iotaledger/iota.go/v4" "github.com/iotaledger/iota.go/v4/merklehasher" "github.com/iotaledger/iota.go/v4/tpkg" + "github.com/iotaledger/iota.go/v4/wallet" ) // idAliases contains a list of aliases registered for a set of IDs. @@ -55,7 +56,7 @@ type Node struct { Name string Validator *BlockIssuer - KeyManager *KeyManager + KeyManager *wallet.KeyManager ctx context.Context ctxCancel context.CancelFunc @@ -83,7 +84,7 @@ type Node struct { func NewNode(t *testing.T, net *Network, partition string, name string, validator bool) *Node { seed := tpkg.RandEd25519Seed() - keyManager := NewKeyManager(seed[:], 0) + keyManager := lo.PanicOnErr(wallet.NewKeyManager(seed[:], 0)) priv, pub := keyManager.KeyPair() accountID := iotago.AccountID(blake2b.Sum256(pub)) diff --git a/pkg/testsuite/mock/wallet.go b/pkg/testsuite/mock/wallet.go index c7c1ec176..2770d8380 100644 --- a/pkg/testsuite/mock/wallet.go +++ b/pkg/testsuite/mock/wallet.go @@ -10,6 +10,7 @@ import ( "github.com/iotaledger/iota-core/pkg/protocol/engine/utxoledger" iotago "github.com/iotaledger/iota.go/v4" "github.com/iotaledger/iota.go/v4/tpkg" + "github.com/iotaledger/iota.go/v4/wallet" ) // Wallet is an object representing a wallet (similar to a FireFly wallet) capable of the following: @@ -24,7 +25,7 @@ type Wallet struct { Node *Node - keyManager *KeyManager + keyManager *wallet.KeyManager BlockIssuer *BlockIssuer @@ -33,11 +34,11 @@ type Wallet struct { currentSlot iotago.SlotIndex } -func NewWallet(t *testing.T, name string, node *Node, keyManager ...*KeyManager) *Wallet { - var km *KeyManager +func NewWallet(t *testing.T, name string, node *Node, keyManager ...*wallet.KeyManager) *Wallet { + var km *wallet.KeyManager if len(keyManager) == 0 { randomSeed := tpkg.RandEd25519Seed() - km = NewKeyManager(randomSeed[:], 0) + km = lo.PanicOnErr(wallet.NewKeyManager(randomSeed[:], 0)) } else { km = keyManager[0] } diff --git a/pkg/testsuite/snapshotcreator/options.go b/pkg/testsuite/snapshotcreator/options.go index 4c9c2ef40..3729aae72 100644 --- a/pkg/testsuite/snapshotcreator/options.go +++ b/pkg/testsuite/snapshotcreator/options.go @@ -6,8 +6,8 @@ import ( "github.com/iotaledger/iota-core/pkg/protocol/engine" "github.com/iotaledger/iota-core/pkg/protocol/engine/ledger" ledger1 "github.com/iotaledger/iota-core/pkg/protocol/engine/ledger/ledger" - "github.com/iotaledger/iota-core/pkg/testsuite/mock" iotago "github.com/iotaledger/iota.go/v4" + "github.com/iotaledger/iota.go/v4/wallet" ) // Options stores the details about snapshots created for integration tests. @@ -25,7 +25,7 @@ type Options struct { RootBlocks map[iotago.BlockID]iotago.CommitmentID // GenesisKeyManager defines the key manager used to generate keypair that can spend Genesis outputs. - GenesisKeyManager *mock.KeyManager + GenesisKeyManager *wallet.KeyManager // Accounts defines the accounts that are created in the ledger as part of the Genesis. Accounts []AccountDetails @@ -85,7 +85,7 @@ func WithAddGenesisRootBlock(add bool) options.Option[Options] { } // WithGenesisKeyManager defines the seed used to generate keypair that can spend Genesis outputs. -func WithGenesisKeyManager(keyManager *mock.KeyManager) options.Option[Options] { +func WithGenesisKeyManager(keyManager *wallet.KeyManager) options.Option[Options] { return func(m *Options) { m.GenesisKeyManager = keyManager } diff --git a/pkg/testsuite/snapshotcreator/snapshotcreator.go b/pkg/testsuite/snapshotcreator/snapshotcreator.go index d2d6cedd5..ef66af742 100644 --- a/pkg/testsuite/snapshotcreator/snapshotcreator.go +++ b/pkg/testsuite/snapshotcreator/snapshotcreator.go @@ -30,8 +30,8 @@ import ( "github.com/iotaledger/iota-core/pkg/protocol/sybilprotection/sybilprotectionv1" "github.com/iotaledger/iota-core/pkg/retainer/retainer" "github.com/iotaledger/iota-core/pkg/storage" - "github.com/iotaledger/iota-core/pkg/testsuite/mock" iotago "github.com/iotaledger/iota.go/v4" + "github.com/iotaledger/iota.go/v4/wallet" ) // CreateSnapshot creates a new snapshot. Genesis is defined by genesisTokenAmount and seedBytes, it @@ -189,7 +189,7 @@ func CreateSnapshot(opts ...options.Option[Options]) error { return engineInstance.WriteSnapshot(opt.FilePath) } -func createGenesisOutput(api iotago.API, genesisTokenAmount iotago.BaseToken, genesisMana iotago.Mana, genesisKeyManager *mock.KeyManager) (iotago.Output, error) { +func createGenesisOutput(api iotago.API, genesisTokenAmount iotago.BaseToken, genesisMana iotago.Mana, genesisKeyManager *wallet.KeyManager) (iotago.Output, error) { if genesisTokenAmount > 0 { output := createOutput(genesisKeyManager.Address(iotago.AddressEd25519), genesisTokenAmount, genesisMana) diff --git a/pkg/testsuite/testsuite.go b/pkg/testsuite/testsuite.go index 77cc15942..d5901e2b6 100644 --- a/pkg/testsuite/testsuite.go +++ b/pkg/testsuite/testsuite.go @@ -26,6 +26,7 @@ import ( "github.com/iotaledger/iota-core/pkg/testsuite/snapshotcreator" iotago "github.com/iotaledger/iota.go/v4" "github.com/iotaledger/iota.go/v4/tpkg" + "github.com/iotaledger/iota.go/v4/wallet" ) func DefaultProtocolParameterOptions(networkName string) []options.Option[iotago.V3ProtocolParameters] { @@ -113,7 +114,7 @@ type TestSuite struct { uniqueBlockTimeCounter atomic.Int64 automaticTransactionIssuingCounters shrinkingmap.ShrinkingMap[string, int] mutex syncutils.RWMutex - genesisKeyManager *mock.KeyManager + genesisKeyManager *wallet.KeyManager currentSlot iotago.SlotIndex } @@ -123,7 +124,7 @@ func NewTestSuite(testingT *testing.T, opts ...options.Option[TestSuite]) *TestS return options.Apply(&TestSuite{ Testing: testingT, fakeTesting: &testing.T{}, - genesisKeyManager: mock.NewKeyManager(genesisSeed[:], 0), + genesisKeyManager: lo.PanicOnErr(wallet.NewKeyManager(genesisSeed[:], 0)), network: mock.NewNetwork(), Directory: utils.NewDirectory(testingT.TempDir()), nodes: orderedmap.New[string, *mock.Node](), @@ -471,7 +472,7 @@ func (t *TestSuite) DefaultWallet() *mock.Wallet { return defaultWallet } -func (t *TestSuite) AddWallet(name string, node *mock.Node, accountID iotago.AccountID, keyManager ...*mock.KeyManager) *mock.Wallet { +func (t *TestSuite) AddWallet(name string, node *mock.Node, accountID iotago.AccountID, keyManager ...*wallet.KeyManager) *mock.Wallet { newWallet := mock.NewWallet(t.Testing, name, node, keyManager...) newWallet.SetBlockIssuer(accountID) t.wallets.Set(name, newWallet) From b17b6a1cc439283f626acd223d5630ed5ded28d5 Mon Sep 17 00:00:00 2001 From: Alexander Sporn Date: Mon, 4 Dec 2023 09:43:17 +0100 Subject: [PATCH 2/2] Adapt to latest iota.go wallet pkg --- go.mod | 4 +- go.sum | 4 +- pkg/testsuite/mock/node.go | 4 +- pkg/testsuite/mock/wallet.go | 4 +- pkg/testsuite/testsuite.go | 3 +- pkg/toolset/ed25519.go | 116 ++++++++++++++------------------- tools/gendoc/go.mod | 2 +- tools/gendoc/go.sum | 4 +- tools/genesis-snapshot/go.mod | 3 +- tools/genesis-snapshot/go.sum | 6 +- tools/genesis-snapshot/main.go | 7 +- 11 files changed, 71 insertions(+), 86 deletions(-) diff --git a/go.mod b/go.mod index f24b5f54c..256f3d414 100644 --- a/go.mod +++ b/go.mod @@ -25,7 +25,7 @@ require ( github.com/iotaledger/hive.go/stringify v0.0.0-20231128121006-331a9e522dfe github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231201123347-1c44b3f24221 github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231201114846-3bb5c3fd5665 - github.com/iotaledger/iota.go/v4 v4.0.0-20231201114738-56b50084ab22 + github.com/iotaledger/iota.go/v4 v4.0.0-20231204084048-9c12053adaf6 github.com/labstack/echo/v4 v4.11.3 github.com/labstack/gommon v0.4.1 github.com/libp2p/go-libp2p v0.32.0 @@ -37,7 +37,6 @@ require ( github.com/prometheus/client_golang v1.17.0 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.8.4 - github.com/wollac/iota-crypto-demo v0.0.0-20221117162917-b10619eccb98 github.com/zyedidia/generic v1.2.1 go.uber.org/atomic v1.11.0 go.uber.org/dig v1.17.1 @@ -161,6 +160,7 @@ require ( github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect + github.com/wollac/iota-crypto-demo v0.0.0-20221117162917-b10619eccb98 // indirect go.opencensus.io v0.24.0 // indirect go.opentelemetry.io/otel v1.19.0 // indirect go.opentelemetry.io/otel/metric v1.19.0 // indirect diff --git a/go.sum b/go.sum index d65c68ff1..947972092 100644 --- a/go.sum +++ b/go.sum @@ -307,8 +307,8 @@ github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231201123347-1c44b3f24221 h1:+ozra github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231201123347-1c44b3f24221/go.mod h1:6cLX3gnhP0WL+Q+mf3/rIqfACe5fWKVR8luPXWh2xiY= github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231201114846-3bb5c3fd5665 h1:XdhojOpZ0t0pJFyNO0zlBogSAUrhEI67eCpTC9H6sGM= github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231201114846-3bb5c3fd5665/go.mod h1:obK1N42oafGA7EH6zC4VX2fKh7jTa3WnIa9h1azfxq0= -github.com/iotaledger/iota.go/v4 v4.0.0-20231201114738-56b50084ab22 h1:awGSiqmM1pYF+e+uILNQAiiNXrirU/mTYmUf4f/wVac= -github.com/iotaledger/iota.go/v4 v4.0.0-20231201114738-56b50084ab22/go.mod h1:aO+5iL0vTNwNfE4QMGHVIufGziSI1wTvwJY1ipSMgCk= +github.com/iotaledger/iota.go/v4 v4.0.0-20231204084048-9c12053adaf6 h1:Wuf8Ps3tzuZ5CCbl7LA5O2qwhfuXWo30RCOT0yhm4pg= +github.com/iotaledger/iota.go/v4 v4.0.0-20231204084048-9c12053adaf6/go.mod h1:lCk9rhP3B5pX9BKhzR+Jobq4xPd+GHlqgF4Ga+eQfWA= github.com/ipfs/boxo v0.13.1 h1:nQ5oQzcMZR3oL41REJDcTbrvDvuZh3J9ckc9+ILeRQI= github.com/ipfs/boxo v0.13.1/go.mod h1:btrtHy0lmO1ODMECbbEY1pxNtrLilvKSYLoGQt1yYCk= github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s= diff --git a/pkg/testsuite/mock/node.go b/pkg/testsuite/mock/node.go index b5b74d95b..827507c27 100644 --- a/pkg/testsuite/mock/node.go +++ b/pkg/testsuite/mock/node.go @@ -28,7 +28,6 @@ import ( "github.com/iotaledger/iota-core/pkg/protocol/engine/notarization" iotago "github.com/iotaledger/iota.go/v4" "github.com/iotaledger/iota.go/v4/merklehasher" - "github.com/iotaledger/iota.go/v4/tpkg" "github.com/iotaledger/iota.go/v4/wallet" ) @@ -83,8 +82,7 @@ type Node struct { } func NewNode(t *testing.T, net *Network, partition string, name string, validator bool) *Node { - seed := tpkg.RandEd25519Seed() - keyManager := lo.PanicOnErr(wallet.NewKeyManager(seed[:], 0)) + keyManager := lo.PanicOnErr(wallet.NewKeyManagerFromRandom(wallet.DefaultIOTAPath)) priv, pub := keyManager.KeyPair() accountID := iotago.AccountID(blake2b.Sum256(pub)) diff --git a/pkg/testsuite/mock/wallet.go b/pkg/testsuite/mock/wallet.go index 2770d8380..bc44e166b 100644 --- a/pkg/testsuite/mock/wallet.go +++ b/pkg/testsuite/mock/wallet.go @@ -9,7 +9,6 @@ import ( "github.com/iotaledger/hive.go/lo" "github.com/iotaledger/iota-core/pkg/protocol/engine/utxoledger" iotago "github.com/iotaledger/iota.go/v4" - "github.com/iotaledger/iota.go/v4/tpkg" "github.com/iotaledger/iota.go/v4/wallet" ) @@ -37,8 +36,7 @@ type Wallet struct { func NewWallet(t *testing.T, name string, node *Node, keyManager ...*wallet.KeyManager) *Wallet { var km *wallet.KeyManager if len(keyManager) == 0 { - randomSeed := tpkg.RandEd25519Seed() - km = lo.PanicOnErr(wallet.NewKeyManager(randomSeed[:], 0)) + km = lo.PanicOnErr(wallet.NewKeyManagerFromRandom(wallet.DefaultIOTAPath)) } else { km = keyManager[0] } diff --git a/pkg/testsuite/testsuite.go b/pkg/testsuite/testsuite.go index d5901e2b6..76d7ecc75 100644 --- a/pkg/testsuite/testsuite.go +++ b/pkg/testsuite/testsuite.go @@ -120,11 +120,10 @@ type TestSuite struct { } func NewTestSuite(testingT *testing.T, opts ...options.Option[TestSuite]) *TestSuite { - genesisSeed := tpkg.RandEd25519Seed() return options.Apply(&TestSuite{ Testing: testingT, fakeTesting: &testing.T{}, - genesisKeyManager: lo.PanicOnErr(wallet.NewKeyManager(genesisSeed[:], 0)), + genesisKeyManager: lo.PanicOnErr(wallet.NewKeyManagerFromRandom(wallet.DefaultIOTAPath)), network: mock.NewNetwork(), Directory: utils.NewDirectory(testingT.TempDir()), nodes: orderedmap.New[string, *mock.Node](), diff --git a/pkg/toolset/ed25519.go b/pkg/toolset/ed25519.go index bae379fbf..4cb8ab8fb 100644 --- a/pkg/toolset/ed25519.go +++ b/pkg/toolset/ed25519.go @@ -1,66 +1,62 @@ package toolset import ( - "crypto/ed25519" "encoding/hex" "fmt" "os" flag "github.com/spf13/pflag" - "github.com/wollac/iota-crypto-demo/pkg/bip32path" - "github.com/wollac/iota-crypto-demo/pkg/bip39" - "github.com/wollac/iota-crypto-demo/pkg/slip10" - "github.com/wollac/iota-crypto-demo/pkg/slip10/eddsa" "github.com/iotaledger/hive.go/app/configuration" "github.com/iotaledger/hive.go/crypto" + "github.com/iotaledger/hive.go/ierrors" iotago "github.com/iotaledger/iota.go/v4" + "github.com/iotaledger/iota.go/v4/wallet" ) -func printEd25519Info(mnemonic bip39.Mnemonic, path bip32path.Path, prvKey ed25519.PrivateKey, pubKey ed25519.PublicKey, hrp iotago.NetworkPrefix, outputJSON bool) error { - addr := iotago.Ed25519AddressFromPubKey(pubKey) +type walletInfo struct { + BIP39 string `json:"mnemonic,omitempty"` + BIP32 string `json:"path,omitempty"` + PrivateKey string `json:"privateKey,omitempty"` + PublicKey string `json:"publicKey"` + Ed25519Address string `json:"ed25519"` + Bech32Address string `json:"bech32"` +} - type keys struct { - BIP39 string `json:"mnemonic,omitempty"` - BIP32 string `json:"path,omitempty"` - PrivateKey string `json:"privateKey,omitempty"` - PublicKey string `json:"publicKey"` - Ed25519Address string `json:"ed25519"` - Bech32Address string `json:"bech32"` - } +func printKeyManagerInfo(keyManager *wallet.KeyManager, hrp iotago.NetworkPrefix, outputJSON bool) error { + addr := keyManager.Address(iotago.AddressEd25519) + privKey, pubKey := keyManager.KeyPair() - k := keys{ + w := walletInfo{ PublicKey: hex.EncodeToString(pubKey), - Ed25519Address: hex.EncodeToString(addr[:]), + PrivateKey: hex.EncodeToString(privKey), + Ed25519Address: addr.String(), Bech32Address: addr.Bech32(hrp), + BIP39: keyManager.Mnemonic().String(), + BIP32: keyManager.Path().String(), } - if prvKey != nil { - k.PrivateKey = hex.EncodeToString(prvKey) - } - - if mnemonic != nil { - k.BIP39 = mnemonic.String() - k.BIP32 = path.String() - } + return printWalletInfo(w, outputJSON) +} +func printWalletInfo(info walletInfo, outputJSON bool) error { if outputJSON { - return printJSON(k) + return printJSON(info) } - if len(k.BIP39) > 0 { - fmt.Println("Your seed BIP39 mnemonic: ", k.BIP39) + if len(info.BIP39) > 0 { + fmt.Println("Your seed BIP39 mnemonic: ", info.BIP39) fmt.Println() - fmt.Println("Your BIP32 path: ", k.BIP32) + fmt.Println("Your BIP32 path: ", info.BIP32) } - if k.PrivateKey != "" { - fmt.Println("Your ed25519 private key: ", k.PrivateKey) + if info.PrivateKey != "" { + fmt.Println("Your ed25519 private key: ", info.PrivateKey) } - fmt.Println("Your ed25519 public key: ", k.PublicKey) - fmt.Println("Your ed25519 address: ", k.Ed25519Address) - fmt.Println("Your bech32 address: ", k.Bech32Address) + fmt.Println("Your ed25519 public key: ", info.PublicKey) + fmt.Println("Your ed25519 address: ", info.Ed25519Address) + fmt.Println("Your bech32 address: ", info.Bech32Address) return nil } @@ -93,43 +89,21 @@ func generateEd25519Key(args []string) error { return fmt.Errorf("'%s' not specified", FlagToolBIP32Path) } - var mnemonicSentence bip39.Mnemonic + var err error + var keyManager *wallet.KeyManager if len(*mnemonicFlag) == 0 { - // Generate random entropy by using ed25519 key generation and using the private key seed (32 bytes) - _, random, err := ed25519.GenerateKey(nil) + keyManager, err = wallet.NewKeyManagerFromRandom(*bip32Path) if err != nil { return err } - entropy := random.Seed() - - mnemonicSentence, err = bip39.EntropyToMnemonic(entropy) + } else { + keyManager, err = wallet.NewKeyManagerFromMnemonic(*mnemonicFlag, *bip32Path) if err != nil { return err } - } else { - mnemonicSentence = bip39.ParseMnemonic(*mnemonicFlag) - if len(mnemonicSentence) != 24 { - return fmt.Errorf("'%s' contains an invalid sentence length. Mnemonic should be 24 words", FlagToolMnemonic) - } - } - - path, err := bip32path.ParsePath(*bip32Path) - if err != nil { - return err } - seed, err := bip39.MnemonicToSeed(mnemonicSentence, "") - if err != nil { - return err - } - - key, err := slip10.DeriveKeyFromPath(seed, eddsa.Ed25519(), path) - if err != nil { - return err - } - pubKey, prvKey := key.Key.(eddsa.Seed).Ed25519Key() - - return printEd25519Info(mnemonicSentence, path, ed25519.PrivateKey(prvKey), ed25519.PublicKey(pubKey), iotago.NetworkPrefix(*hrpFlag), *outputJSONFlag) + return printKeyManagerInfo(keyManager, iotago.NetworkPrefix(*hrpFlag), *outputJSONFlag) } func generateEd25519Address(args []string) error { @@ -155,18 +129,28 @@ func generateEd25519Address(args []string) error { } if len(*hrpFlag) == 0 { - return fmt.Errorf("'%s' not specified", FlagToolHRP) + return ierrors.Errorf("'%s' not specified", FlagToolHRP) } if len(*publicKeyFlag) == 0 { - return fmt.Errorf("'%s' not specified", FlagToolPublicKey) + return ierrors.Errorf("'%s' not specified", FlagToolPublicKey) } // parse pubkey pubKey, err := crypto.ParseEd25519PublicKeyFromString(*publicKeyFlag) if err != nil { - return fmt.Errorf("can't decode '%s': %w", FlagToolPublicKey, err) + return ierrors.Wrapf(err, "can't decode '%s'", FlagToolPublicKey) + } + + addr := iotago.Ed25519AddressFromPubKey(pubKey) + + hrp := iotago.NetworkPrefix(*hrpFlag) + + w := walletInfo{ + PublicKey: hex.EncodeToString(pubKey), + Ed25519Address: addr.String(), + Bech32Address: addr.Bech32(hrp), } - return printEd25519Info(nil, nil, nil, pubKey, iotago.NetworkPrefix(*hrpFlag), *outputJSONFlag) + return printWalletInfo(w, *outputJSONFlag) } diff --git a/tools/gendoc/go.mod b/tools/gendoc/go.mod index d93bf4c5e..ee1992d2c 100644 --- a/tools/gendoc/go.mod +++ b/tools/gendoc/go.mod @@ -72,7 +72,7 @@ require ( github.com/iotaledger/hive.go/stringify v0.0.0-20231128121006-331a9e522dfe // indirect github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231201123347-1c44b3f24221 // indirect github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231201114846-3bb5c3fd5665 // indirect - github.com/iotaledger/iota.go/v4 v4.0.0-20231201114738-56b50084ab22 // indirect + github.com/iotaledger/iota.go/v4 v4.0.0-20231204084048-9c12053adaf6 // indirect github.com/ipfs/boxo v0.13.1 // indirect github.com/ipfs/go-cid v0.4.1 // indirect github.com/ipfs/go-datastore v0.6.0 // indirect diff --git a/tools/gendoc/go.sum b/tools/gendoc/go.sum index 1208a30b0..734814b2e 100644 --- a/tools/gendoc/go.sum +++ b/tools/gendoc/go.sum @@ -311,8 +311,8 @@ github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231201123347-1c44b3f24221 h1:+ozra github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231201123347-1c44b3f24221/go.mod h1:6cLX3gnhP0WL+Q+mf3/rIqfACe5fWKVR8luPXWh2xiY= github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231201114846-3bb5c3fd5665 h1:XdhojOpZ0t0pJFyNO0zlBogSAUrhEI67eCpTC9H6sGM= github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231201114846-3bb5c3fd5665/go.mod h1:obK1N42oafGA7EH6zC4VX2fKh7jTa3WnIa9h1azfxq0= -github.com/iotaledger/iota.go/v4 v4.0.0-20231201114738-56b50084ab22 h1:awGSiqmM1pYF+e+uILNQAiiNXrirU/mTYmUf4f/wVac= -github.com/iotaledger/iota.go/v4 v4.0.0-20231201114738-56b50084ab22/go.mod h1:aO+5iL0vTNwNfE4QMGHVIufGziSI1wTvwJY1ipSMgCk= +github.com/iotaledger/iota.go/v4 v4.0.0-20231204084048-9c12053adaf6 h1:Wuf8Ps3tzuZ5CCbl7LA5O2qwhfuXWo30RCOT0yhm4pg= +github.com/iotaledger/iota.go/v4 v4.0.0-20231204084048-9c12053adaf6/go.mod h1:lCk9rhP3B5pX9BKhzR+Jobq4xPd+GHlqgF4Ga+eQfWA= github.com/ipfs/boxo v0.13.1 h1:nQ5oQzcMZR3oL41REJDcTbrvDvuZh3J9ckc9+ILeRQI= github.com/ipfs/boxo v0.13.1/go.mod h1:btrtHy0lmO1ODMECbbEY1pxNtrLilvKSYLoGQt1yYCk= github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s= diff --git a/tools/genesis-snapshot/go.mod b/tools/genesis-snapshot/go.mod index aa2165797..416b59558 100644 --- a/tools/genesis-snapshot/go.mod +++ b/tools/genesis-snapshot/go.mod @@ -10,7 +10,7 @@ require ( github.com/iotaledger/hive.go/lo v0.0.0-20231128121006-331a9e522dfe github.com/iotaledger/hive.go/runtime v0.0.0-20231128121006-331a9e522dfe github.com/iotaledger/iota-core v0.0.0-00010101000000-000000000000 - github.com/iotaledger/iota.go/v4 v4.0.0-20231201114738-56b50084ab22 + github.com/iotaledger/iota.go/v4 v4.0.0-20231204084048-9c12053adaf6 github.com/mr-tron/base58 v1.2.0 github.com/spf13/pflag v1.0.5 golang.org/x/crypto v0.16.0 @@ -62,6 +62,7 @@ require ( golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect golang.org/x/sync v0.5.0 // indirect golang.org/x/sys v0.15.0 // indirect + golang.org/x/text v0.14.0 // indirect google.golang.org/protobuf v1.31.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect lukechampine.com/blake3 v1.2.1 // indirect diff --git a/tools/genesis-snapshot/go.sum b/tools/genesis-snapshot/go.sum index 138a2d62f..30c064306 100644 --- a/tools/genesis-snapshot/go.sum +++ b/tools/genesis-snapshot/go.sum @@ -52,8 +52,8 @@ github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231127134220-90b88e3 github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231127134220-90b88e35bdb2/go.mod h1:FoH3T6yKlZJp8xm8K+zsQiibSynp32v21CpWx8xkek8= github.com/iotaledger/hive.go/stringify v0.0.0-20231128121006-331a9e522dfe h1:RcFUqhnJ+86+sA0XMrZ0q+086ULrdWQkWrjUt2OnJK4= github.com/iotaledger/hive.go/stringify v0.0.0-20231128121006-331a9e522dfe/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs= -github.com/iotaledger/iota.go/v4 v4.0.0-20231201114738-56b50084ab22 h1:awGSiqmM1pYF+e+uILNQAiiNXrirU/mTYmUf4f/wVac= -github.com/iotaledger/iota.go/v4 v4.0.0-20231201114738-56b50084ab22/go.mod h1:aO+5iL0vTNwNfE4QMGHVIufGziSI1wTvwJY1ipSMgCk= +github.com/iotaledger/iota.go/v4 v4.0.0-20231204084048-9c12053adaf6 h1:Wuf8Ps3tzuZ5CCbl7LA5O2qwhfuXWo30RCOT0yhm4pg= +github.com/iotaledger/iota.go/v4 v4.0.0-20231204084048-9c12053adaf6/go.mod h1:lCk9rhP3B5pX9BKhzR+Jobq4xPd+GHlqgF4Ga+eQfWA= github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s= github.com/ipfs/go-cid v0.4.1/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk= github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg= @@ -131,6 +131,8 @@ golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= diff --git a/tools/genesis-snapshot/main.go b/tools/genesis-snapshot/main.go index 05728937e..2bec17bae 100644 --- a/tools/genesis-snapshot/main.go +++ b/tools/genesis-snapshot/main.go @@ -8,9 +8,9 @@ import ( "github.com/iotaledger/hive.go/ierrors" "github.com/iotaledger/hive.go/runtime/options" - "github.com/iotaledger/iota-core/pkg/testsuite/mock" "github.com/iotaledger/iota-core/pkg/testsuite/snapshotcreator" "github.com/iotaledger/iota-core/tools/genesis-snapshot/presets" + "github.com/iotaledger/iota.go/v4/wallet" ) func main() { @@ -49,7 +49,10 @@ func parseFlags() (opt []options.Option[snapshotcreator.Options], conf string) { if err != nil { log.Fatal(ierrors.Wrap(err, "failed to decode base58 seed")) } - keyManager := mock.NewKeyManager(genesisSeed[:], 0) + keyManager, err := wallet.NewKeyManager(genesisSeed[:], wallet.DefaultIOTAPath) + if err != nil { + log.Fatal(ierrors.Wrap(err, "failed to create KeyManager from seed")) + } opt = append(opt, snapshotcreator.WithGenesisKeyManager(keyManager)) return opt, *config