Skip to content

Commit

Permalink
Add support for Substrate Network on Key load (#87)
Browse files Browse the repository at this point in the history
* Revert "Add ExtrinsicSignatureV5 (#68)"

This reverts commit 461cf42.

* Add Support for network during key loading

* remove debug line

* added subkey + official image

* comment dockerfile

* Load network from env in test function
  • Loading branch information
mikiquantum authored Jul 17, 2020
1 parent 6c55974 commit 40b1b01
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
16 changes: 15 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# Note: We don't use Alpine and its packaged Rust/Cargo because they're too often out of date,
# preventing them from being used to build Substrate/Polkadot.

FROM philipstanislaus/substrate
# First Phase - Load Subkey
FROM parity/subkey:2.0.0-alpha.3 as subkey
RUN subkey --version

## Second Phase - Build context for tests
FROM parity/substrate:v2.0.0-alpha.3

USER root

COPY --from=subkey /usr/local/bin/subkey /usr/local/bin/subkey

# gcc for cgo
RUN apt-get update && apt-get install -y --no-install-recommends \
Expand Down Expand Up @@ -56,6 +65,11 @@ RUN mkdir -p $GOPATH/src/github.com/centrifuge/go-substrate-rpc-client
WORKDIR $GOPATH/src/github.com/centrifuge/go-substrate-rpc-client
COPY . .

# Ensuring Subkey is available
RUN subkey --version

RUN make install

# Reset parent entrypoint
ENTRYPOINT []
CMD ["make", "test-cover"]
17 changes: 14 additions & 3 deletions signature/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,17 @@ var rePubKey = regexp.MustCompile(`Public key \(hex\): 0x([a-f0-9]*)\n`)
var reAddressOld = regexp.MustCompile(`Address \(SS58\): ([a-zA-Z0-9]*)\n`)
var reAddressNew = regexp.MustCompile(`SS58 Address:\s+([a-zA-Z0-9]*)\n`)

func KeyringPairFromSecret(seedOrPhrase string) (KeyringPair, error) {
// KeyringPairFromSecret creates KeyPair based on seed/phrase and network
// Leave network empty for default behavior
func KeyringPairFromSecret(seedOrPhrase, network string) (KeyringPair, error) {
var args []string
if network != "" {
args = []string{"-n", network}
}
args = append(args, []string{"inspect", seedOrPhrase}...)

// use "subkey" command for creation of public key and address
cmd := exec.Command(subkeyCmd, "inspect", seedOrPhrase)
cmd := exec.Command(subkeyCmd, args...)

// execute the command, get the output
out, err := cmd.Output()
Expand Down Expand Up @@ -165,12 +173,15 @@ func Verify(data []byte, sig []byte, privateKeyURI string) (bool, error) {
// LoadKeyringPairFromEnv looks up whether the env variable TEST_PRIV_KEY is set and is not empty and tries to use its
// content as a private phrase, seed or URI to derive a key ring pair. Panics if the private phrase, seed or URI is
// not valid or the keyring pair cannot be derived
// Loads Network from TEST_NETWORK variable
// Leave TEST_NETWORK empty or unset for default
func LoadKeyringPairFromEnv() (kp KeyringPair, ok bool) {
network := os.Getenv("TEST_NETWORK")
priv, ok := os.LookupEnv("TEST_PRIV_KEY")
if !ok || priv == "" {
return kp, false
}
kp, err := KeyringPairFromSecret(priv)
kp, err := KeyringPairFromSecret(priv, network)
if err != nil {
panic(fmt.Errorf("cannot load keyring pair from env or use fallback: %v", err))
}
Expand Down
20 changes: 16 additions & 4 deletions signature/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ var testSecretPhrase = "little orbit comfort eyebrow talk pink flame ridge bring
var testSecretSeed = "0x167d9a020688544ea246b056799d6a771e97c9da057e4d0b87024537f99177bc"
var testPubKey = "0xdc64bef918ddda3126a39a11113767741ddfdf91399f055e1d963f2ae1ec2535"
var testAddressSS58 = "5H3gKVQU7DfNFfNGkgTrD7p715jjg7QXtat8X3UxiSyw7APW"
var testKusamaAddressSS58 = "HZHyokLjagJ1KBiXPGu75B79g1yUnDiLxisuhkvCFCRrWBk"

func TestKeyRingPairFromSecretPhrase(t *testing.T) {
p, err := KeyringPairFromSecret(testSecretPhrase)
p, err := KeyringPairFromSecret(testSecretPhrase, "")
assert.NoError(t, err)

assert.Equal(t, KeyringPair{
Expand All @@ -42,7 +43,7 @@ func TestKeyRingPairFromSecretPhrase(t *testing.T) {
}

func TestKeyringPairFromSecretSeed(t *testing.T) {
p, err := KeyringPairFromSecret(testSecretSeed)
p, err := KeyringPairFromSecret(testSecretSeed, "")
assert.NoError(t, err)

assert.Equal(t, KeyringPair{
Expand All @@ -52,13 +53,24 @@ func TestKeyringPairFromSecretSeed(t *testing.T) {
}, p)
}

func TestKeyringPairFromSecretSeedAndNetwork(t *testing.T) {
p, err := KeyringPairFromSecret(testSecretSeed, "kusama")
assert.NoError(t, err)

assert.Equal(t, KeyringPair{
URI: testSecretSeed,
Address: testKusamaAddressSS58,
PublicKey: types.MustHexDecodeString(testPubKey),
}, p)
}

func TestSignAndVerify(t *testing.T) {
data := []byte("hello!")

sig, err := Sign(data, TestKeyringPairAlice.URI)
assert.NoError(t, err)

ok, err := Verify(data, sig, TestKeyringPairAlice.URI)
ok, err := Verify(data, sig, types.HexEncodeToString(TestKeyringPairAlice.PublicKey))
assert.NoError(t, err)

assert.True(t, ok)
Expand All @@ -72,7 +84,7 @@ func TestSignAndVerifyLong(t *testing.T) {
sig, err := Sign(data, TestKeyringPairAlice.URI)
assert.NoError(t, err)

ok, err := Verify(data, sig, TestKeyringPairAlice.URI)
ok, err := Verify(data, sig, types.HexEncodeToString(TestKeyringPairAlice.PublicKey))
assert.NoError(t, err)

assert.True(t, ok)
Expand Down
2 changes: 1 addition & 1 deletion types/extrinsic_payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestExtrinsicPayload_Sign(t *testing.T) {
// verify sig
b, err := EncodeToBytes(examplaryExtrinsicPayload)
assert.NoError(t, err)
ok, err := signature.Verify(b, sig[:], signature.TestKeyringPairAlice.URI)
ok, err := signature.Verify(b, sig[:], HexEncodeToString(signature.TestKeyringPairAlice.PublicKey))
assert.NoError(t, err)
assert.True(t, ok)
}
2 changes: 1 addition & 1 deletion types/extrinsic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestExtrinsic_Sign(t *testing.T) {
// verify sig
b, err := EncodeToBytes(verifyPayload)
assert.NoError(t, err)
ok, err := signature.Verify(b, extDec.Signature.Signature.AsSr25519[:], signature.TestKeyringPairAlice.URI)
ok, err := signature.Verify(b, extDec.Signature.Signature.AsSr25519[:], HexEncodeToString(signature.TestKeyringPairAlice.PublicKey))
assert.NoError(t, err)
assert.True(t, ok)
}
Expand Down

0 comments on commit 40b1b01

Please sign in to comment.