forked from ChainSafe/chainbridge-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mj52951
committed
Feb 14, 2024
1 parent
05b43ca
commit 6f2c8ba
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |