Skip to content

Commit

Permalink
feat: (wasp-cli) use bpi-44 derivation path for wallet seed
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemmsilva committed Nov 8, 2023
1 parent 5e6e717 commit 7f26e76
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 23 deletions.
12 changes: 4 additions & 8 deletions contracts/wasm/testwasmlib/test/testwasmlib_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ func setupClientCluster(t *testing.T) *wasmclient.WasmClientContext {
templates.WaspConfig = strings.ReplaceAll(templates.WaspConfig, "rocksdb", "mapdb")
env := clustertests.SetupWithChain(t)
templates.WaspConfig = strings.ReplaceAll(templates.WaspConfig, "mapdb", "rocksdb")
seed := cryptolib.SeedFromBytes(wasmtypes.BytesFromString(mySeed))
wallet := cryptolib.KeyPairFromSeed(seed.SubSeed(0))
wallet := cryptolib.KeyPairFromSeed(cryptolib.SubSeed(wasmtypes.BytesFromString(mySeed), 0))

// request funds to the wallet that the wasmclient will use
err := env.Clu.RequestFunds(wallet.Address())
Expand Down Expand Up @@ -127,8 +126,7 @@ func setupClientDisposable(t testing.TB) *wasmclient.WasmClientContext {
cfgWaspAPI := cfgWasp["0"].(string)

// we'll use the seed keypair to sign requests
seed := cryptolib.SeedFromBytes(wasmtypes.BytesFromString(cfgSeed))
wallet := cryptolib.KeyPairFromSeed(seed.SubSeed(0))
wallet := cryptolib.KeyPairFromSeed(cryptolib.SubSeed(wasmtypes.BytesFromString(cfgSeed), 0))

svc := wasmclient.NewWasmClientService(cfgWaspAPI)
require.True(t, svc.IsHealthy())
Expand Down Expand Up @@ -338,8 +336,7 @@ func setupClientLib(t *testing.T) *wasmclient.WasmClientContext {
ctx := wasmclient.NewWasmClientContext(svc, testwasmlib.ScName)
require.NoError(t, ctx.Err)

seed := cryptolib.SeedFromBytes(wasmtypes.BytesFromString(mySeed))
wallet := cryptolib.KeyPairFromSeed(seed.SubSeed(0))
wallet := cryptolib.KeyPairFromSeed(cryptolib.SubSeed(wasmtypes.BytesFromString(mySeed), 0))
ctx.SignRequests(wallet)
require.NoError(t, ctx.Err)
return ctx
Expand Down Expand Up @@ -413,8 +410,7 @@ func testAPIErrorHandling(t *testing.T, ctx *wasmclient.WasmClientContext) {
// fmt.Println("Error: " + ctx.Err.Error())

fmt.Println("check sign with wrong wallet")
seed := cryptolib.SeedFromBytes(wasmtypes.BytesFromString(mySeed))
wallet := cryptolib.KeyPairFromSeed(seed.SubSeed(1))
wallet := cryptolib.KeyPairFromSeed(cryptolib.SubSeed(wasmtypes.BytesFromString(mySeed), 1))
ctx.SignRequests(wallet)
f := testwasmlib.ScFuncs.Random(ctx)
f.Func.Post()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ require (
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/wollac/iota-crypto-demo v0.0.0-20221117162917-b10619eccb98 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
go.dedis.ch/fixbuf v1.0.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,8 @@ github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49u
github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
github.com/wasmerio/wasmer-go v1.0.4 h1:MnqHoOGfiQ8MMq2RF6wyCeebKOe84G88h5yv+vmxJgs=
github.com/wasmerio/wasmer-go v1.0.4/go.mod h1:0gzVdSfg6pysA6QVp6iVRPTagC6Wq9pOE8J86WKb2Fk=
github.com/wollac/iota-crypto-demo v0.0.0-20221117162917-b10619eccb98 h1:i7k63xHOX2ntuHrhHewfKro67c834jug2DIk599fqAA=
github.com/wollac/iota-crypto-demo v0.0.0-20221117162917-b10619eccb98/go.mod h1:Knu2XMRWe8SkwTlHc/+ghP+O9DEaZRQQEyTjvLJ5Cck=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I=
github.com/xdg/stringprep v1.0.0/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
Expand Down
51 changes: 46 additions & 5 deletions packages/cryptolib/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,56 @@ package cryptolib
import (
"crypto/ed25519"
"encoding/binary"
"fmt"

"golang.org/x/crypto/blake2b"
"github.com/minio/blake2b-simd"

Check failure on line 8 in packages/cryptolib/seed.go

View workflow job for this annotation

GitHub Actions / Test

missing go.sum entry for module providing package github.com/minio/blake2b-simd (imported by github.com/iotaledger/wasp/packages/cryptolib); to add:

Check failure on line 8 in packages/cryptolib/seed.go

View workflow job for this annotation

GitHub Actions / Wasm contract tests (go)

missing go.sum entry for module providing package github.com/minio/blake2b-simd (imported by github.com/iotaledger/wasp/packages/cryptolib); to add:

Check failure on line 8 in packages/cryptolib/seed.go

View workflow job for this annotation

GitHub Actions / Wasm contract tests (rswasm)

missing go.sum entry for module providing package github.com/minio/blake2b-simd (imported by github.com/iotaledger/wasp/packages/cryptolib); to add:
"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"

hivecrypto "github.com/iotaledger/hive.go/crypto/ed25519"
"github.com/iotaledger/wasp/packages/cryptolib/byteutils"
"github.com/iotaledger/wasp/packages/parameters"
)

// testnet/alphanet uses COIN_TYPE = 1
const TestnetCoinType = uint32(1)

// / IOTA coin type <https://github.com/satoshilabs/slips/blob/master/slip-0044.md>
const IotaCoinType = uint32(4218)

// / Shimmer coin type <https://github.com/satoshilabs/slips/blob/master/slip-0044.md>
const ShimmerCoinType = uint32(4219)

// SubSeed returns a Seed (ed25519 Seed) from a master seed (that has arbitrary length)
func SubSeed(walletSeed []byte, accountIndex uint64, useLegacyDerivation ...bool) Seed {
if len(useLegacyDerivation) > 0 && useLegacyDerivation[0] {
seed := SeedFromBytes(walletSeed)
return legacyDerivation(&seed, accountIndex)
}

coinType := TestnetCoinType // default to the testnet
switch parameters.L1().Protocol.Bech32HRP {
case "iota":
coinType = IotaCoinType
case "smr":
coinType = ShimmerCoinType
}

bip32Path := fmt.Sprintf("m/44'/%d'/%d'/0'/0'", coinType, accountIndex) // this is the same as FF does it (only the account index changes, the ADDRESS_INDEX stays 0)
path, err := bip32path.ParsePath(bip32Path)
if err != nil {
panic(err)
}
key, err := slip10.DeriveKeyFromPath(walletSeed, eddsa.Ed25519(), path)
if err != nil {
panic(err)
}
_, prvKey := key.Key.(eddsa.Seed).Ed25519Key()
return SeedFromBytes(prvKey)
}

// ---
const (
SeedSize = ed25519.SeedSize
)
Expand All @@ -26,14 +69,12 @@ func SeedFromBytes(data []byte) (ret Seed) {
return ret
}

func (seed *Seed) SubSeed(n uint64) Seed {
func legacyDerivation(seed *Seed, index uint64) Seed {
subSeed := make([]byte, SeedSize)

indexBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(indexBytes, n)
binary.LittleEndian.PutUint64(indexBytes, index)
hashOfIndexBytes := blake2b.Sum256(indexBytes)

Check failure on line 76 in packages/cryptolib/seed.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: blake2b (typecheck)

byteutils.XORBytes(subSeed, seed[:], hashOfIndexBytes[:])

return SeedFromBytes(subSeed)
}
4 changes: 2 additions & 2 deletions packages/solo/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,10 @@ func (ch *Chain) L1L2Funds(addr iotago.Address) *L1L2AddressAssets {
func (ch *Chain) GetL2FundsFromFaucet(agentID isc.AgentID, baseTokens ...uint64) {
// find a deterministic L1 address that has 0 balance
walletKey, walletAddr := func() (*cryptolib.KeyPair, iotago.Address) {
seed := cryptolib.SeedFromBytes([]byte("GetL2FundsFromFaucet"))
masterSeed := []byte("GetL2FundsFromFaucet")
i := uint64(0)
for {
ss := seed.SubSeed(i)
ss := cryptolib.SubSeed(masterSeed, i)
key, addr := ch.Env.NewKeyPair(&ss)
_, err := ch.Env.GetFundsFromFaucet(addr)
require.NoError(ch.Env.T, err)
Expand Down
2 changes: 1 addition & 1 deletion packages/testutil/testdbhash/TestStorageContract.hex
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0x3413c0b12114d471fbef75736eeeefbee2df902370539bbb5d54459f5425384b
0xe2dc176c7b9f293e8a1a82e8cff880a8e33f4c12bad5bb59ee831d48b0665489
6 changes: 2 additions & 4 deletions packages/wasmvm/wasmclient/go/test/wasmclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ func setupClient(t *testing.T) *wasmclient.WasmClientContext {
ctx := wasmclient.NewWasmClientContext(svc, testwasmlib.ScName)
require.NoError(t, ctx.Err)

seed := cryptolib.SeedFromBytes(wasmtypes.BytesFromString(mySeed))
wallet := cryptolib.KeyPairFromSeed(seed.SubSeed(0))
wallet := cryptolib.KeyPairFromSeed(cryptolib.SubSeed(wasmtypes.BytesFromString(mySeed), 0))
ctx.SignRequests(wallet)
require.NoError(t, ctx.Err)
return ctx
Expand Down Expand Up @@ -111,8 +110,7 @@ func TestErrorHandling(t *testing.T) {
// fmt.Println("Error: " + ctx.Err.Error())

// sign with wrong wallet
seed := cryptolib.SeedFromBytes(wasmtypes.BytesFromString(mySeed))
wallet := cryptolib.KeyPairFromSeed(seed.SubSeed(1))
wallet := cryptolib.KeyPairFromSeed(cryptolib.SubSeed(wasmtypes.BytesFromString(mySeed), 1))
ctx.SignRequests(wallet)
f := testwasmlib.ScFuncs.Random(ctx)
f.Func.Post()
Expand Down
6 changes: 3 additions & 3 deletions tools/wasp-cli/cli/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ type Wallet struct {

func Load() *Wallet {
seedHex := viper.GetString("wallet.seed")
useLegacyDerivation := viper.GetBool("wallet.useLegacyDerivation")
if seedHex == "" {
log.Fatal("call `init` first")
}

seedBytes, err := iotago.DecodeHex(seedHex)
masterSeed, err := iotago.DecodeHex(seedHex)
log.Check(err)

seed := cryptolib.SeedFromBytes(seedBytes)
kp := cryptolib.KeyPairFromSeed(seed.SubSeed(uint64(AddressIndex)))
kp := cryptolib.KeyPairFromSeed(cryptolib.SubSeed(masterSeed, uint64(AddressIndex), useLegacyDerivation))

return &Wallet{KeyPair: kp, AddressIndex: AddressIndex}
}
Expand Down
2 changes: 2 additions & 0 deletions tools/wasp-cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ require (
github.com/miekg/dns v1.1.55 // indirect
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b // indirect
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc // indirect
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand Down Expand Up @@ -190,6 +191,7 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/wasmerio/wasmer-go v1.0.4 // indirect
github.com/wollac/iota-crypto-demo v0.0.0-20221117162917-b10619eccb98 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.dedis.ch/fixbuf v1.0.3 // indirect
go.dedis.ch/kyber/v3 v3.1.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions tools/wasp-cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b h1:z78hV3sbSMAUoyUM
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b/go.mod h1:lxPUiZwKoFL8DUUmalo2yJJUCxbPKtm8OKfqr2/FTNU=
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc h1:PTfri+PuQmWDqERdnNMiD9ZejrlswWrCpBEZgWOiTrc=
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc/go.mod h1:cGKTAVKx4SxOuR/czcZ/E2RSJ3sfHs8FpHhQ5CWMf9s=
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM=
Expand Down Expand Up @@ -822,6 +823,8 @@ github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49u
github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
github.com/wasmerio/wasmer-go v1.0.4 h1:MnqHoOGfiQ8MMq2RF6wyCeebKOe84G88h5yv+vmxJgs=
github.com/wasmerio/wasmer-go v1.0.4/go.mod h1:0gzVdSfg6pysA6QVp6iVRPTagC6Wq9pOE8J86WKb2Fk=
github.com/wollac/iota-crypto-demo v0.0.0-20221117162917-b10619eccb98 h1:i7k63xHOX2ntuHrhHewfKro67c834jug2DIk599fqAA=
github.com/wollac/iota-crypto-demo v0.0.0-20221117162917-b10619eccb98/go.mod h1:Knu2XMRWe8SkwTlHc/+ghP+O9DEaZRQQEyTjvLJ5Cck=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I=
github.com/xdg/stringprep v1.0.0/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down

0 comments on commit 7f26e76

Please sign in to comment.