Skip to content

Commit

Permalink
fix(lib/crypto/ed25519): update ed25519 to use go-schnorrkel bip39 de…
Browse files Browse the repository at this point in the history
…rivation (#1488)
  • Loading branch information
noot committed Mar 24, 2021
1 parent d82b2da commit dfb95d2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
7 changes: 5 additions & 2 deletions lib/crypto/ed25519/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto"

bip39 "github.com/cosmos/go-bip39"
"github.com/ChainSafe/go-schnorrkel"
)

// PublicKeyLength is the fixed Public Key Length
Expand Down Expand Up @@ -120,7 +120,10 @@ func NewKeypairFromPrivateKeyString(in string) (*Keypair, error) {

// NewKeypairFromMnenomic returns a new Keypair using the given mnemonic and password.
func NewKeypairFromMnenomic(mnemonic, password string) (*Keypair, error) {
seed := bip39.NewSeed(mnemonic, password)
seed, err := schnorrkel.SeedFromMnemonic(mnemonic, password)
if err != nil {
return nil, err
}
return NewKeypairFromSeed(seed[:32])
}

Expand Down
11 changes: 11 additions & 0 deletions lib/crypto/ed25519/ed25519_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"reflect"
"testing"

"github.com/ChainSafe/gossamer/lib/common"

bip39 "github.com/cosmos/go-bip39"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -101,3 +103,12 @@ func TestNewKeypairFromMnenomic(t *testing.T) {
_, err = NewKeypairFromMnenomic(mnemonic, "")
require.NoError(t, err)
}

func TestNewKeypairFromMnenomic_Again(t *testing.T) {
mnemonic := "twist sausage october vivid neglect swear crumble hawk beauty fabric egg fragile"
kp, err := NewKeypairFromMnenomic(mnemonic, "")
require.NoError(t, err)

expectedPubkey := common.MustHexToBytes("0xf56d9231e7b7badd3f1e10ad15ef8aa08b70839723d0a2d10d7329f0ea2b8c61")
require.Equal(t, expectedPubkey, kp.Public().Encode())
}
10 changes: 5 additions & 5 deletions lib/runtime/wasmer/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ func ext_logging_log_version_1(context unsafe.Pointer, level C.int32_t, targetDa
logger.Trace("[ext_logging_log_version_1] executing...")
instanceContext := wasm.IntoInstanceContext(context)

target := fmt.Sprintf("%s", asMemorySlice(instanceContext, targetData))
msg := fmt.Sprintf("%s", asMemorySlice(instanceContext, msgData))
target := string(asMemorySlice(instanceContext, targetData))
msg := string(asMemorySlice(instanceContext, msgData))

switch int(level) {
case 0:
Expand Down Expand Up @@ -215,7 +215,7 @@ func ext_crypto_ed25519_generate_version_1(context unsafe.Pointer, keyTypeID C.i
var kp crypto.Keypair

if seed.Exists() {
kp, err = ed25519.NewKeypairFromMnenomic(string(seedBytes), "")
kp, err = ed25519.NewKeypairFromMnenomic(string(seed.Value()), "")
} else {
kp, err = ed25519.GenerateKeypair()
}
Expand Down Expand Up @@ -849,7 +849,7 @@ func ext_misc_print_utf8_version_1(context unsafe.Pointer, dataSpan C.int64_t) {

instanceContext := wasm.IntoInstanceContext(context)
data := asMemorySlice(instanceContext, dataSpan)
logger.Debug("[ext_misc_print_utf8_version_1]", "utf8", fmt.Sprintf("%s", data))
logger.Debug("[ext_misc_print_utf8_version_1]", "utf8", string(data))
}

//export ext_misc_runtime_version_version_1
Expand Down Expand Up @@ -1241,7 +1241,7 @@ func ext_hashing_twox_128_version_1(context unsafe.Pointer, dataSpan C.int64_t)
return 0
}

logger.Debug("[ext_hashing_twox_128_version_1]", "data", fmt.Sprintf("%s", data), "hash", fmt.Sprintf("0x%x", hash))
logger.Debug("[ext_hashing_twox_128_version_1]", "data", string(data), "hash", fmt.Sprintf("0x%x", hash))

out, err := toWasmMemorySized(instanceContext, hash, 16)
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions lib/runtime/wasmer/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,6 @@ func Test_ext_crypto_ed25519_generate_version_1(t *testing.T) {
mnemonic, err := crypto.NewBIP39Mnemonic()
require.NoError(t, err)

// TODO: we currently don't provide a seed since the spec says the seed is an optional BIP-39 seed
// clarify whether this is a mnemonic or not
data := optional.NewBytes(true, []byte(mnemonic))
seedData, err := data.Encode()
require.NoError(t, err)
Expand Down

0 comments on commit dfb95d2

Please sign in to comment.