Skip to content

Commit

Permalink
maintainence(lib/runtime/wasmer): add support for polkadot runtime v0…
Browse files Browse the repository at this point in the history
….9.10 (ChainSafe#1818)
  • Loading branch information
noot authored and timwu20 committed Dec 6, 2021
1 parent 428cb57 commit c4cdb8a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 16 deletions.
13 changes: 13 additions & 0 deletions lib/crypto/secp256k1/secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const PrivateKeyLength = 32
// SignatureLength is the fixed Signature Length
const SignatureLength = 64

// SignatureLengthRecovery is the length of a secp256k1 signature with recovery byte (used for ecrecover)
const SignatureLengthRecovery = 65

// MessageLength is the fixed Message Length
const MessageLength = 32

Expand All @@ -48,11 +51,21 @@ type PublicKey struct {

// RecoverPublicKey returns the 64-byte uncompressed public key that created the given signature.
func RecoverPublicKey(msg, sig []byte) ([]byte, error) {
// update recovery bit
if sig[64] >= 27 {
sig[64] -= 27
}

return secp256k1.Ecrecover(msg, sig)
}

// RecoverPublicKeyCompressed returns the 33-byte compressed public key that signed the given message.
func RecoverPublicKeyCompressed(msg, sig []byte) ([]byte, error) {
// update recovery bit
if sig[64] >= 27 {
sig[64] -= 27
}

pub, err := secp256k1.SigToPub(msg, sig)
if err != nil {
return nil, err
Expand Down
5 changes: 5 additions & 0 deletions lib/runtime/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ const (
NODE_RUNTIME_FP_v098 = "node_runtime-v0.9.8.compact.wasm"
NODE_RUNTIME_URL_v098 = "https://github.com/noot/substrate/blob/noot/v0.9.8/target/debug/wbuild/node-runtime/node_runtime.compact.wasm?raw=true"

// v0.9.10 polkadot runtime
POLKADOT_RUNTIME_v0910 = "polkadot_runtime-v9100"
POLKADOT_RUNTIME_FP_v0910 = "polkadot_runtime-v9100.compact.wasm"
POLKADOT_RUNTIME_URL_v0910 = "https://github.com/paritytech/polkadot/releases/download/v0.9.10/polkadot_runtime-v9100.compact.wasm"

// v0.8 polkadot runtime
POLKADOT_RUNTIME = "polkadot_runtime"
POLKADOT_RUNTIME_FP = "polkadot_runtime.compact.wasm"
Expand Down
2 changes: 2 additions & 0 deletions lib/runtime/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func GetRuntimeVars(targetRuntime string) (string, string) {
return GetAbsolutePath(NODE_RUNTIME_FP), NODE_RUNTIME_URL
case NODE_RUNTIME_v098:
return GetAbsolutePath(NODE_RUNTIME_FP_v098), NODE_RUNTIME_URL_v098
case POLKADOT_RUNTIME_v0910:
return GetAbsolutePath(POLKADOT_RUNTIME_FP_v0910), POLKADOT_RUNTIME_URL_v0910
case POLKADOT_RUNTIME:
return GetAbsolutePath(POLKADOT_RUNTIME_FP), POLKADOT_RUNTIME_URL
case HOST_API_TEST_RUNTIME:
Expand Down
31 changes: 31 additions & 0 deletions lib/runtime/wasmer/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,37 @@ func TestInstance_Version_NodeRuntime_v098(t *testing.T) {
require.Equal(t, expected.TransactionVersion(), version.TransactionVersion())
}

func TestInstance_Version_PolkadotRuntime_v0910(t *testing.T) {
expected := runtime.NewVersionData(
[]byte("polkadot"),
[]byte("parity-polkadot"),
0,
9100,
0,
nil,
8,
)

instance := NewTestInstance(t, runtime.POLKADOT_RUNTIME_v0910)
version, err := instance.Version()
require.NoError(t, err)

t.Logf("SpecName: %s\n", version.SpecName())
t.Logf("ImplName: %s\n", version.ImplName())
t.Logf("AuthoringVersion: %d\n", version.AuthoringVersion())
t.Logf("SpecVersion: %d\n", version.SpecVersion())
t.Logf("ImplVersion: %d\n", version.ImplVersion())
t.Logf("TransactionVersion: %d\n", version.TransactionVersion())

require.Equal(t, 14, len(version.APIItems()))
require.Equal(t, expected.SpecName(), version.SpecName())
require.Equal(t, expected.ImplName(), version.ImplName())
require.Equal(t, expected.AuthoringVersion(), version.AuthoringVersion())
require.Equal(t, expected.SpecVersion(), version.SpecVersion())
require.Equal(t, expected.ImplVersion(), version.ImplVersion())
require.Equal(t, expected.TransactionVersion(), version.TransactionVersion())
}

func TestInstance_Version_PolkadotRuntime(t *testing.T) {
expected := runtime.NewVersionData(
[]byte("polkadot"),
Expand Down
38 changes: 22 additions & 16 deletions lib/runtime/wasmer/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ package wasmer
// extern int32_t ext_crypto_ed25519_verify_version_1(void *context, int32_t a, int64_t b, int32_t c);
// extern int32_t ext_crypto_finish_batch_verify_version_1(void *context);
// extern int64_t ext_crypto_secp256k1_ecdsa_recover_version_1(void *context, int32_t a, int32_t b);
// extern int64_t ext_crypto_secp256k1_ecdsa_recover_version_2(void *context, int32_t a, int32_t b);
// extern int64_t ext_crypto_secp256k1_ecdsa_recover_compressed_version_1(void *context, int32_t a, int32_t b);
// extern int64_t ext_crypto_secp256k1_ecdsa_recover_compressed_version_2(void *context, int32_t a, int32_t b);
// extern int32_t ext_crypto_sr25519_generate_version_1(void *context, int32_t a, int64_t b);
// extern int64_t ext_crypto_sr25519_public_keys_version_1(void *context, int32_t a);
// extern int64_t ext_crypto_sr25519_sign_version_1(void *context, int32_t a, int32_t b, int64_t c);
Expand Down Expand Up @@ -418,14 +420,6 @@ func ext_crypto_secp256k1_ecdsa_recover_version_1(context unsafe.Pointer, sig, m
message := memory[msg : msg+32]
signature := memory[sig : sig+65]

if signature[64] == 27 {
signature[64] = 0
}

if signature[64] == 28 {
signature[64] = 1
}

pub, err := secp256k1.RecoverPublicKey(message, signature)
if err != nil {
logger.Error("[ext_crypto_secp256k1_ecdsa_recover_version_1] failed to recover public key", "error", err)
Expand All @@ -449,6 +443,12 @@ func ext_crypto_secp256k1_ecdsa_recover_version_1(context unsafe.Pointer, sig, m
return C.int64_t(ret)
}

//export ext_crypto_secp256k1_ecdsa_recover_version_2
func ext_crypto_secp256k1_ecdsa_recover_version_2(context unsafe.Pointer, sig, msg C.int32_t) C.int64_t {
logger.Trace("[ext_crypto_secp256k1_ecdsa_recover_version_2] executing...")
return ext_crypto_secp256k1_ecdsa_recover_version_1(context, sig, msg)
}

//export ext_crypto_secp256k1_ecdsa_recover_compressed_version_1
func ext_crypto_secp256k1_ecdsa_recover_compressed_version_1(context unsafe.Pointer, sig, msg C.int32_t) C.int64_t {
logger.Trace("[ext_crypto_secp256k1_ecdsa_recover_compressed_version_1] executing...")
Expand All @@ -461,14 +461,6 @@ func ext_crypto_secp256k1_ecdsa_recover_compressed_version_1(context unsafe.Poin
message := memory[msg : msg+32]
signature := memory[sig : sig+65]

if signature[64] == 27 {
signature[64] = 0
}

if signature[64] == 28 {
signature[64] = 1
}

cpub, err := secp256k1.RecoverPublicKeyCompressed(message, signature)
if err != nil {
logger.Error("[ext_crypto_secp256k1_ecdsa_recover_compressed_version_1] failed to recover public key", "error", err)
Expand All @@ -487,6 +479,12 @@ func ext_crypto_secp256k1_ecdsa_recover_compressed_version_1(context unsafe.Poin
return C.int64_t(ret)
}

//export ext_crypto_secp256k1_ecdsa_recover_compressed_version_2
func ext_crypto_secp256k1_ecdsa_recover_compressed_version_2(context unsafe.Pointer, sig, msg C.int32_t) C.int64_t {
logger.Trace("[ext_crypto_secp256k1_ecdsa_recover_compressed_version_2] executing...")
return ext_crypto_secp256k1_ecdsa_recover_compressed_version_1(context, sig, msg)
}

//export ext_crypto_sr25519_generate_version_1
func ext_crypto_sr25519_generate_version_1(context unsafe.Pointer, keyTypeID C.int32_t, seedSpan C.int64_t) C.int32_t {
logger.Trace("[ext_crypto_sr25519_generate_version_1] executing...")
Expand Down Expand Up @@ -1992,10 +1990,18 @@ func ImportsNodeRuntime() (*wasm.Imports, error) { //nolint
if err != nil {
return nil, err
}
_, err = imports.Append("ext_crypto_secp256k1_ecdsa_recover_version_2", ext_crypto_secp256k1_ecdsa_recover_version_2, C.ext_crypto_secp256k1_ecdsa_recover_version_2)
if err != nil {
return nil, err
}
_, err = imports.Append("ext_crypto_secp256k1_ecdsa_recover_compressed_version_1", ext_crypto_secp256k1_ecdsa_recover_compressed_version_1, C.ext_crypto_secp256k1_ecdsa_recover_compressed_version_1)
if err != nil {
return nil, err
}
_, err = imports.Append("ext_crypto_secp256k1_ecdsa_recover_compressed_version_2", ext_crypto_secp256k1_ecdsa_recover_compressed_version_2, C.ext_crypto_secp256k1_ecdsa_recover_compressed_version_2)
if err != nil {
return nil, err
}
_, err = imports.Append("ext_crypto_sr25519_generate_version_1", ext_crypto_sr25519_generate_version_1, C.ext_crypto_sr25519_generate_version_1)
if err != nil {
return nil, err
Expand Down

0 comments on commit c4cdb8a

Please sign in to comment.