Skip to content

Commit 5a06239

Browse files
committed
replace private keys with menmonic
1 parent 0fda763 commit 5a06239

File tree

10 files changed

+35
-57
lines changed

10 files changed

+35
-57
lines changed

rmb-sdk-go/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ This connection could be established using a `direct-client`, or an `rmb-peer`.
2121
A process could connect to an `rmb-relay` using a direct client.\
2222
To create a new direct client instance, a process needs to have:
2323

24-
- A valid private key, with an activated account on the Registrar.
24+
- A valid mnemonic, with an activated account on the Registrar.
2525
- The key type of these mnemonics.
2626
- A relay URL that the direct client will connect to.
2727
- A session id. This could be anything, but a twin must only have a unique session id per connection.

rmb-sdk-go/examples/rpc_client/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This is a `Go` example for the `RMB` [rpc client](https://github.com/threefoldte
66

77
To use the example, you needs to:
88

9-
- Set the private key variable to a valid private key, with an activated account on the Registrar.
9+
- Set the mnemonics variable to a valid mnemonic, with an activated account on the Registrar.
1010
- A node id to send the call to
1111

1212
## Usage

rmb-sdk-go/examples/rpc_client/main.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"context"
5-
"encoding/hex"
65
"fmt"
76
"log"
87
"time"
@@ -16,16 +15,11 @@ type version struct {
1615
}
1716

1817
func app() error {
19-
privateKey := "<private key goes here>"
20-
21-
privateKeyBytes, err := hex.DecodeString(privateKey)
22-
if err != nil {
23-
return fmt.Errorf("failed to decode private key: %w", err)
24-
}
18+
mnemonic := "<mnemonics goes here>"
2519

2620
client, err := peer.NewRpcClient(
2721
context.Background(),
28-
privateKeyBytes,
22+
mnemonic,
2923
peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"),
3024
peer.WithRelay("wss://relay.dev.grid.tf"),
3125
peer.WithSession("test-client"),

rmb-sdk-go/peer/examples/peer/main.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"context"
5-
"encoding/hex"
65
"fmt"
76
"math/rand"
87

@@ -15,17 +14,12 @@ import (
1514
var resultsChan = make(chan bool)
1615

1716
func app() error {
18-
privateKey := "<private key here>"
17+
mnemonic := "<mnemonics goes here>"
1918
ctx := context.Background()
2019

21-
privateKeyBytes, err := hex.DecodeString(privateKey)
22-
if err != nil {
23-
return fmt.Errorf("failed to decode private key: %w", err)
24-
}
25-
2620
peer, err := peer.NewPeer(
2721
ctx,
28-
privateKeyBytes,
22+
mnemonic,
2923
relayCallback,
3024
peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"),
3125
peer.WithRelay("wss://relay.dev.grid.tf"),

rmb-sdk-go/peer/examples/peer_pingmany/main.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"context"
5-
"encoding/hex"
65
"encoding/json"
76
"fmt"
87
"net/http"
@@ -17,8 +16,8 @@ import (
1716
)
1817

1918
const (
20-
relayUrl = "ws://localhost:"
21-
privateKey = "<private key>"
19+
relayUrl = "ws://localhost:"
20+
mnemonic = "<mnemonics goes here>"
2221
)
2322

2423
type Node struct {
@@ -55,14 +54,8 @@ func main() {
5554
log.Info().Uint32("twin", env.Source.Twin).Str("version", version).Msg("received response")
5655
}
5756

58-
privateKeyBytes, err := hex.DecodeString(privateKey)
59-
if err != nil {
60-
log.Error().Err(err).Msg("failed to decode private key")
61-
return
62-
}
63-
6457
bus, err := peer.NewPeer(context.Background(),
65-
privateKeyBytes,
58+
mnemonic,
6659
handler,
6760
peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"),
6861
peer.WithKeyType(peer.KeyTypeSr25519),

rmb-sdk-go/peer/examples/router_server/main.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"context"
5-
"encoding/hex"
65
"encoding/json"
76
"errors"
87
"fmt"
@@ -59,20 +58,15 @@ func app() error {
5958
})
6059

6160
// adding a peer for the router
62-
privateKey := "<private key here>"
61+
mnemonic := "<mnemonics goes here>"
6362
ctx := context.Background()
6463

65-
privateKeyBytes, err := hex.DecodeString(privateKey)
66-
if err != nil {
67-
return fmt.Errorf("failed to decode private key: %w", err)
68-
}
69-
7064
// this peer will be a 'calculator' session.
7165
// means other peers on the network need to know that
7266
// session id to use when they are making calls
73-
_, err = peer.NewPeer(
67+
_, err := peer.NewPeer(
7468
ctx,
75-
privateKeyBytes,
69+
mnemonic,
7670
router.Serve,
7771
peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"),
7872
peer.WithRelay("wss://relay.dev.grid.tf"),

rmb-sdk-go/peer/examples/rpc/main.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"context"
5-
"encoding/hex"
65
"fmt"
76
"time"
87

@@ -12,16 +11,11 @@ import (
1211
)
1312

1413
func app() error {
15-
privateKey := "<private key goes here>"
16-
17-
privateKeyBytes, err := hex.DecodeString(privateKey)
18-
if err != nil {
19-
return fmt.Errorf("failed to decode private key: %w", err)
20-
}
14+
mnemonic := "<mnemonics goes here>"
2115

2216
client, err := peer.NewRpcClient(
2317
context.Background(),
24-
privateKeyBytes,
18+
mnemonic,
2519
peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"),
2620
peer.WithKeyType(peer.KeyTypeSr25519),
2721
peer.WithRelay("wss://relay.dev.grid.tf"),

rmb-sdk-go/peer/peer.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ func generateSecureKey(identity substrate.Identity) (*secp256k1.PrivateKey, erro
137137
return priv, nil
138138
}
139139

140-
func getIdentity(keyType string, privateKey []byte) (substrate.Identity, error) {
140+
func getIdentity(keyType string, mnemonics string) (substrate.Identity, error) { //TODO:
141141
var identity substrate.Identity
142142
var err error
143143

144144
switch keyType {
145145
case KeyTypeEd25519:
146-
identity, err = substrate.NewIdentityFromEd25519Key(privateKey)
146+
identity, err = substrate.NewIdentityFromEd25519Phrase(mnemonics)
147147
case KeyTypeSr25519:
148-
// identity, err = substrate.NewIdentityFromSr25519Phrase(privateKeyBytes) //TODO:
148+
identity, err = substrate.NewIdentityFromSr25519Phrase(mnemonics)
149149
default:
150150
return nil, fmt.Errorf("invalid key type %s, should be one of %s or %s ", keyType, KeyTypeEd25519, KeyTypeSr25519)
151151
}
@@ -164,7 +164,7 @@ func getIdentity(keyType string, privateKey []byte) (substrate.Identity, error)
164164
// Call() will panic if called while the directClient's context is canceled.
165165
func NewPeer(
166166
ctx context.Context,
167-
privateKey []byte,
167+
mnemonic string,
168168
handler Handler,
169169
opts ...PeerOpt,
170170
) (*Peer, error) {
@@ -186,7 +186,8 @@ func NewPeer(
186186
if cfg.encoder == nil {
187187
cfg.encoder = encoder.NewJSONEncoder()
188188
}
189-
identity, err := getIdentity(cfg.keyType, privateKey)
189+
190+
identity, err := getIdentity(cfg.keyType, mnemonic)
190191
if err != nil {
191192
return nil, err
192193
}
@@ -233,7 +234,7 @@ func NewPeer(
233234

234235
if !bytes.Equal(twin.E2EKey, publicKey) || twin.Relay == nil || relayURLs[0] != *twin.Relay { // TODO: multiple relays (slice?)
235236
log.Info().Strs("Relay url/s", relayURLs).Msg("twin relay/public key didn't match, updating on registrar ...")
236-
if err = UpdateTwin(twin.ID, privateKey, publicKey, relayURLs, cfg.registrarUrl); err != nil {
237+
if err = UpdateTwin(twin.ID, cfg.registrarUrl, mnemonic, publicKey, relayURLs); err != nil {
237238
return nil, errors.Wrap(err, "could not update twin relay information")
238239
}
239240
}

rmb-sdk-go/peer/rpc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type RpcClient struct {
3333
// it easy to make rpc calls
3434
func NewRpcClient(
3535
ctx context.Context,
36-
privateKey []byte,
36+
mnemonic string,
3737
opts ...PeerOpt) (*RpcClient, error) {
3838

3939
rpc := RpcClient{
@@ -42,7 +42,7 @@ func NewRpcClient(
4242

4343
base, err := NewPeer(
4444
ctx,
45-
privateKey,
45+
mnemonic,
4646
rpc.router,
4747
opts...,
4848
)

rmb-sdk-go/peer/twindb.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package peer
22

33
import (
4-
"crypto/ed25519"
54
"encoding/base64"
65
"encoding/json"
76
"fmt"
@@ -14,6 +13,8 @@ import (
1413

1514
"github.com/pkg/errors"
1615
"github.com/rs/zerolog/log"
16+
"github.com/vedhavyas/go-subkey"
17+
subkeyEd25519 "github.com/vedhavyas/go-subkey/ed25519"
1718
)
1819

1920
var (
@@ -130,14 +131,21 @@ func (t *twinDB) GetByPk(pk []byte) (uint32, error) {
130131
return uint32(registrarTwin.TwinID), nil
131132
}
132133

133-
func UpdateTwin(twinID uint32, privateKey, rmbEncKey []byte, relays []string, registrarUrl string) error {
134+
func UpdateTwin(twinID uint32, registrarUrl, mnemonic string, rmbEncKey []byte, relays []string) error {
134135
client := &http.Client{}
135136

137+
keypair, err := subkey.DeriveKeyPair(subkeyEd25519.Scheme{}, mnemonic)
138+
if err != nil {
139+
return err
140+
}
141+
136142
timestamp := time.Now().Unix()
137143
challenge := []byte(fmt.Sprintf("%d:%v", timestamp, twinID))
138-
signature := ed25519.Sign(privateKey, challenge)
144+
signature, err := keypair.Sign(challenge)
145+
if err != nil {
146+
return err
147+
}
139148

140-
fmt.Printf("rmbEncKey: %v\n", rmbEncKey)
141149
updates := updateTwin{
142150
Relays: relays,
143151
RMBEncKey: base64.StdEncoding.EncodeToString(rmbEncKey),

0 commit comments

Comments
 (0)