From 0fda76363540ba4f6a98d6908a0692cc047acf2b Mon Sep 17 00:00:00 2001 From: rawdaGastan Date: Thu, 27 Feb 2025 14:35:18 +0200 Subject: [PATCH 1/5] replace chain with registrar in rmb client --- rmb-sdk-go/README.md | 29 +--- rmb-sdk-go/examples/rpc_client/README.md | 2 +- rmb-sdk-go/examples/rpc_client/main.go | 19 ++- rmb-sdk-go/peer/examples/peer/README.md | 2 +- rmb-sdk-go/peer/examples/peer/main.go | 15 +- .../peer/examples/peer_pingmany/main.go | 20 +-- .../peer/examples/router_server/README.md | 2 +- .../peer/examples/router_server/main.go | 17 ++- rmb-sdk-go/peer/examples/rpc/README.md | 2 +- rmb-sdk-go/peer/examples/rpc/main.go | 14 +- rmb-sdk-go/peer/peer.go | 54 +++---- rmb-sdk-go/peer/rpc.go | 7 +- rmb-sdk-go/peer/twindb.go | 143 +++++++++++++++--- rmb-sdk-go/wiki/using_rmb_peer.md | 2 +- 14 files changed, 211 insertions(+), 117 deletions(-) diff --git a/rmb-sdk-go/README.md b/rmb-sdk-go/README.md index f98c37c..9cff723 100644 --- a/rmb-sdk-go/README.md +++ b/rmb-sdk-go/README.md @@ -21,35 +21,8 @@ This connection could be established using a `direct-client`, or an `rmb-peer`. A process could connect to an `rmb-relay` using a direct client.\ To create a new direct client instance, a process needs to have: -- A valid mnemonics, with an activated account on the TFChain. +- A valid private key, with an activated account on the Registrar. - The key type of these mnemonics. - A relay URL that the direct client will connect to. - A session id. This could be anything, but a twin must only have a unique session id per connection. - A substrate connection. - -#### **Example** - -Creating a new direct client instance: - -```Go -subManager := substrate.NewManager("wss://tfchain.dev.grid.tf/ws") -sub, err := subManager.Substrate() -if err != nil { - return fmt.Errorf("failed to connect to substrate: %w", err) -} - -defer sub.Close() -client, err := direct.NewRpcClient(direct.KeyTypeSr25519, mnemonics, "wss://relay.dev.grid.tf", "test-client", sub, false) -if err != nil { - return fmt.Errorf("failed to create direct client: %w", err) -} -``` - -Assuming there is a remote calculator process that could add two integers, an rmb call using the direct client would look like this: - -```Go -x := 1 -y := 2 -var sum int -err := client.Call(ctx, destinationTwinID, "calculator.add", []int{x, y}, &sum) -``` diff --git a/rmb-sdk-go/examples/rpc_client/README.md b/rmb-sdk-go/examples/rpc_client/README.md index 3c14c25..e4253aa 100644 --- a/rmb-sdk-go/examples/rpc_client/README.md +++ b/rmb-sdk-go/examples/rpc_client/README.md @@ -6,7 +6,7 @@ This is a `Go` example for the `RMB` [rpc client](https://github.com/threefoldte To use the example, you needs to: -- Set the mnemonics variable to a valid mnemonics, with an activated account on the TFChain. +- Set the private key variable to a valid private key, with an activated account on the Registrar. - A node id to send the call to ## Usage diff --git a/rmb-sdk-go/examples/rpc_client/main.go b/rmb-sdk-go/examples/rpc_client/main.go index c9b97cd..301e35a 100644 --- a/rmb-sdk-go/examples/rpc_client/main.go +++ b/rmb-sdk-go/examples/rpc_client/main.go @@ -2,11 +2,11 @@ package main import ( "context" + "encoding/hex" "fmt" "log" "time" - substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go" "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer" ) @@ -16,13 +16,20 @@ type version struct { } func app() error { - mnemonics := "" - subNodeURL := "wss://tfchain.dev.grid.tf/ws" - relayURL := "wss://relay.dev.grid.tf" + privateKey := "" - subManager := substrate.NewManager(subNodeURL) + privateKeyBytes, err := hex.DecodeString(privateKey) + if err != nil { + return fmt.Errorf("failed to decode private key: %w", err) + } - client, err := peer.NewRpcClient(context.Background(), mnemonics, subManager, peer.WithRelay(relayURL), peer.WithSession("test-client")) + client, err := peer.NewRpcClient( + context.Background(), + privateKeyBytes, + peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"), + peer.WithRelay("wss://relay.dev.grid.tf"), + peer.WithSession("test-client"), + ) if err != nil { return fmt.Errorf("failed to create direct client: %w", err) } diff --git a/rmb-sdk-go/peer/examples/peer/README.md b/rmb-sdk-go/peer/examples/peer/README.md index eb707d0..ac8df60 100644 --- a/rmb-sdk-go/peer/examples/peer/README.md +++ b/rmb-sdk-go/peer/examples/peer/README.md @@ -6,7 +6,7 @@ This is a `Go` example for the `RMB` [direct client](https://github.com/threefol To use the example, you needs to: -- Set the mnemonics variable to a valid mnemonics, with an activated account on the TFChain. +- Set the mnemonics variable to a valid mnemonics, with an activated account on the Registrar. - Set dst to the twinId of a remote calculator process that could add two integers ## Usage diff --git a/rmb-sdk-go/peer/examples/peer/main.go b/rmb-sdk-go/peer/examples/peer/main.go index e551ef8..2493a00 100644 --- a/rmb-sdk-go/peer/examples/peer/main.go +++ b/rmb-sdk-go/peer/examples/peer/main.go @@ -2,12 +2,12 @@ package main import ( "context" + "encoding/hex" "fmt" "math/rand" "github.com/google/uuid" "github.com/rs/zerolog/log" - substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go" "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer" "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer/types" ) @@ -15,18 +15,23 @@ import ( var resultsChan = make(chan bool) func app() error { - mnemonics := "" - subManager := substrate.NewManager("wss://tfchain.dev.grid.tf/ws") + privateKey := "" ctx := context.Background() + privateKeyBytes, err := hex.DecodeString(privateKey) + if err != nil { + return fmt.Errorf("failed to decode private key: %w", err) + } + peer, err := peer.NewPeer( ctx, - mnemonics, - subManager, + privateKeyBytes, relayCallback, + peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"), peer.WithRelay("wss://relay.dev.grid.tf"), peer.WithSession("test-client"), peer.WithInMemoryExpiration(6*60*60), // six hours + peer.WithKeyType(peer.KeyTypeEd25519), ) if err != nil { diff --git a/rmb-sdk-go/peer/examples/peer_pingmany/main.go b/rmb-sdk-go/peer/examples/peer_pingmany/main.go index c2290c2..b565f8c 100644 --- a/rmb-sdk-go/peer/examples/peer_pingmany/main.go +++ b/rmb-sdk-go/peer/examples/peer_pingmany/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/hex" "encoding/json" "fmt" "net/http" @@ -10,17 +11,14 @@ import ( "time" "github.com/rs/zerolog/log" - substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go" "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer" "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer/types" - // "rmbClient/peer" ) const ( - chainUrl = "wss://tfchain.grid.tf/" - relayUrl = "ws://localhost:" - mnemonic = "" + relayUrl = "ws://localhost:" + privateKey = "" ) type Node struct { @@ -32,8 +30,6 @@ var static = []uint32{7, 9, 10, 13, 14, 16, 22, 23, 24, 27, 29, 35, 46, 47, 69, const use_static = true func main() { - subMan := substrate.NewManager(chainUrl) - count := 500 var wg sync.WaitGroup wg.Add(count) @@ -59,10 +55,16 @@ func main() { log.Info().Uint32("twin", env.Source.Twin).Str("version", version).Msg("received response") } + privateKeyBytes, err := hex.DecodeString(privateKey) + if err != nil { + log.Error().Err(err).Msg("failed to decode private key") + return + } + bus, err := peer.NewPeer(context.Background(), - mnemonic, - subMan, + privateKeyBytes, handler, + peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"), peer.WithKeyType(peer.KeyTypeSr25519), peer.WithSession("rmb-playground999"), peer.WithInMemoryExpiration(10*60*60), // in seconds that's 10 hours diff --git a/rmb-sdk-go/peer/examples/router_server/README.md b/rmb-sdk-go/peer/examples/router_server/README.md index d7c10e4..c52b406 100644 --- a/rmb-sdk-go/peer/examples/router_server/README.md +++ b/rmb-sdk-go/peer/examples/router_server/README.md @@ -6,7 +6,7 @@ This is a `Go` example for the `RMB` [peer router using direct client](https://g To use the example, you needs to: -- Set the mnemonics variable to a valid mnemonics for client peer and server, with an activated account on the TFChain. +- Set the mnemonics variable to a valid mnemonics for client peer and server, with an activated account on the Registrar. - set the client peer destination twin and session with the ones of the created peer router. - make sure you are running the server before the client peer. diff --git a/rmb-sdk-go/peer/examples/router_server/main.go b/rmb-sdk-go/peer/examples/router_server/main.go index 1677735..12a7bb5 100644 --- a/rmb-sdk-go/peer/examples/router_server/main.go +++ b/rmb-sdk-go/peer/examples/router_server/main.go @@ -2,12 +2,12 @@ package main import ( "context" + "encoding/hex" "encoding/json" "errors" "fmt" "os" - substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go" "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go" "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer" ) @@ -59,20 +59,25 @@ func app() error { }) // adding a peer for the router - mnemonics := "" - subManager := substrate.NewManager("wss://tfchain.dev.grid.tf/ws") + privateKey := "" ctx := context.Background() + privateKeyBytes, err := hex.DecodeString(privateKey) + if err != nil { + return fmt.Errorf("failed to decode private key: %w", err) + } + // this peer will be a 'calculator' session. // means other peers on the network need to know that // session id to use when they are making calls - _, err := peer.NewPeer( + _, err = peer.NewPeer( ctx, - mnemonics, - subManager, + privateKeyBytes, router.Serve, + peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"), peer.WithRelay("wss://relay.dev.grid.tf"), peer.WithSession("calculator"), + peer.WithKeyType(peer.KeyTypeEd25519), ) if err != nil { diff --git a/rmb-sdk-go/peer/examples/rpc/README.md b/rmb-sdk-go/peer/examples/rpc/README.md index a65b0f8..79ced1f 100644 --- a/rmb-sdk-go/peer/examples/rpc/README.md +++ b/rmb-sdk-go/peer/examples/rpc/README.md @@ -6,7 +6,7 @@ This is a `Go` example for the `RMB` [rpc client](https://github.com/threefoldte To use the example, you needs to: -- Set the mnemonics variable to a valid mnemonics, with an activated account on the TFChain. +- Set the mnemonics variable to a valid mnemonics, with an activated account on the Registrar. - A twinId of a remote calculator process that could add two integers ## Usage diff --git a/rmb-sdk-go/peer/examples/rpc/main.go b/rmb-sdk-go/peer/examples/rpc/main.go index 73c768f..5222fce 100644 --- a/rmb-sdk-go/peer/examples/rpc/main.go +++ b/rmb-sdk-go/peer/examples/rpc/main.go @@ -2,23 +2,27 @@ package main import ( "context" + "encoding/hex" "fmt" "time" "log" - substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go" "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer" ) func app() error { - mnemonics := "" - subManager := substrate.NewManager("wss://tfchain.dev.grid.tf/ws") + privateKey := "" + + privateKeyBytes, err := hex.DecodeString(privateKey) + if err != nil { + return fmt.Errorf("failed to decode private key: %w", err) + } client, err := peer.NewRpcClient( context.Background(), - mnemonics, - subManager, + privateKeyBytes, + peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"), peer.WithKeyType(peer.KeyTypeSr25519), peer.WithRelay("wss://relay.dev.grid.tf"), peer.WithSession("test-client"), diff --git a/rmb-sdk-go/peer/peer.go b/rmb-sdk-go/peer/peer.go index dc05f12..c83714b 100644 --- a/rmb-sdk-go/peer/peer.go +++ b/rmb-sdk-go/peer/peer.go @@ -35,10 +35,11 @@ const ( // messages. An error can be non-nil error if verification or decryption failed type Handler func(ctx context.Context, peer *Peer, env *types.Envelope, err error) -type cacheFactory = func(inner TwinDB, chainURL string) (TwinDB, error) +type cacheFactory = func(inner TwinDB) (TwinDB, error) type peerCfg struct { relayURLs []string + registrarUrl string keyType string session string enableEncryption bool @@ -69,6 +70,13 @@ func WithRelay(urls ...string) PeerOpt { } } +// WithRelay set up the relay url, default is mainnet relay +func WithRegistrarUrl(url string) PeerOpt { + return func(p *peerCfg) { + p.registrarUrl = url + } +} + // WithKeyType set up the mnemonic key type, default is Sr25519 func WithKeyType(keyType string) PeerOpt { return func(p *peerCfg) { @@ -91,8 +99,8 @@ func WithEncoder(encoder encoder.Encoder) PeerOpt { // if ttl == 0, twins are cached forever func WithTmpCacheExpiration(ttl uint64) PeerOpt { return func(pc *peerCfg) { - pc.cacheFactory = func(inner TwinDB, chainURL string) (TwinDB, error) { - return newTmpCache(ttl, inner, chainURL) + pc.cacheFactory = func(inner TwinDB) (TwinDB, error) { + return newTmpCache(ttl, inner) } } } @@ -100,7 +108,7 @@ func WithTmpCacheExpiration(ttl uint64) PeerOpt { // if ttl == 0 twins are cached forever func WithInMemoryExpiration(ttl uint64) PeerOpt { return func(pc *peerCfg) { - pc.cacheFactory = func(inner TwinDB, chainURL string) (TwinDB, error) { + pc.cacheFactory = func(inner TwinDB) (TwinDB, error) { return newInMemoryCache(inner, ttl), nil } } @@ -129,17 +137,17 @@ func generateSecureKey(identity substrate.Identity) (*secp256k1.PrivateKey, erro return priv, nil } -func getIdentity(keytype string, mnemonics string) (substrate.Identity, error) { +func getIdentity(keyType string, privateKey []byte) (substrate.Identity, error) { var identity substrate.Identity var err error - switch keytype { + switch keyType { case KeyTypeEd25519: - identity, err = substrate.NewIdentityFromEd25519Phrase(mnemonics) + identity, err = substrate.NewIdentityFromEd25519Key(privateKey) case KeyTypeSr25519: - identity, err = substrate.NewIdentityFromSr25519Phrase(mnemonics) + // identity, err = substrate.NewIdentityFromSr25519Phrase(privateKeyBytes) //TODO: default: - return nil, fmt.Errorf("invalid key type %s, should be one of %s or %s ", keytype, KeyTypeEd25519, KeyTypeSr25519) + return nil, fmt.Errorf("invalid key type %s, should be one of %s or %s ", keyType, KeyTypeEd25519, KeyTypeSr25519) } if err != nil { @@ -156,17 +164,17 @@ func getIdentity(keytype string, mnemonics string) (substrate.Identity, error) { // Call() will panic if called while the directClient's context is canceled. func NewPeer( ctx context.Context, - mnemonics string, - subManager substrate.Manager, + privateKey []byte, handler Handler, opts ...PeerOpt, ) (*Peer, error) { cfg := &peerCfg{ relayURLs: []string{"wss://relay.grid.tf"}, + registrarUrl: "https://registrar.prod4.grid.tf", session: "", enableEncryption: true, keyType: KeyTypeSr25519, - cacheFactory: func(inner TwinDB, _ string) (TwinDB, error) { + cacheFactory: func(inner TwinDB) (TwinDB, error) { return newInMemoryCache(inner, 0), nil }, } @@ -178,22 +186,12 @@ func NewPeer( if cfg.encoder == nil { cfg.encoder = encoder.NewJSONEncoder() } - identity, err := getIdentity(cfg.keyType, mnemonics) - if err != nil { - return nil, err - } - - subConn, err := subManager.Substrate() + identity, err := getIdentity(cfg.keyType, privateKey) if err != nil { return nil, err } - api, _, err := subConn.GetClient() - if err != nil { - return nil, err - } - - twinDB, err := cfg.cacheFactory(NewTwinDB(subConn), api.Client.URL()) + twinDB, err := cfg.cacheFactory(NewTwinDB(cfg.registrarUrl)) if err != nil { return nil, err } @@ -233,11 +231,9 @@ func NewPeer( sort.Slice(relayURLs, func(i, j int) bool { return strings.ToLower(relayURLs[i]) < strings.ToLower(relayURLs[j]) }) relayURLs = slices.Compact(relayURLs) - joinURLs := strings.Join(relayURLs, "_") - - if !bytes.Equal(twin.E2EKey, publicKey) || twin.Relay == nil || joinURLs != *twin.Relay { - log.Info().Str("Relay url/s", joinURLs).Msg("twin relay/public key didn't match, updating on chain ...") - if _, err = subConn.UpdateTwin(identity, joinURLs, publicKey); err != nil { + if !bytes.Equal(twin.E2EKey, publicKey) || twin.Relay == nil || relayURLs[0] != *twin.Relay { // TODO: multiple relays (slice?) + log.Info().Strs("Relay url/s", relayURLs).Msg("twin relay/public key didn't match, updating on registrar ...") + if err = UpdateTwin(twin.ID, privateKey, publicKey, relayURLs, cfg.registrarUrl); err != nil { return nil, errors.Wrap(err, "could not update twin relay information") } } diff --git a/rmb-sdk-go/peer/rpc.go b/rmb-sdk-go/peer/rpc.go index 206d6c4..1288354 100644 --- a/rmb-sdk-go/peer/rpc.go +++ b/rmb-sdk-go/peer/rpc.go @@ -8,7 +8,6 @@ import ( "sync" "github.com/google/uuid" - substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go" "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go" "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer/types" ) @@ -34,8 +33,7 @@ type RpcClient struct { // it easy to make rpc calls func NewRpcClient( ctx context.Context, - mnemonics string, - subManager substrate.Manager, + privateKey []byte, opts ...PeerOpt) (*RpcClient, error) { rpc := RpcClient{ @@ -44,8 +42,7 @@ func NewRpcClient( base, err := NewPeer( ctx, - mnemonics, - subManager, + privateKey, rpc.router, opts..., ) diff --git a/rmb-sdk-go/peer/twindb.go b/rmb-sdk-go/peer/twindb.go index 7438ca4..4cef7bd 100644 --- a/rmb-sdk-go/peer/twindb.go +++ b/rmb-sdk-go/peer/twindb.go @@ -1,17 +1,19 @@ package peer import ( + "crypto/ed25519" + "encoding/base64" "encoding/json" "fmt" - "net/url" + "net/http" "os" "path/filepath" + "strings" "sync" "time" "github.com/pkg/errors" "github.com/rs/zerolog/log" - substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go" ) var ( @@ -28,48 +30,155 @@ type TwinDB interface { type Twin struct { ID uint32 PublicKey []byte - Relay *string + Relay *string // TODO: multiple relays (slice?) E2EKey []byte Timestamp uint64 } +type RegistrarTwin struct { + TwinID uint64 `json:"twin_id"` + Relays []string `json:"relays"` + RMBEncKey string `json:"rmb_enc_key"` + PublicKey string `json:"public_key"` +} + type twinDB struct { - subConn *substrate.Substrate + httpClient *http.Client + registrarUrl string } // NewTwinDB creates a new twinDBImpl instance, with a non expiring cache. -func NewTwinDB(subConn *substrate.Substrate) TwinDB { +func NewTwinDB(registrarUrl string) TwinDB { return &twinDB{ - subConn: subConn, + httpClient: &http.Client{}, + registrarUrl: registrarUrl, } } +type updateTwin struct { + Relays []string `json:"relays"` + RMBEncKey string `json:"rmb_enc_key"` +} + // GetTwin gets Twin from cache if present. if not, gets it from substrate client and caches it. func (t *twinDB) Get(id uint32) (Twin, error) { - substrateTwin, err := t.subConn.GetTwin(id) + req, _ := http.NewRequest( + "GET", + fmt.Sprintf("%s/v1/accounts?twin_id=%v", t.registrarUrl, id), + nil, + ) + + resp, err := t.httpClient.Do(req) if err != nil { return Twin{}, errors.Wrapf(err, "could not get twin with id %d", id) } + defer resp.Body.Close() + + var registrarTwin RegistrarTwin + if err = json.NewDecoder(resp.Body).Decode(®istrarTwin); err != nil { + return Twin{}, err + } var relay *string - if substrateTwin.Relay.HasValue { - relay = &substrateTwin.Relay.AsValue + if len(registrarTwin.Relays) > 0 { + relay = ®istrarTwin.Relays[0] // TODO: will relays be a slice??? + } + + pk, err := base64.StdEncoding.DecodeString(registrarTwin.PublicKey) + if err != nil { + return Twin{}, err + } + + e2ePK, err := base64.StdEncoding.DecodeString(registrarTwin.RMBEncKey) + if err != nil { + return Twin{}, err + } + + if len(e2ePK) == 0 { + e2ePK = pk } - _, PK := substrateTwin.Pk.Unwrap() twin := Twin{ ID: id, - PublicKey: substrateTwin.Account.PublicKey(), + PublicKey: pk, Relay: relay, - E2EKey: PK, + E2EKey: e2ePK, } return twin, nil } func (t *twinDB) GetByPk(pk []byte) (uint32, error) { - return t.subConn.GetTwinByPubKey(pk) + req, _ := http.NewRequest( + "GET", + fmt.Sprintf("%s/v1/accounts?public_key=%v", t.registrarUrl, base64.StdEncoding.EncodeToString(pk)), + nil, + ) + + resp, err := t.httpClient.Do(req) + if err != nil { + return 0, errors.Wrap(err, "could not get twin") + } + defer resp.Body.Close() + + var registrarTwin RegistrarTwin + if err = json.NewDecoder(resp.Body).Decode(®istrarTwin); err != nil { + return 0, err + } + + return uint32(registrarTwin.TwinID), nil +} + +func UpdateTwin(twinID uint32, privateKey, rmbEncKey []byte, relays []string, registrarUrl string) error { + client := &http.Client{} + + timestamp := time.Now().Unix() + challenge := []byte(fmt.Sprintf("%d:%v", timestamp, twinID)) + signature := ed25519.Sign(privateKey, challenge) + + fmt.Printf("rmbEncKey: %v\n", rmbEncKey) + updates := updateTwin{ + Relays: relays, + RMBEncKey: base64.StdEncoding.EncodeToString(rmbEncKey), + } + + jsonData, err := json.Marshal(updates) + if err != nil { + return err + } + + req, _ := http.NewRequest( + "PATCH", + fmt.Sprintf("%s/v1/accounts/%v", registrarUrl, twinID), + strings.NewReader(string(jsonData)), + ) + + authHeader := fmt.Sprintf( + "%s:%s", + base64.StdEncoding.EncodeToString(challenge), + base64.StdEncoding.EncodeToString(signature), + ) + + req.Header.Set("X-Auth", authHeader) + req.Header.Set("Content-Type", "application/json") + + resp, err := client.Do(req) + if err != nil { + return err + } + + if resp == nil { + return errors.New("failed to update twin, no response received") + } + + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("request failed with %d", resp.StatusCode) + } + + return nil } // if ttl == 0, then the data will stay forever @@ -126,12 +235,8 @@ type tmpCache struct { inner TwinDB } -func newTmpCache(ttl uint64, inner TwinDB, chainURL string) (TwinDB, error) { - u, err := url.Parse(chainURL) - if err != nil { - return nil, err - } - path := filepath.Join(os.TempDir(), "rmb-cache", u.Host) +func newTmpCache(ttl uint64, inner TwinDB) (TwinDB, error) { + path := filepath.Join(os.TempDir(), "rmb-cache") if err := os.MkdirAll(path, 0755); err != nil { return nil, err } diff --git a/rmb-sdk-go/wiki/using_rmb_peer.md b/rmb-sdk-go/wiki/using_rmb_peer.md index 9ec374e..1e5e65c 100644 --- a/rmb-sdk-go/wiki/using_rmb_peer.md +++ b/rmb-sdk-go/wiki/using_rmb_peer.md @@ -13,7 +13,7 @@ rmb-peer -m "" ``` > Can be added to the system service with systemd so it can be running all the time.\ -> run `rmb-peer -h` to customize the peer, including which relay and which tfchain to connect to. +> run `rmb-peer -h` to customize the peer, including which relay and which registrar to connect to. ## Example From 5a06239592464e4c6e30a14156b859067bbae690 Mon Sep 17 00:00:00 2001 From: rawdaGastan Date: Tue, 11 Mar 2025 13:35:18 +0200 Subject: [PATCH 2/5] replace private keys with menmonic --- rmb-sdk-go/README.md | 2 +- rmb-sdk-go/examples/rpc_client/README.md | 2 +- rmb-sdk-go/examples/rpc_client/main.go | 10 ++-------- rmb-sdk-go/peer/examples/peer/main.go | 10 ++-------- rmb-sdk-go/peer/examples/peer_pingmany/main.go | 13 +++---------- rmb-sdk-go/peer/examples/router_server/main.go | 12 +++--------- rmb-sdk-go/peer/examples/rpc/main.go | 10 ++-------- rmb-sdk-go/peer/peer.go | 13 +++++++------ rmb-sdk-go/peer/rpc.go | 4 ++-- rmb-sdk-go/peer/twindb.go | 16 ++++++++++++---- 10 files changed, 35 insertions(+), 57 deletions(-) diff --git a/rmb-sdk-go/README.md b/rmb-sdk-go/README.md index 9cff723..942350d 100644 --- a/rmb-sdk-go/README.md +++ b/rmb-sdk-go/README.md @@ -21,7 +21,7 @@ This connection could be established using a `direct-client`, or an `rmb-peer`. A process could connect to an `rmb-relay` using a direct client.\ To create a new direct client instance, a process needs to have: -- A valid private key, with an activated account on the Registrar. +- A valid mnemonic, with an activated account on the Registrar. - The key type of these mnemonics. - A relay URL that the direct client will connect to. - A session id. This could be anything, but a twin must only have a unique session id per connection. diff --git a/rmb-sdk-go/examples/rpc_client/README.md b/rmb-sdk-go/examples/rpc_client/README.md index e4253aa..b2b6083 100644 --- a/rmb-sdk-go/examples/rpc_client/README.md +++ b/rmb-sdk-go/examples/rpc_client/README.md @@ -6,7 +6,7 @@ This is a `Go` example for the `RMB` [rpc client](https://github.com/threefoldte To use the example, you needs to: -- Set the private key variable to a valid private key, with an activated account on the Registrar. +- Set the mnemonics variable to a valid mnemonic, with an activated account on the Registrar. - A node id to send the call to ## Usage diff --git a/rmb-sdk-go/examples/rpc_client/main.go b/rmb-sdk-go/examples/rpc_client/main.go index 301e35a..68dfc19 100644 --- a/rmb-sdk-go/examples/rpc_client/main.go +++ b/rmb-sdk-go/examples/rpc_client/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "encoding/hex" "fmt" "log" "time" @@ -16,16 +15,11 @@ type version struct { } func app() error { - privateKey := "" - - privateKeyBytes, err := hex.DecodeString(privateKey) - if err != nil { - return fmt.Errorf("failed to decode private key: %w", err) - } + mnemonic := "" client, err := peer.NewRpcClient( context.Background(), - privateKeyBytes, + mnemonic, peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"), peer.WithRelay("wss://relay.dev.grid.tf"), peer.WithSession("test-client"), diff --git a/rmb-sdk-go/peer/examples/peer/main.go b/rmb-sdk-go/peer/examples/peer/main.go index 2493a00..425c912 100644 --- a/rmb-sdk-go/peer/examples/peer/main.go +++ b/rmb-sdk-go/peer/examples/peer/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "encoding/hex" "fmt" "math/rand" @@ -15,17 +14,12 @@ import ( var resultsChan = make(chan bool) func app() error { - privateKey := "" + mnemonic := "" ctx := context.Background() - privateKeyBytes, err := hex.DecodeString(privateKey) - if err != nil { - return fmt.Errorf("failed to decode private key: %w", err) - } - peer, err := peer.NewPeer( ctx, - privateKeyBytes, + mnemonic, relayCallback, peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"), peer.WithRelay("wss://relay.dev.grid.tf"), diff --git a/rmb-sdk-go/peer/examples/peer_pingmany/main.go b/rmb-sdk-go/peer/examples/peer_pingmany/main.go index b565f8c..ea8ec67 100644 --- a/rmb-sdk-go/peer/examples/peer_pingmany/main.go +++ b/rmb-sdk-go/peer/examples/peer_pingmany/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "encoding/hex" "encoding/json" "fmt" "net/http" @@ -17,8 +16,8 @@ import ( ) const ( - relayUrl = "ws://localhost:" - privateKey = "" + relayUrl = "ws://localhost:" + mnemonic = "" ) type Node struct { @@ -55,14 +54,8 @@ func main() { log.Info().Uint32("twin", env.Source.Twin).Str("version", version).Msg("received response") } - privateKeyBytes, err := hex.DecodeString(privateKey) - if err != nil { - log.Error().Err(err).Msg("failed to decode private key") - return - } - bus, err := peer.NewPeer(context.Background(), - privateKeyBytes, + mnemonic, handler, peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"), peer.WithKeyType(peer.KeyTypeSr25519), diff --git a/rmb-sdk-go/peer/examples/router_server/main.go b/rmb-sdk-go/peer/examples/router_server/main.go index 12a7bb5..361e56c 100644 --- a/rmb-sdk-go/peer/examples/router_server/main.go +++ b/rmb-sdk-go/peer/examples/router_server/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "encoding/hex" "encoding/json" "errors" "fmt" @@ -59,20 +58,15 @@ func app() error { }) // adding a peer for the router - privateKey := "" + mnemonic := "" ctx := context.Background() - privateKeyBytes, err := hex.DecodeString(privateKey) - if err != nil { - return fmt.Errorf("failed to decode private key: %w", err) - } - // this peer will be a 'calculator' session. // means other peers on the network need to know that // session id to use when they are making calls - _, err = peer.NewPeer( + _, err := peer.NewPeer( ctx, - privateKeyBytes, + mnemonic, router.Serve, peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"), peer.WithRelay("wss://relay.dev.grid.tf"), diff --git a/rmb-sdk-go/peer/examples/rpc/main.go b/rmb-sdk-go/peer/examples/rpc/main.go index 5222fce..d898191 100644 --- a/rmb-sdk-go/peer/examples/rpc/main.go +++ b/rmb-sdk-go/peer/examples/rpc/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "encoding/hex" "fmt" "time" @@ -12,16 +11,11 @@ import ( ) func app() error { - privateKey := "" - - privateKeyBytes, err := hex.DecodeString(privateKey) - if err != nil { - return fmt.Errorf("failed to decode private key: %w", err) - } + mnemonic := "" client, err := peer.NewRpcClient( context.Background(), - privateKeyBytes, + mnemonic, peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"), peer.WithKeyType(peer.KeyTypeSr25519), peer.WithRelay("wss://relay.dev.grid.tf"), diff --git a/rmb-sdk-go/peer/peer.go b/rmb-sdk-go/peer/peer.go index c83714b..e6f1844 100644 --- a/rmb-sdk-go/peer/peer.go +++ b/rmb-sdk-go/peer/peer.go @@ -137,15 +137,15 @@ func generateSecureKey(identity substrate.Identity) (*secp256k1.PrivateKey, erro return priv, nil } -func getIdentity(keyType string, privateKey []byte) (substrate.Identity, error) { +func getIdentity(keyType string, mnemonics string) (substrate.Identity, error) { //TODO: var identity substrate.Identity var err error switch keyType { case KeyTypeEd25519: - identity, err = substrate.NewIdentityFromEd25519Key(privateKey) + identity, err = substrate.NewIdentityFromEd25519Phrase(mnemonics) case KeyTypeSr25519: - // identity, err = substrate.NewIdentityFromSr25519Phrase(privateKeyBytes) //TODO: + identity, err = substrate.NewIdentityFromSr25519Phrase(mnemonics) default: return nil, fmt.Errorf("invalid key type %s, should be one of %s or %s ", keyType, KeyTypeEd25519, KeyTypeSr25519) } @@ -164,7 +164,7 @@ func getIdentity(keyType string, privateKey []byte) (substrate.Identity, error) // Call() will panic if called while the directClient's context is canceled. func NewPeer( ctx context.Context, - privateKey []byte, + mnemonic string, handler Handler, opts ...PeerOpt, ) (*Peer, error) { @@ -186,7 +186,8 @@ func NewPeer( if cfg.encoder == nil { cfg.encoder = encoder.NewJSONEncoder() } - identity, err := getIdentity(cfg.keyType, privateKey) + + identity, err := getIdentity(cfg.keyType, mnemonic) if err != nil { return nil, err } @@ -233,7 +234,7 @@ func NewPeer( if !bytes.Equal(twin.E2EKey, publicKey) || twin.Relay == nil || relayURLs[0] != *twin.Relay { // TODO: multiple relays (slice?) log.Info().Strs("Relay url/s", relayURLs).Msg("twin relay/public key didn't match, updating on registrar ...") - if err = UpdateTwin(twin.ID, privateKey, publicKey, relayURLs, cfg.registrarUrl); err != nil { + if err = UpdateTwin(twin.ID, cfg.registrarUrl, mnemonic, publicKey, relayURLs); err != nil { return nil, errors.Wrap(err, "could not update twin relay information") } } diff --git a/rmb-sdk-go/peer/rpc.go b/rmb-sdk-go/peer/rpc.go index 1288354..98be5f9 100644 --- a/rmb-sdk-go/peer/rpc.go +++ b/rmb-sdk-go/peer/rpc.go @@ -33,7 +33,7 @@ type RpcClient struct { // it easy to make rpc calls func NewRpcClient( ctx context.Context, - privateKey []byte, + mnemonic string, opts ...PeerOpt) (*RpcClient, error) { rpc := RpcClient{ @@ -42,7 +42,7 @@ func NewRpcClient( base, err := NewPeer( ctx, - privateKey, + mnemonic, rpc.router, opts..., ) diff --git a/rmb-sdk-go/peer/twindb.go b/rmb-sdk-go/peer/twindb.go index 4cef7bd..2b4e963 100644 --- a/rmb-sdk-go/peer/twindb.go +++ b/rmb-sdk-go/peer/twindb.go @@ -1,7 +1,6 @@ package peer import ( - "crypto/ed25519" "encoding/base64" "encoding/json" "fmt" @@ -14,6 +13,8 @@ import ( "github.com/pkg/errors" "github.com/rs/zerolog/log" + "github.com/vedhavyas/go-subkey" + subkeyEd25519 "github.com/vedhavyas/go-subkey/ed25519" ) var ( @@ -130,14 +131,21 @@ func (t *twinDB) GetByPk(pk []byte) (uint32, error) { return uint32(registrarTwin.TwinID), nil } -func UpdateTwin(twinID uint32, privateKey, rmbEncKey []byte, relays []string, registrarUrl string) error { +func UpdateTwin(twinID uint32, registrarUrl, mnemonic string, rmbEncKey []byte, relays []string) error { client := &http.Client{} + keypair, err := subkey.DeriveKeyPair(subkeyEd25519.Scheme{}, mnemonic) + if err != nil { + return err + } + timestamp := time.Now().Unix() challenge := []byte(fmt.Sprintf("%d:%v", timestamp, twinID)) - signature := ed25519.Sign(privateKey, challenge) + signature, err := keypair.Sign(challenge) + if err != nil { + return err + } - fmt.Printf("rmbEncKey: %v\n", rmbEncKey) updates := updateTwin{ Relays: relays, RMBEncKey: base64.StdEncoding.EncodeToString(rmbEncKey), From bf79e91cb722d93fd908a93f285e170f3fdfb0a5 Mon Sep 17 00:00:00 2001 From: rawdaGastan Date: Wed, 12 Mar 2025 13:12:52 +0200 Subject: [PATCH 3/5] replace private key with mnemonic and remove tf chain totally --- .github/workflows/lint.yml | 1 + .github/workflows/test.yml | 1 + Makefile | 7 +-- README.md | 1 + go.work | 5 +- rmb-sdk-go/README.md | 2 +- rmb-sdk-go/examples/client/main.go | 2 +- rmb-sdk-go/examples/rpc_client/main.go | 2 +- rmb-sdk-go/examples/server/main.go | 2 +- rmb-sdk-go/go.mod | 18 ++----- rmb-sdk-go/go.sum | 47 ++++--------------- rmb-sdk-go/peer/connection.go | 8 ++-- rmb-sdk-go/peer/examples/peer/main.go | 4 +- .../peer/examples/peer_pingmany/main.go | 4 +- .../peer/examples/router_server/main.go | 4 +- rmb-sdk-go/peer/examples/rpc/main.go | 2 +- rmb-sdk-go/peer/hashes.go | 2 +- rmb-sdk-go/peer/jwt.go | 37 ++++++++++++--- rmb-sdk-go/peer/peer.go | 31 ++++++------ rmb-sdk-go/peer/router.go | 4 +- rmb-sdk-go/peer/rpc.go | 4 +- rmb-sdk-go/peer/sig.go | 2 +- rmb-sdk-go/peer/sig_test.go | 19 +++----- rmb-sdk-go/peer/twindb.go | 33 ++++++++----- rmb-sdk-go/peer/types.proto | 2 +- 25 files changed, 113 insertions(+), 131 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index cb726f2..a057dec 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -28,3 +28,4 @@ jobs: matrix: dir: - node-registrar + - rmb-sdk-go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 64c4850..41d9428 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -30,3 +30,4 @@ jobs: matrix: dir: - node-registrar + - rmb-sdk-go diff --git a/Makefile b/Makefile index bf3bd47..f77543b 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,4 @@ -DIRS := "node-registrar" - -mainnet-release: - cd grid-client && go get github.com/threefoldtech/tfchain/clients/tfchain-client-go@5d6a2dd - go work sync - make tidy +DIRS := "node-registrar" "rmb-sdk-go" release-rmb: @echo "Release RMB..." diff --git a/README.md b/README.md index 93514bc..018e0de 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ This repo contains the go clients for Threefold grid. ## Packages - [node-registrar](./node-registrar/README.md) +- [rmb-sdk-go](./rmb-sdk-go/README.md) ## Release diff --git a/go.work b/go.work index f329111..739afde 100644 --- a/go.work +++ b/go.work @@ -1,3 +1,6 @@ go 1.21.0 -use ./node-registrar +use ( + ./node-registrar + ./rmb-sdk-go +) diff --git a/rmb-sdk-go/README.md b/rmb-sdk-go/README.md index 942350d..0f44057 100644 --- a/rmb-sdk-go/README.md +++ b/rmb-sdk-go/README.md @@ -1,4 +1,4 @@ -[![Go Documentation](https://godocs.io/github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go?status.svg)](https://godocs.io/github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go) +[![Go Documentation](https://godocs.io/github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go?status.svg)](https://godocs.io/github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go) # Introduction diff --git a/rmb-sdk-go/examples/client/main.go b/rmb-sdk-go/examples/client/main.go index 282f132..13dec1e 100644 --- a/rmb-sdk-go/examples/client/main.go +++ b/rmb-sdk-go/examples/client/main.go @@ -6,7 +6,7 @@ import ( "os" "time" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go" ) func app() error { diff --git a/rmb-sdk-go/examples/rpc_client/main.go b/rmb-sdk-go/examples/rpc_client/main.go index 68dfc19..da402aa 100644 --- a/rmb-sdk-go/examples/rpc_client/main.go +++ b/rmb-sdk-go/examples/rpc_client/main.go @@ -6,7 +6,7 @@ import ( "log" "time" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go/peer" ) type version struct { diff --git a/rmb-sdk-go/examples/server/main.go b/rmb-sdk-go/examples/server/main.go index b797418..47cce3f 100644 --- a/rmb-sdk-go/examples/server/main.go +++ b/rmb-sdk-go/examples/server/main.go @@ -6,7 +6,7 @@ import ( "fmt" "os" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go" ) func app() error { diff --git a/rmb-sdk-go/go.mod b/rmb-sdk-go/go.mod index 2a4c96c..c7da67a 100644 --- a/rmb-sdk-go/go.mod +++ b/rmb-sdk-go/go.mod @@ -1,4 +1,4 @@ -module github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go +module github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go go 1.21 @@ -15,38 +15,28 @@ require ( github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.33.0 github.com/stretchr/testify v1.10.0 - github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20241007205731-5e76664a3cc4 + github.com/vedhavyas/go-subkey/v2 v2.0.0 gonum.org/v1/gonum v0.15.0 google.golang.org/protobuf v1.34.1 ) require ( - github.com/cenkalti/backoff v2.2.1+incompatible // indirect - github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.12 // indirect github.com/cosmos/go-bip39 v1.0.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect - github.com/deckarep/golang-set v1.8.0 // indirect github.com/decred/base58 v1.0.5 // indirect github.com/decred/dcrd/crypto/blake256 v1.0.1 // indirect github.com/ethereum/go-ethereum v1.11.6 // indirect - github.com/go-stack/stack v1.8.1 // indirect github.com/gtank/ristretto255 v0.1.2 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/holiman/uint256 v1.2.3 // indirect - github.com/jbenet/go-base58 v0.0.0-20150317085156-6237cf65f3a6 // indirect + github.com/kr/pretty v0.3.1 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mimoo/StrobeGo v0.0.0-20220103164710-9a04d6ca976b // indirect - github.com/pierrec/xxHash v0.1.5 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/rogpeppe/go-internal v1.11.0 // indirect - github.com/rs/cors v1.10.1 // indirect - github.com/shirou/gopsutil v3.21.11+incompatible // indirect - github.com/tklauser/go-sysconf v0.3.11 // indirect - github.com/vedhavyas/go-subkey v1.0.3 // indirect - github.com/yusufpapurcu/wmi v1.2.2 // indirect golang.org/x/crypto v0.28.0 // indirect golang.org/x/sys v0.26.0 // indirect - gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/rmb-sdk-go/go.sum b/rmb-sdk-go/go.sum index 2651341..e72f25b 100644 --- a/rmb-sdk-go/go.sum +++ b/rmb-sdk-go/go.sum @@ -1,24 +1,14 @@ github.com/ChainSafe/go-schnorrkel v1.1.0 h1:rZ6EU+CZFCjB4sHUE1jIu8VDoB/wRKZxoe1tkcO71Wk= github.com/ChainSafe/go-schnorrkel v1.1.0/go.mod h1:ABkENxiP+cvjFiByMIZ9LYbRoNNLeBLiakC1XeTFxfE= -github.com/btcsuite/btcd v0.22.0-beta h1:LTDpDKUM5EeOFBPM8IXpinEcmZ6FWfNZbE3lfrfdnWo= github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPxiMSCF5k= github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU= -github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce h1:YtWJF7RHm2pYCvA5t0RPmAaLUhREsKuKd+SLhxFbFeQ= -github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce/go.mod h1:0DVlHczLPewLcPGEIeUEzfOJhqGPQ0mJJRDBtD307+o= -github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= -github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= -github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.12 h1:DCYWIBOalB0mKKfUg2HhtGgIkBbMA1fnlnkZp7fHB18= -github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.12/go.mod h1:5g1oM4Zu3BOaLpsKQ+O8PAv2kNuq+kPcA1VzFbsSqxE= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY= github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/deckarep/golang-set v1.8.0 h1:sk9/l/KqpunDwP7pSjUg0keiOOLEnOBHzykLrsPppp4= -github.com/deckarep/golang-set v1.8.0/go.mod h1:5nI87KwE7wgsBU1F4GKAw2Qod7p5kyS383rP6+o6qqo= -github.com/deckarep/golang-set/v2 v2.1.0 h1:g47V4Or+DUdzbs8FxCCmgb6VYd+ptPAngjM6dtGktsI= -github.com/deckarep/golang-set/v2 v2.1.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4= github.com/decred/base58 v1.0.5 h1:hwcieUM3pfPnE/6p3J100zoRfGkQxBulZHo7GZfOqic= github.com/decred/base58 v1.0.5/go.mod h1:s/8lukEHFA6bUQQb/v3rjUySJ2hu+RioCzLukAVkrfw= github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5ilcvdfma9wOH6Y= @@ -27,10 +17,6 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 h1:rpfIENRNNilwHwZeG5+P150SMrnN github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0= github.com/ethereum/go-ethereum v1.11.6 h1:2VF8Mf7XiSUfmoNOy3D+ocfl9Qu8baQBrCNbo2CXQ8E= github.com/ethereum/go-ethereum v1.11.6/go.mod h1:+a8pUj1tOyJ2RinsNQD4326YS+leSoKGiG/uVVb0x6Y= -github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= -github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= -github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw= -github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= @@ -40,8 +26,6 @@ github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNu github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa h1:Q75Upo5UN4JbPFURXZ8nLKYUvF85dyFRop/vQ0Rv+64= -github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= @@ -57,10 +41,11 @@ github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+l github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= github.com/holiman/uint256 v1.2.3 h1:K8UWO1HUJpRMXBxbmaY1Y8IAMZC/RsKB+ArEnnK4l5o= github.com/holiman/uint256 v1.2.3/go.mod h1:SC8Ryt4n+UBbPbIBKaG9zbbDlp4jOru9xFZmPzLUTxw= -github.com/jbenet/go-base58 v0.0.0-20150317085156-6237cf65f3a6 h1:4zOlv2my+vf98jT1nQt4bT/yKWUImevYPJ2H344CloE= -github.com/jbenet/go-base58 v0.0.0-20150317085156-6237cf65f3a6/go.mod h1:r/8JmuR0qjuCiEhAolkfvdZgmPiHTnJaG0UXCSeR1Zo= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= @@ -72,37 +57,25 @@ github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643/go.mod h1:43+3pMjjKimDBf5Kr4ZFNGbLql1zKkbImw+fZbw3geM= github.com/mimoo/StrobeGo v0.0.0-20220103164710-9a04d6ca976b h1:QrHweqAtyJ9EwCaGHBu1fghwxIPiopAHV06JlXrMHjk= github.com/mimoo/StrobeGo v0.0.0-20220103164710-9a04d6ca976b/go.mod h1:xxLb2ip6sSUts3g1irPVHyk/DGslwQsNOo9I7smJfNU= -github.com/pierrec/xxHash v0.1.5 h1:n/jBpwTHiER4xYvK3/CdPVnLDPchj8eTJFFLUb4QHBo= -github.com/pierrec/xxHash v0.1.5/go.mod h1:w2waW5Zoa/Wc4Yqe0wgrIYAGKqRMf7czn2HNKXmuL+I= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= -github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo= -github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= -github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI= -github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20241007205731-5e76664a3cc4 h1:XIXVdFrum50Wnxv62sS+cEgqHtvdInWB2Co8AJVJ8xs= -github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20241007205731-5e76664a3cc4/go.mod h1:cOL5YgHUmDG5SAXrsZxFjUECRQQuAqOoqvXhZG5sEUw= -github.com/tklauser/go-sysconf v0.3.11 h1:89WgdJhk5SNwJfu+GKyYveZ4IaJ7xAkecBo+KdJV0CM= -github.com/tklauser/go-sysconf v0.3.11/go.mod h1:GqXfhXY3kiPa0nAXPDIQIWzJbMCB7AmcWpGR8lSZfqI= -github.com/tklauser/numcpus v0.6.0 h1:kebhY2Qt+3U6RNK7UqpYNA+tJ23IBEGKkB7JQBfDYms= -github.com/tklauser/numcpus v0.6.0/go.mod h1:FEZLMke0lhOUG6w2JadTzp0a+Nl8PF/GFkQ5UVIcaL4= -github.com/vedhavyas/go-subkey v1.0.3 h1:iKR33BB/akKmcR2PMlXPBeeODjWLM90EL98OrOGs8CA= -github.com/vedhavyas/go-subkey v1.0.3/go.mod h1:CloUaFQSSTdWnINfBRFjVMkWXZANW+nd8+TI5jYcl6Y= +github.com/vedhavyas/go-subkey/v2 v2.0.0 h1:LemDIsrVtRSOkp0FA8HxP6ynfKjeOj3BY2U9UNfeDMA= +github.com/vedhavyas/go-subkey/v2 v2.0.0/go.mod h1:95aZ+XDCWAUUynjlmi7BtPExjXgXxByE0WfBwbmIRH4= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= -github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg= -github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= @@ -118,12 +91,10 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= @@ -144,8 +115,6 @@ google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHh gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU= -gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/rmb-sdk-go/peer/connection.go b/rmb-sdk-go/peer/connection.go index 4a342ca..f4be484 100644 --- a/rmb-sdk-go/peer/connection.go +++ b/rmb-sdk-go/peer/connection.go @@ -10,7 +10,7 @@ import ( "github.com/gorilla/websocket" "github.com/pkg/errors" "github.com/rs/zerolog/log" - substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go" + "github.com/vedhavyas/go-subkey/v2" ) const ( @@ -24,7 +24,7 @@ var errTimeout = fmt.Errorf("connection timeout") type InnerConnection struct { twinID uint32 session string - identity substrate.Identity + identity subkey.KeyPair url string writer chan send } @@ -57,10 +57,10 @@ func (r Reader) Read() []byte { } // NewConnection creates a new InnerConnection instance -func NewConnection(identity substrate.Identity, url string, session string, twinID uint32) InnerConnection { +func NewConnection(keyPair subkey.KeyPair, url string, session string, twinID uint32) InnerConnection { return InnerConnection{ twinID: twinID, - identity: identity, + identity: keyPair, url: url, session: session, writer: make(chan send), diff --git a/rmb-sdk-go/peer/examples/peer/main.go b/rmb-sdk-go/peer/examples/peer/main.go index 425c912..0fcfae4 100644 --- a/rmb-sdk-go/peer/examples/peer/main.go +++ b/rmb-sdk-go/peer/examples/peer/main.go @@ -7,8 +7,8 @@ import ( "github.com/google/uuid" "github.com/rs/zerolog/log" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer/types" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go/peer" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go/peer/types" ) var resultsChan = make(chan bool) diff --git a/rmb-sdk-go/peer/examples/peer_pingmany/main.go b/rmb-sdk-go/peer/examples/peer_pingmany/main.go index ea8ec67..a2e5dbb 100644 --- a/rmb-sdk-go/peer/examples/peer_pingmany/main.go +++ b/rmb-sdk-go/peer/examples/peer_pingmany/main.go @@ -11,8 +11,8 @@ import ( "github.com/rs/zerolog/log" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer/types" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go/peer" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go/peer/types" ) const ( diff --git a/rmb-sdk-go/peer/examples/router_server/main.go b/rmb-sdk-go/peer/examples/router_server/main.go index 361e56c..4db67c1 100644 --- a/rmb-sdk-go/peer/examples/router_server/main.go +++ b/rmb-sdk-go/peer/examples/router_server/main.go @@ -7,8 +7,8 @@ import ( "fmt" "os" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go/peer" ) func app() error { diff --git a/rmb-sdk-go/peer/examples/rpc/main.go b/rmb-sdk-go/peer/examples/rpc/main.go index d898191..2e87609 100644 --- a/rmb-sdk-go/peer/examples/rpc/main.go +++ b/rmb-sdk-go/peer/examples/rpc/main.go @@ -7,7 +7,7 @@ import ( "log" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go/peer" ) func app() error { diff --git a/rmb-sdk-go/peer/hashes.go b/rmb-sdk-go/peer/hashes.go index 2e1e85a..fe761ed 100644 --- a/rmb-sdk-go/peer/hashes.go +++ b/rmb-sdk-go/peer/hashes.go @@ -5,7 +5,7 @@ import ( "fmt" "io" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer/types" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go/peer/types" ) func Challenge(env *types.Envelope) ([]byte, error) { diff --git a/rmb-sdk-go/peer/jwt.go b/rmb-sdk-go/peer/jwt.go index 26b0499..6c4576b 100644 --- a/rmb-sdk-go/peer/jwt.go +++ b/rmb-sdk-go/peer/jwt.go @@ -2,11 +2,13 @@ package peer import ( "encoding/base64" + "errors" "fmt" + "reflect" "time" "github.com/golang-jwt/jwt" - substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go" + "github.com/vedhavyas/go-subkey/v2" ) const CustomSigning = "RMB" @@ -22,9 +24,9 @@ func (s *RmbSigner) Verify(signingString, signature string, key interface{}) err } func (s *RmbSigner) Sign(signingString string, key interface{}) (string, error) { - identity, ok := key.(substrate.Identity) + identity, ok := key.(subkey.KeyPair) if !ok { - return "", fmt.Errorf("invalid key expecting substrate identity") + return "", fmt.Errorf("invalid key expecting subkey keypair") } signature, err := Sign(identity, []byte(signingString)) @@ -39,7 +41,7 @@ func (s *RmbSigner) Alg() string { return "RS512" } -func NewJWT(identity substrate.Identity, id uint32, session string, ttl uint32) (string, error) { +func NewJWT(identity subkey.KeyPair, id uint32, session string, ttl uint32) (string, error) { now := time.Now().Unix() claims := jwt.MapClaims{ "sub": id, @@ -54,14 +56,37 @@ func NewJWT(identity substrate.Identity, id uint32, session string, ttl uint32) return token.SignedString(identity) } -func Sign(signer substrate.Identity, input []byte) ([]byte, error) { +func Sign(signer subkey.KeyPair, input []byte) ([]byte, error) { signature, err := signer.Sign(input) if err != nil { return nil, err } withType := make([]byte, len(signature)+1) - withType[0] = signer.Type()[0] // edIdentity will return e, while sr will be s + keyType, err := getKeyPairType(signer) + if err != nil { + return nil, err + } + + if keyType == KeyTypeSr25519 { + withType[0] = []byte("s")[0] // edIdentity will return e, while sr will be s + } + + if keyType == KeyTypeEd25519 { + withType[0] = []byte("e")[0] // edIdentity will return e, while sr will be s + } + copy(withType[1:], signature) return withType, nil } + +func getKeyPairType(pair subkey.KeyPair) (string, error) { + switch reflect.TypeOf(pair).String() { + case "*sr25519.keyRing": + return KeyTypeSr25519, nil + case "ed25519.keyRing": + return KeyTypeEd25519, nil + default: + return "", errors.New("unknown key type") + } +} diff --git a/rmb-sdk-go/peer/peer.go b/rmb-sdk-go/peer/peer.go index e6f1844..a98e7e2 100644 --- a/rmb-sdk-go/peer/peer.go +++ b/rmb-sdk-go/peer/peer.go @@ -19,10 +19,12 @@ import ( "github.com/hashicorp/go-multierror" "github.com/pkg/errors" "github.com/rs/zerolog/log" - substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer/encoder" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer/types" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go/peer/encoder" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go/peer/types" + "github.com/vedhavyas/go-subkey/v2" + "github.com/vedhavyas/go-subkey/v2/ed25519" + "github.com/vedhavyas/go-subkey/v2/sr25519" "google.golang.org/protobuf/proto" ) @@ -117,7 +119,7 @@ func WithInMemoryExpiration(ttl uint64) PeerOpt { // Peer exposes the functionality to talk directly to an rmb relay type Peer struct { source *types.Address - signer substrate.Identity + signer subkey.KeyPair twinDB TwinDB privKey *secp256k1.PrivateKey reader Reader @@ -127,25 +129,20 @@ type Peer struct { relays []string } -func generateSecureKey(identity substrate.Identity) (*secp256k1.PrivateKey, error) { - keyPair, err := identity.KeyPair() - if err != nil { - return nil, errors.Wrap(err, "failed to generate identity key pair") - } - +func generateSecureKey(keyPair subkey.KeyPair) (*secp256k1.PrivateKey, error) { priv := secp256k1.PrivKeyFromBytes(keyPair.Seed()) return priv, nil } -func getIdentity(keyType string, mnemonics string) (substrate.Identity, error) { //TODO: - var identity substrate.Identity +func getIdentity(keyType string, mnemonic string) (subkey.KeyPair, error) { + var identity subkey.KeyPair var err error switch keyType { case KeyTypeEd25519: - identity, err = substrate.NewIdentityFromEd25519Phrase(mnemonics) + identity, err = subkey.DeriveKeyPair(ed25519.Scheme{}, mnemonic) case KeyTypeSr25519: - identity, err = substrate.NewIdentityFromSr25519Phrase(mnemonics) + identity, err = subkey.DeriveKeyPair(sr25519.Scheme{}, mnemonic) default: return nil, fmt.Errorf("invalid key type %s, should be one of %s or %s ", keyType, KeyTypeEd25519, KeyTypeSr25519) } @@ -197,7 +194,7 @@ func NewPeer( return nil, err } - id, err := twinDB.GetByPk(identity.PublicKey()) + id, err := twinDB.GetByPk(identity.Public()) if err != nil { return nil, errors.Wrapf(err, "failed to get twin by public key") } @@ -234,7 +231,7 @@ func NewPeer( if !bytes.Equal(twin.E2EKey, publicKey) || twin.Relay == nil || relayURLs[0] != *twin.Relay { // TODO: multiple relays (slice?) log.Info().Strs("Relay url/s", relayURLs).Msg("twin relay/public key didn't match, updating on registrar ...") - if err = UpdateTwin(twin.ID, cfg.registrarUrl, mnemonic, publicKey, relayURLs); err != nil { + if err = UpdateTwin(twin.ID, cfg.registrarUrl, identity, publicKey, relayURLs); err != nil { return nil, errors.Wrap(err, "could not update twin relay information") } } diff --git a/rmb-sdk-go/peer/router.go b/rmb-sdk-go/peer/router.go index 429eb80..740c7b7 100644 --- a/rmb-sdk-go/peer/router.go +++ b/rmb-sdk-go/peer/router.go @@ -6,8 +6,8 @@ import ( "strings" "github.com/rs/zerolog/log" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer/types" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go/peer/types" ) var ( diff --git a/rmb-sdk-go/peer/rpc.go b/rmb-sdk-go/peer/rpc.go index 98be5f9..2997e56 100644 --- a/rmb-sdk-go/peer/rpc.go +++ b/rmb-sdk-go/peer/rpc.go @@ -8,8 +8,8 @@ import ( "sync" "github.com/google/uuid" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer/types" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go/peer/types" ) var ( diff --git a/rmb-sdk-go/peer/sig.go b/rmb-sdk-go/peer/sig.go index b698fd2..e20bbca 100644 --- a/rmb-sdk-go/peer/sig.go +++ b/rmb-sdk-go/peer/sig.go @@ -7,7 +7,7 @@ import ( sr25519 "github.com/ChainSafe/go-schnorrkel" "github.com/pkg/errors" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer/types" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go/peer/types" "github.com/gtank/merlin" "github.com/rs/zerolog/log" diff --git a/rmb-sdk-go/peer/sig_test.go b/rmb-sdk-go/peer/sig_test.go index 94c5827..9bb9093 100644 --- a/rmb-sdk-go/peer/sig_test.go +++ b/rmb-sdk-go/peer/sig_test.go @@ -7,18 +7,17 @@ import ( gomock "github.com/golang/mock/gomock" "github.com/google/uuid" "github.com/stretchr/testify/assert" - substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go" - "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer/types" + "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go/peer/types" + "github.com/vedhavyas/go-subkey/v2" + "github.com/vedhavyas/go-subkey/v2/sr25519" ) const sigVerifyAccMnemonics = "garage dad improve reunion girl saddle theory know label reason fantasy deputy" const sigVerifyAccTwinID = uint32(1171) -var sigVerifyAccAddress = "5CtwsdH1ggRAgCv2GVfBviWywHzwsYJvhPWhmShpx2DGnb6B" - func TestSignature(t *testing.T) { - identity, err := substrate.NewIdentityFromSr25519Phrase(sigVerifyAccMnemonics) + identity, err := subkey.DeriveKeyPair(sr25519.Scheme{}, sigVerifyAccMnemonics) if err != nil { t.Fatalf("could not init new identity: %s", err) } @@ -52,13 +51,10 @@ func TestSignature(t *testing.T) { env.Signature, err = Sign(identity, toSign) assert.NoError(t, err) - account, err := substrate.FromAddress(sigVerifyAccAddress) - assert.NoError(t, err) - twinDB := NewMockTwinDB(ctrl) twinDB.EXPECT().Get(sigVerifyAccTwinID).Return(Twin{ ID: sigVerifyAccTwinID, - PublicKey: account.PublicKey(), + PublicKey: identity.Public(), }, nil) err = VerifySignature(twinDB, &env) @@ -97,13 +93,10 @@ func TestSignature(t *testing.T) { env.Signature = []byte("s13p49fnaskdjnv") - account, err := substrate.FromAddress(sigVerifyAccAddress) - assert.NoError(t, err) - twinDB := NewMockTwinDB(ctrl) twinDB.EXPECT().Get(sigVerifyAccTwinID).Return(Twin{ ID: sigVerifyAccTwinID, - PublicKey: account.PublicKey(), + PublicKey: identity.Public(), }, nil) err = VerifySignature(twinDB, &env) diff --git a/rmb-sdk-go/peer/twindb.go b/rmb-sdk-go/peer/twindb.go index 2b4e963..e1df0a4 100644 --- a/rmb-sdk-go/peer/twindb.go +++ b/rmb-sdk-go/peer/twindb.go @@ -13,8 +13,7 @@ import ( "github.com/pkg/errors" "github.com/rs/zerolog/log" - "github.com/vedhavyas/go-subkey" - subkeyEd25519 "github.com/vedhavyas/go-subkey/ed25519" + "github.com/vedhavyas/go-subkey/v2" ) var ( @@ -63,11 +62,14 @@ type updateTwin struct { // GetTwin gets Twin from cache if present. if not, gets it from substrate client and caches it. func (t *twinDB) Get(id uint32) (Twin, error) { - req, _ := http.NewRequest( + req, err := http.NewRequest( "GET", fmt.Sprintf("%s/v1/accounts?twin_id=%v", t.registrarUrl, id), nil, ) + if err != nil { + return Twin{}, errors.Wrap(err, "could not create new request") + } resp, err := t.httpClient.Do(req) if err != nil { @@ -111,11 +113,18 @@ func (t *twinDB) Get(id uint32) (Twin, error) { } func (t *twinDB) GetByPk(pk []byte) (uint32, error) { - req, _ := http.NewRequest( + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/v1/accounts?public_key=%v", t.registrarUrl, base64.StdEncoding.EncodeToString(pk)), + fmt.Sprintf("%s/v1/accounts", t.registrarUrl), nil, ) + if err != nil { + return 0, errors.Wrap(err, "could not create new request") + } + + q := req.URL.Query() + q.Add("public_key", base64.StdEncoding.EncodeToString(pk)) + req.URL.RawQuery = q.Encode() resp, err := t.httpClient.Do(req) if err != nil { @@ -131,17 +140,12 @@ func (t *twinDB) GetByPk(pk []byte) (uint32, error) { return uint32(registrarTwin.TwinID), nil } -func UpdateTwin(twinID uint32, registrarUrl, mnemonic string, rmbEncKey []byte, relays []string) error { +func UpdateTwin(twinID uint32, registrarUrl string, kp subkey.KeyPair, rmbEncKey []byte, relays []string) error { client := &http.Client{} - keypair, err := subkey.DeriveKeyPair(subkeyEd25519.Scheme{}, mnemonic) - if err != nil { - return err - } - timestamp := time.Now().Unix() challenge := []byte(fmt.Sprintf("%d:%v", timestamp, twinID)) - signature, err := keypair.Sign(challenge) + signature, err := kp.Sign(challenge) if err != nil { return err } @@ -156,11 +160,14 @@ func UpdateTwin(twinID uint32, registrarUrl, mnemonic string, rmbEncKey []byte, return err } - req, _ := http.NewRequest( + req, err := http.NewRequest( "PATCH", fmt.Sprintf("%s/v1/accounts/%v", registrarUrl, twinID), strings.NewReader(string(jsonData)), ) + if err != nil { + return errors.Wrap(err, "could not create new request") + } authHeader := fmt.Sprintf( "%s:%s", diff --git a/rmb-sdk-go/peer/types.proto b/rmb-sdk-go/peer/types.proto index 5fec89c..4453fc4 100644 --- a/rmb-sdk-go/peer/types.proto +++ b/rmb-sdk-go/peer/types.proto @@ -1,6 +1,6 @@ syntax = "proto3"; -option go_package = "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer/types"; +option go_package = "github.com/threefoldtech/tfgridv4-sdk-go/rmb-sdk-go/peer/types"; // A Request annotate this message as a request message // with proper command From 34260a162f05f3d9a2d6e24ad72cbc5e82ac7d33 Mon Sep 17 00:00:00 2001 From: rawdaGastan Date: Mon, 14 Apr 2025 15:13:53 +0200 Subject: [PATCH 4/5] don't replace e2epk with twin pk --- rmb-sdk-go/peer/twindb.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/rmb-sdk-go/peer/twindb.go b/rmb-sdk-go/peer/twindb.go index e1df0a4..c8dc95b 100644 --- a/rmb-sdk-go/peer/twindb.go +++ b/rmb-sdk-go/peer/twindb.go @@ -98,10 +98,6 @@ func (t *twinDB) Get(id uint32) (Twin, error) { return Twin{}, err } - if len(e2ePK) == 0 { - e2ePK = pk - } - twin := Twin{ ID: id, PublicKey: pk, From 7e4ac316ef5b29d3fcd815fe35aecc3ac6ed4c1e Mon Sep 17 00:00:00 2001 From: rawdaGastan Date: Mon, 14 Apr 2025 15:14:23 +0200 Subject: [PATCH 5/5] increase jwt expiration --- rmb-sdk-go/peer/connection.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rmb-sdk-go/peer/connection.go b/rmb-sdk-go/peer/connection.go index f4be484..c087226 100644 --- a/rmb-sdk-go/peer/connection.go +++ b/rmb-sdk-go/peer/connection.go @@ -193,7 +193,7 @@ func (c *InnerConnection) listenAndServe(ctx context.Context, output chan []byte } func (c *InnerConnection) connect() (*websocket.Conn, error) { - token, err := NewJWT(c.identity, c.twinID, c.session, 60) + token, err := NewJWT(c.identity, c.twinID, c.session, 100) if err != nil { return nil, errors.Wrap(err, "could not create new jwt") }