Skip to content

Commit

Permalink
Added sr25519
Browse files Browse the repository at this point in the history
  • Loading branch information
mj52951 committed Feb 14, 2024
1 parent 05b43ca commit 6f2c8ba
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
69 changes: 69 additions & 0 deletions crypto/sr25519/sr25519.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: LGPL-3.0-only

package sr25519

import (
"bytes"
"crypto/rand"

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

"github.com/ethereum/go-ethereum/common/hexutil"
)

type Keypair struct {
keyringPair *signature.KeyringPair
}

func GenerateKeypair(network uint16) (*Keypair, error) {
data := make([]byte, 32)
_, err := rand.Read(data)
if err != nil {
return nil, err
}
return NewKeypairFromSeed("//"+hexutil.Encode(data), network)
}

func NewKeypairFromSeed(seed string, network uint16) (*Keypair, error) {
//network2, _, _ := subkey.SS58Decode(network)
kp, err := signature.KeyringPairFromSecret(seed, network)
return &Keypair{&kp}, err
}

func NewKeypairFromKRP(pair signature.KeyringPair) *Keypair {
return &Keypair{&pair}
}

// AsKeyringPair returns the underlying KeyringPair
func (kp *Keypair) AsKeyringPair() *signature.KeyringPair {
return kp.keyringPair
}

// Encode uses scale to encode underlying KeyringPair
func (kp *Keypair) Encode() ([]byte, error) {
var buffer = bytes.Buffer{}
err := scale.NewEncoder(&buffer).Encode(kp.keyringPair)
if err != nil {
return buffer.Bytes(), err
}
return buffer.Bytes(), nil
}

// Decode initializes keypair by decoding input as a KeyringPair
func (kp *Keypair) Decode(in []byte) error {
kp.keyringPair = &signature.KeyringPair{}

return scale.NewDecoder(bytes.NewReader(in)).Decode(kp.keyringPair)
}

// Address returns the ss58 formated address
func (kp *Keypair) Address() string {
return kp.keyringPair.Address
}

// PublicKey returns the publickey encoded as a string
func (kp *Keypair) PublicKey() string {
return hexutil.Encode(kp.keyringPair.PublicKey)
}
60 changes: 60 additions & 0 deletions crypto/sr25519/sr25519_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: LGPL-3.0-only

package sr25519

import (
"reflect"
"testing"

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

func TestNewKeypairFromSeed(t *testing.T) {
kp, err := NewKeypairFromSeed("//Alice", 42)
if err != nil {
t.Fatal(err)
}

if kp.PublicKey() == "" || kp.Address() == "" {
t.Fatalf("key is missing data: %#v", kp)
}
}

func TestKeypair_AsKeyringPair(t *testing.T) {

kp, err := NewKeypairFromSeed("//Alice", 42)
if err != nil {
t.Fatal(err)
}

krp := kp.AsKeyringPair()

// TODO: Add expected output from subkey

if !reflect.DeepEqual(&signature.TestKeyringPairAlice, krp) {
t.Fatalf("unexpected result.\n\tGot: %#v\n\texpected: %#v\n", krp, &signature.TestKeyringPairAlice)
}

}

func TestEncodeAndDecodeKeypair(t *testing.T) {
kp, err := NewKeypairFromSeed("//Alice", 42)
if err != nil {
t.Fatal(err)
}

enc, err := kp.Encode()
if err != nil {
t.Fatal(err)
}
res := new(Keypair)
err = res.Decode(enc)
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(res, kp) {
t.Fatalf("Fail: got %#v expected %#v", res, kp)
}
}

0 comments on commit 6f2c8ba

Please sign in to comment.