Skip to content

Commit

Permalink
Merge pull request #1 from centrifuge/master
Browse files Browse the repository at this point in the history
Update fork with parent repository
  • Loading branch information
LaurentTrk authored Oct 1, 2020
2 parents a5b9dc6 + 305d63b commit e823330
Show file tree
Hide file tree
Showing 31 changed files with 878 additions and 199 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dist: xenial
language: go

go:
- 1.11.x
- 1.14.x

# Only clone the most recent commit.
git:
Expand Down
20 changes: 10 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# preventing them from being used to build Substrate/Polkadot.

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

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

USER root

Expand All @@ -22,20 +22,20 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
git-core \
&& rm -rf /var/lib/apt/lists/*

ENV GOLANG_VERSION 1.12.9
ENV GOLANG_VERSION 1.14.9

RUN set -eux; \
\
# this "case" statement is generated via "update.sh"
dpkgArch="$(dpkg --print-architecture)"; \
case "${dpkgArch##*-}" in \
amd64) goRelArch='linux-amd64'; goRelSha256='ac2a6efcc1f5ec8bdc0db0a988bb1d301d64b6d61b7e8d9e42f662fbb75a2b9b' ;; \
armhf) goRelArch='linux-armv6l'; goRelSha256='0d9be0efa9cd296d6f8ab47de45356ba45cb82102bc5df2614f7af52e3fb5842' ;; \
arm64) goRelArch='linux-arm64'; goRelSha256='3606dc6ce8b4a5faad81d7365714a86b3162df041a32f44568418c9efbd7f646' ;; \
i386) goRelArch='linux-386'; goRelSha256='c40824a3e6c948b8ecad8fe9095b620c488b3d8d6694bdd48084a4798db4799a' ;; \
ppc64el) goRelArch='linux-ppc64le'; goRelSha256='2e74c071c6a68446c9b00c1717ceeb59a826025b9202b3b0efed4f128e868b30' ;; \
s390x) goRelArch='linux-s390x'; goRelSha256='2aac6de8e83b253b8413781a2f9a0733384d859cff1b89a2ad0d13814541c336' ;; \
*) goRelArch='src'; goRelSha256='ab0e56ed9c4732a653ed22e232652709afbf573e710f56a07f7fdeca578d62fc'; \
amd64) goRelArch='linux-amd64'; goRelSha256='f0d26ff572c72c9823ae752d3c81819a81a60c753201f51f89637482531c110a' ;; \
armhf) goRelArch='linux-armv6l'; goRelSha256='e85dc09608dc9fc245ebc5daea0826898ac0eb0d48ed24e2300427850876c442' ;; \
arm64) goRelArch='linux-arm64'; goRelSha256='65e6cef5c474a3514e754f6a7987c49388bb85a7b370370c1318087ac35427fa' ;; \
i386) goRelArch='linux-386'; goRelSha256='14982ef997ec323023a11cffe1a4afc3aacd1b5edebf70a00e17b67f888d8cdb' ;; \
ppc64el) goRelArch='linux-ppc64le'; goRelSha256='5880a37faf93b2396edc3ff231e0f8df14d0520505cc13d01116e24d7d1d0147' ;; \
s390x) goRelArch='linux-s390x'; goRelSha256='381fc24aff153c4affcb00f4547683212157af29b8f9e3de5952d78ac35f5a0f' ;; \
*) goRelArch='src'; goRelSha256='c687c848cc09bcabf2b5e534c3fc4259abebbfc9014dd05a1a2dc6106f404554'; \
echo >&2; echo >&2 "warning: current architecture ($dpkgArch) does not have a corresponding Go binary release; will be building from source"; echo >&2 ;; \
esac; \
\
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ run-substrate-docker: ## runs the Substrate 1.0 Default Docker image, this can
docker run -p 9933:9933 -p 9944:9944 -p 30333:30333 parity/substrate:latest-v1.0 --dev --rpc-external --ws-external

run-substrate-docker-v2: ## runs the Substrate 2.0 Default Docker image, this can be used to run the tests
docker run -p 9933:9933 -p 9944:9944 -p 30333:30333 parity/substrate:v2.0.0-alpha.3 --dev --rpc-external --ws-external
docker run -p 9933:9933 -p 9944:9944 -p 30333:30333 parity/substrate:v2.0.0-rc6 --dev --rpc-external --ws-external

help: ## shows this help
@sed -ne '/@sed/!s/## //p' $(MAKEFILE_LIST)
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ This client is modelled after [polkadot-js/api](https://github.com/polkadot-js/a

This package is feature complete, but it is relatively new and might still contain bugs. We advice to use it with caution in production. It comes without any warranties, please refer to LICENCE for details.

## Requirements
Substrate Key Management requires `subkey` to be present in your PATH: https://substrate.dev/docs/en/knowledgebase/integrate/subkey

The `subkey` recommended version: https://github.com/paritytech/substrate/releases/tag/v2.0.0-rc6

## Documentation & Usage Examples

Please refer to https://godoc.org/github.com/centrifuge/go-substrate-rpc-client
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.7"
services:
substrate:
image: parity/substrate:v2.0.0-alpha.3
image: parity/substrate:v2.0.0-rc6
ports:
- 9933:9933
- 9944:9944
Expand Down
6 changes: 3 additions & 3 deletions gethrpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@ func (h *handler) handleResponse(msg *jsonrpcMessage) {
op.err = msg.Error
return
}
var subid int
if op.err = json.Unmarshal(msg.Result, &subid); op.err == nil {
op.sub.subid = fmt.Sprintf("%v", subid)
var subID string
if op.err = json.Unmarshal(msg.Result, &subID); op.err == nil {
op.sub.subid = subID
go op.sub.start()
h.clientSubs[op.sub.subid] = op.sub
}
Expand Down
2 changes: 1 addition & 1 deletion gethrpc/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const (
var null = json.RawMessage("null")

type subscriptionResult struct {
ID int `json:"subscription"`
ID string `json:"subscription"`
Result json.RawMessage `json:"result,omitempty"`
}

Expand Down
2 changes: 1 addition & 1 deletion gethrpc/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (n *Notifier) activate() error {
}

func (n *Notifier) send(sub *Subscription, data json.RawMessage) error {
params, _ := json.Marshal(&subscriptionResult{ID: int(sub.ID), Result: data})
params, _ := json.Marshal(&subscriptionResult{ID: string(sub.ID), Result: data})
ctx := context.Background()
return n.h.conn.Write(ctx, &jsonrpcMessage{
Version: vsn,
Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Example_simpleConnect() {

fmt.Printf("You are connected to chain %v using %v v%v\n", chain, nodeName, nodeVersion)

// Output: You are connected to chain Development using Substrate Node v2.0.0-alpha.3
// Output: You are connected to chain Development using Substrate Node v2.0.0-rc6-be8bb186-x86_64-linux-gnu
}

func Example_listenToNewBlocks() {
Expand Down Expand Up @@ -361,7 +361,7 @@ func Example_displaySystemEvents() {
}
for _, e := range events.Staking_Reward {
fmt.Printf("\tStaking:Reward:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%v\n", e.Balance)
fmt.Printf("\t\t%v\n", e.Amount)
}
for _, e := range events.Staking_Slash {
fmt.Printf("\tStaking:Slash:: (phase=%#v)\n", e.Phase)
Expand Down
47 changes: 22 additions & 25 deletions signature/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package signature

import (
"encoding/hex"
"encoding/json"
"fmt"
"os"
"os/exec"
"regexp"
"strings"

"golang.org/x/crypto/blake2b"
Expand All @@ -38,18 +38,23 @@ type KeyringPair struct {
PublicKey []byte
}

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`)
// InspectKeyInfo type is used as target from `subkey` inspect JSON output
type InspectKeyInfo struct {
AccountID string `json:"accountId"`
PublicKey string `json:"publicKey"`
SecretPhrase string `json:"secretPhrase"`
SecretSeed string `json:"secretSeed"`
SS58Address string `json:"ss58Address"`
}

// 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 = []string{"--network", network}
}
args = append(args, []string{"inspect", seedOrPhrase}...)
args = append([]string{"inspect", "--output-type", "Json", seedOrPhrase}, args...)

// use "subkey" command for creation of public key and address
cmd := exec.Command(subkeyCmd, args...)
Expand All @@ -64,29 +69,21 @@ func KeyringPairFromSecret(seedOrPhrase, network string) (KeyringPair, error) {
return KeyringPair{}, fmt.Errorf("failed to generate keyring pair from secret: invalid phrase/URI given")
}

// find the pub key
resPk := rePubKey.FindStringSubmatch(string(out))
if len(resPk) != 2 {
return KeyringPair{}, fmt.Errorf("failed to generate keyring pair from secret, pubkey not found in output: %v", resPk)
}
pk, err := hex.DecodeString(resPk[1])
var keyInfo InspectKeyInfo
err = json.Unmarshal(out, &keyInfo)
if err != nil {
return KeyringPair{}, fmt.Errorf("failed to generate keyring pair from secret, could not hex decode pubkey: %v",
resPk[1])
return KeyringPair{}, fmt.Errorf("failed to deserialize key info JSON output: %v", err.Error())
}

// find the address
addr := reAddressNew.FindStringSubmatch(string(out))
if len(addr) != 2 {
addr = reAddressOld.FindStringSubmatch(string(out))
}
if len(addr) != 2 {
return KeyringPair{}, fmt.Errorf("failed to generate keyring pair from secret, address not found in output: %v", addr)
pk, err := hex.DecodeString(strings.Replace(keyInfo.PublicKey, "0x", "", 1))
if err != nil {
return KeyringPair{}, fmt.Errorf("failed to generate keyring pair from secret, could not hex decode pubkey: "+
"%v with error: %v", keyInfo.PublicKey, err.Error())
}

return KeyringPair{
URI: seedOrPhrase,
Address: addr[1],
Address: keyInfo.SS58Address,
PublicKey: pk,
}, nil
}
Expand All @@ -107,7 +104,7 @@ func Sign(data []byte, privateKeyURI string) ([]byte, error) {
}

// use "subkey" command for signature
cmd := exec.Command(subkeyCmd, "sign", privateKeyURI, "--hex")
cmd := exec.Command(subkeyCmd, "sign", "--suri", privateKeyURI, "--hex")

// data to stdin
dataHex := hex.EncodeToString(data)
Expand Down Expand Up @@ -146,13 +143,13 @@ func Verify(data []byte, sig []byte, privateKeyURI string) (bool, error) {
sigHex := hex.EncodeToString(sig)

// use "subkey" command for signature
cmd := exec.Command(subkeyCmd, "verify", sigHex, privateKeyURI, "--hex")
cmd := exec.Command(subkeyCmd, "verify", "--hex", sigHex, privateKeyURI)

// data to stdin
dataHex := hex.EncodeToString(data)
cmd.Stdin = strings.NewReader(dataHex)

// log.Printf("echo -n \"%v\" | %v verify %v %v --hex", dataHex, subkeyCmd, sigHex, privateKeyURI)
//log.Printf("echo -n \"%v\" | %v verify --hex %v %v", dataHex, subkeyCmd, sigHex, privateKeyURI)

// execute the command, get the output
out, err := cmd.Output()
Expand Down
3 changes: 1 addition & 2 deletions teste2e/author_submit_and_watch_extrinsic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,9 @@ func TestAuthor_SubmitAndWatchExtrinsic(t *testing.T) {
Nonce: types.NewUCompactFromUInt(uint64(nonce)),
SpecVersion: rv.SpecVersion,
Tip: types.NewUCompactFromUInt(0),
TransactionVersion: 1,
}



err = ext.Sign(from, o)
if err != nil {
panic(err)
Expand Down
1 change: 1 addition & 0 deletions teste2e/author_submit_extrinsic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func TestChain_SubmitExtrinsic(t *testing.T) {
Nonce: types.NewUCompactFromUInt(uint64(nonce + i)),
SpecVersion: rv.SpecVersion,
Tip: types.NewUCompactFromUInt(0),
TransactionVersion: 1,
}

extI := ext
Expand Down
1 change: 1 addition & 0 deletions teste2e/state_subscribe_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func TestState_SubscribeStorage_Events(t *testing.T) {
continue
}

fmt.Printf("%s\n", chng.StorageKey.Hex())
fmt.Printf("%#x\n", chng.StorageData)

events := types.EventRecords{}
Expand Down
32 changes: 32 additions & 0 deletions types/balance_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package types

import (
"fmt"

"github.com/centrifuge/go-substrate-rpc-client/scale"
)

type BalanceStatus byte

const (
// Funds are free, as corresponding to `free` item in Balances.
Free BalanceStatus = 0
// Funds are reserved, as corresponding to `reserved` item in Balances.
Reserved BalanceStatus = 1
)

func (bs *BalanceStatus) Decode(decoder scale.Decoder) error {
b, err := decoder.ReadOneByte()
vb := BalanceStatus(b)
switch vb {
case Free, Reserved:
*bs = vb
default:
return fmt.Errorf("unknown BalanceStatus enum: %v", vb)
}
return err
}

func (bs BalanceStatus) Encode(encoder scale.Encoder) error {
return encoder.PushByte(byte(bs))
}
32 changes: 32 additions & 0 deletions types/balance_status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package types

import (
"bytes"
"testing"

"github.com/centrifuge/go-substrate-rpc-client/scale"
"github.com/stretchr/testify/assert"
)

func TestBalanceStatusEncodeDecode(t *testing.T) {
// encode
bs := Reserved
var buf bytes.Buffer
encoder := scale.NewEncoder(&buf)
assert.NoError(t, encoder.Encode(bs))
assert.Equal(t, buf.Len(), 1)
assert.Equal(t, buf.Bytes(), []byte{1})

//decode
decoder := scale.NewDecoder(bytes.NewReader(buf.Bytes()))
bs0 := BalanceStatus(0)
err := decoder.Decode(&bs0)
assert.NoError(t, err)
assert.Equal(t, bs0, Reserved)

//decode error
decoder = scale.NewDecoder(bytes.NewReader([]byte{5}))
bs0 = BalanceStatus(0)
err = decoder.Decode(&bs0)
assert.Error(t, err)
}
34 changes: 34 additions & 0 deletions types/election_compute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package types

import (
"fmt"

"github.com/centrifuge/go-substrate-rpc-client/scale"
)

type ElectionCompute byte

const (
// Result was forcefully computed on chain at the end of the session.
OnChain ElectionCompute = 0
// Result was submitted and accepted to the chain via a signed transaction.
Signed ElectionCompute = 1
// Result was submitted and accepted to the chain via an unsigned transaction (by an authority).
Unsigned ElectionCompute = 2
)

func (ec *ElectionCompute) Decode(decoder scale.Decoder) error {
b, err := decoder.ReadOneByte()
vb := ElectionCompute(b)
switch vb {
case OnChain, Signed, Unsigned:
*ec = vb
default:
return fmt.Errorf("unknown ElectionCompute enum: %v", vb)
}
return err
}

func (ec ElectionCompute) Encode(encoder scale.Encoder) error {
return encoder.PushByte(byte(ec))
}
32 changes: 32 additions & 0 deletions types/election_compute_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package types

import (
"bytes"
"testing"

"github.com/centrifuge/go-substrate-rpc-client/scale"
"github.com/stretchr/testify/assert"
)

func TestElectionComputeEncodeDecode(t *testing.T) {
// encode
ec := OnChain
var buf bytes.Buffer
encoder := scale.NewEncoder(&buf)
assert.NoError(t, encoder.Encode(ec))
assert.Equal(t, buf.Len(), 1)
assert.Equal(t, buf.Bytes(), []byte{0})

//decode
decoder := scale.NewDecoder(bytes.NewReader(buf.Bytes()))
ec0 := ElectionCompute(0)
err := decoder.Decode(&ec0)
assert.NoError(t, err)
assert.Equal(t, ec0, OnChain)

//decode error
decoder = scale.NewDecoder(bytes.NewReader([]byte{5}))
ec0 = ElectionCompute(0)
err = decoder.Decode(&ec0)
assert.Error(t, err)
}
Loading

0 comments on commit e823330

Please sign in to comment.