Skip to content

Commit 9a3e2c2

Browse files
committed
refactor: intialize registrar client with keypair type
1 parent 35f428a commit 9a3e2c2

File tree

15 files changed

+116
-46
lines changed

15 files changed

+116
-46
lines changed

packages/registrar_client/README.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,22 @@ This package provides a client for interacting with the TFGrid v4 Node Registrar
3535

3636
## Getting Started
3737

38-
To initialize the Registrar Client, you need to provide the base url of registrar and your mnemonics or 64 character hex seed
38+
To initialize the Registrar Client, you need to provide the base URL of the registrar and either your mnemonic phrase or a 64-character hex seed. You also need to specify the keypair type.
39+
40+
The supported keypair types are:
41+
42+
- `ed25519`
43+
- `sr25519` (default)
44+
45+
Here's how to initialize the client:
46+
47+
```typescript
48+
const client = new RegistrarClient({
49+
baseUrl: "https://registrar.dev4.grid.tf/v1",
50+
mnemonicOrSeed: "your_mnemonic_or_seed",
51+
keypairType: "sr25519", // Optional, defaults to "sr25519"
52+
});
53+
```
3954

4055
To generate 64 character hex seed:
4156

@@ -46,7 +61,11 @@ openssl rand -hex 32
4661
Here is an example:
4762

4863
```typescript
49-
const client = new RegistrarClient({ baseURl: "https://registrar.dev4.grid.tf/v1", mnemonicOrSeed: "your_mnemonic_or_seed" });
64+
const client = new RegistrarClient({
65+
baseURl: "https://registrar.dev4.grid.tf/v1",
66+
mnemonicOrSeed: "your_mnemonic_or_seed",
67+
keypairType: "ed21559"
68+
});
5069
```
5170

5271
To be able to create a farm you need to have a Stellar wallet and provide your Stellar address. For more details on how to create a Stellar wallet and generate a Stellar address, please refer to the [Stellar Account Viewer](https://www.stellar.org/account-viewer/#!/) or the [Stellar Documentation](https://developers.stellar.org/docs/tutorials/create-account/).
@@ -56,7 +75,7 @@ To be able to create a farm you need to have a Stellar wallet and provide your S
5675
Here is an example of how to use the Registrar Client:
5776

5877
```typescript
59-
const client = new RegistrarClient({ baseUrl: URl, mnemonicOrSeed: your_mnemonic_or_seed });
78+
const client = new RegistrarClient({ baseUrl: URl, mnemonicOrSeed: your_mnemonic_or_seed, keypairType: "ed21559" });
6079

6180
// Example: Create an account
6281
const accountRequest: CreateAccountRequest = {

packages/registrar_client/scripts/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ yarn install
1616

1717
## Getting Started
1818

19-
- Set base URL, your mnemonic or seed, and Stellar address (if needed) in `scripts/config.json`.
19+
- Set base URL, your mnemonic or seed, keypair type and Stellar address (if needed) in `scripts/config.json`.
2020

2121
## Usage
2222

2323
To run any of the scripts, use the following command format:
2424

2525
```bash
26-
yarn ts-node --project tsconfig-node.json scripts/<script_name>.ts
26+
yarn ts-node --project tsconfig-node.json scripts/<script_name>.ts
2727
```
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"baseUrl": "https://registrar.dev4.grid.tf/v1", "mnemonicOrSeed": "", "stellarAddress": ""}
1+
{"baseUrl": "https://registrar.dev4.grid.tf/v1", "mnemonicOrSeed": "", "stellarAddress": "", "keypairType": "ed25519"}

packages/registrar_client/scripts/create_account.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { log } from "console";
22
import { RegistrarClient } from "../src/";
33
import config from "./config.json";
4+
import { KeypairType } from "@polkadot/util-crypto/types";
45

56
async function createAccount(client: RegistrarClient) {
67
const account = await client.accounts.createAccount({});
@@ -10,7 +11,7 @@ async function createAccount(client: RegistrarClient) {
1011
}
1112

1213
async function main() {
13-
const client = new RegistrarClient({ baseURL: config.baseUrl, mnemonicOrSeed: config.mnemonicOrSeed });
14+
const client = new RegistrarClient({ baseURL: config.baseUrl, mnemonicOrSeed: config.mnemonicOrSeed, keypairType: config.keypairType as KeypairType });
1415
await createAccount(client);
1516
}
1617

packages/registrar_client/scripts/create_farm.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { Account, RegistrarClient } from "../src/";
33
import config from "./config.json";
44
import * as base64 from "base64-js";
55
import { deriveKeyPair } from "../src/utils";
6+
import { KeypairType } from "@polkadot/util-crypto/types";
67

78
async function getAccount(client: RegistrarClient): Promise<Account> {
8-
const keyPair = await deriveKeyPair(config.mnemonicOrSeed);
9+
const keyPair = await deriveKeyPair(config.mnemonicOrSeed, config.keypairType as KeypairType);
910
const account = await client.accounts.getAccountByPublicKey(base64.fromByteArray(keyPair.publicKey));
1011
log("================= Getting Account =================");
1112
log(account);

packages/registrar_client/scripts/create_node.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { Account, RegistrarClient, NodeRegistrationRequest } from "../src/";
33
import config from "./config.json";
44
import * as base64 from "base64-js";
55
import { deriveKeyPair } from "../src/utils";
6+
import { KeypairType } from "@polkadot/util-crypto/types";
67

78
async function getAccount(client: RegistrarClient): Promise<Account> {
8-
const keyPair = await deriveKeyPair(config.mnemonicOrSeed);
9+
const keyPair = await deriveKeyPair(config.mnemonicOrSeed, config.keypairType as KeypairType);
910
const account = await client.accounts.getAccountByPublicKey(base64.fromByteArray(keyPair.publicKey));
1011
log("================= Getting Account =================");
1112
log(account);
@@ -29,7 +30,7 @@ async function getNode(client: RegistrarClient, nodeID: number) {
2930
}
3031

3132
async function main() {
32-
const client = new RegistrarClient({ baseURL: config.baseUrl, mnemonicOrSeed: config.mnemonicOrSeed });
33+
const client = new RegistrarClient({ baseURL: config.baseUrl, mnemonicOrSeed: config.mnemonicOrSeed, keypairType: config.keypairType as KeypairType });
3334
const account = await getAccount(client);
3435
const twinID = account.twin_id;
3536
const node: NodeRegistrationRequest = {

packages/registrar_client/scripts/update_account.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { log } from "console";
22
import { RegistrarClient, UpdateAccountRequest } from "../src/";
33
import config from "./config.json";
4+
import { KeypairType } from "@polkadot/util-crypto/types";
45

56
async function updateAccount(client: RegistrarClient, twinID: number, update: UpdateAccountRequest) {
67
const account = await client.accounts.updateAccount(twinID, update);
@@ -17,7 +18,7 @@ async function getAccount(client: RegistrarClient, twinID: number) {
1718
}
1819

1920
async function main() {
20-
const client = new RegistrarClient({ baseURL: config.baseUrl, mnemonicOrSeed: config.mnemonicOrSeed });
21+
const client = new RegistrarClient({ baseURL: config.baseUrl, mnemonicOrSeed: config.mnemonicOrSeed, keypairType: config.keypairType as KeypairType });
2122
const update: UpdateAccountRequest = {
2223
relays: ["relay1", "relay2"],
2324
rmb_enc_key: "rmb_enc_key",

packages/registrar_client/scripts/update_farm.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { log } from "console";
22
import { RegistrarClient } from "../src/";
33
import config from "./config.json";
4+
import { KeypairType } from "@polkadot/util-crypto/types";
45

56
async function updateFarm(client: RegistrarClient, twinID: number, farmID: number, farmName: string) {
67
const farm = await client.farms.updateFarm(farmID, twinID, farmName, config.stellarAddress);
@@ -17,7 +18,7 @@ async function getFarm(client: RegistrarClient, farmID: number) {
1718
}
1819

1920
async function main() {
20-
const client = new RegistrarClient({ baseURL: config.baseUrl, mnemonicOrSeed: config.mnemonicOrSeed });
21+
const client = new RegistrarClient({ baseURL: config.baseUrl, mnemonicOrSeed: config.mnemonicOrSeed, keypairType: config.keypairType as KeypairType });
2122
const twinID = 143;
2223
const farmID = 46;
2324
const farmName = "testfarm";

packages/registrar_client/scripts/update_node.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { log } from "console";
22
import { UpdateNodeRequest, RegistrarClient } from "../src/";
33
import config from "./config.json";
4+
import { KeypairType } from "@polkadot/util-crypto/types";
45

56
async function updateNode(client: RegistrarClient, twinID: number, nodeID: number, update: UpdateNodeRequest) {
67
const account = await client.nodes.updateNode(nodeID, twinID, update);
@@ -17,7 +18,7 @@ async function getNode(client: RegistrarClient, nodeID: number) {
1718
}
1819

1920
async function main() {
20-
const client = new RegistrarClient({ baseURL: config.baseUrl, mnemonicOrSeed: config.mnemonicOrSeed });
21+
const client = new RegistrarClient({ baseURL: config.baseUrl, mnemonicOrSeed: config.mnemonicOrSeed, keypairType: config.keypairType as KeypairType });
2122
const update: UpdateNodeRequest = {
2223
farm_id: 46,
2324
interfaces: [

packages/registrar_client/src/client/client.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { Accounts } from "../modules/accounts";
33
import { Farms } from "../modules/farms";
44
import { Nodes } from "../modules/nodes";
55
import { Zos } from "../modules/zos";
6-
import {validateMnemonic} from "bip39";
6+
import { validateMnemonic } from "bip39";
7+
import { KeypairType } from "@polkadot/util-crypto/types";
8+
import { SUPPORTED_KEYPAIR_TYPES } from "../utils";
79
export abstract class BaseClient {
810
private client: AxiosInstance;
911

@@ -36,10 +38,12 @@ export abstract class BaseClient {
3638
interface Config {
3739
baseURL: string;
3840
mnemonicOrSeed: string;
41+
keypairType?: KeypairType;
3942
}
4043

4144
export class RegistrarClient extends BaseClient {
4245
public readonly mnemonicOrSeed: string;
46+
public readonly keypairType: KeypairType;
4347
accounts: Accounts;
4448
farms: Farms;
4549
nodes: Nodes;
@@ -55,7 +59,10 @@ export class RegistrarClient extends BaseClient {
5559
return seed;
5660
}
5761

58-
constructor({ baseURL, mnemonicOrSeed }: Config) {
62+
constructor({ baseURL, mnemonicOrSeed, keypairType = "sr25519" }: Config) {
63+
if (!SUPPORTED_KEYPAIR_TYPES.includes(keypairType)) {
64+
throw new Error(`Unsupported keypair type: ${keypairType}`);
65+
}
5966
if (!baseURL) {
6067
throw new Error("Base URL is required");
6168
}
@@ -69,6 +76,7 @@ export class RegistrarClient extends BaseClient {
6976
}
7077

7178
this.mnemonicOrSeed = mnemonicOrSeed;
79+
this.keypairType = keypairType;
7280
this.accounts = new Accounts(this);
7381
this.farms = new Farms(this);
7482
this.nodes = new Nodes(this);

packages/registrar_client/src/modules/accounts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class Accounts {
1212
}
1313

1414
async createAccount(request: Partial<CreateAccountRequest>): Promise<Account | null> {
15-
const { signature, publicKey, timestamp } = await createSignatureWithPublicKey(this.client.mnemonicOrSeed);
15+
const { signature, publicKey, timestamp } = await createSignatureWithPublicKey(this.client.mnemonicOrSeed, this.client.keypairType);
1616

1717
request.public_key = publicKey;
1818
request.signature = signature;
@@ -53,7 +53,7 @@ export class Accounts {
5353

5454
async updateAccount(twinID: number, body: UpdateAccountRequest): Promise<any> {
5555
try {
56-
const headers = await createAuthHeader(twinID, this.client.mnemonicOrSeed);
56+
const headers = await createAuthHeader(twinID, this.client.mnemonicOrSeed, this.client.keypairType);
5757

5858
const data = await this.client.patch<any>(`${this.accountUri}/${twinID}`, body, { headers });
5959
return data;

packages/registrar_client/src/modules/farms.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class Farms {
5252
}
5353

5454
const farm = { farm_name: farmName, dedicated, twin_id: twinID, stellar_address: stellarAddress };
55-
const headers = await createAuthHeader(twinID, this.client.mnemonicOrSeed);
55+
const headers = await createAuthHeader(twinID, this.client.mnemonicOrSeed, this.client.keypairType);
5656
try {
5757
const data = await this.client.post<FarmCreationResponse>(`${this.farmUri}/`, farm, { headers });
5858
return data;
@@ -94,7 +94,7 @@ export class Farms {
9494
throw new Error("Invalid stellar address");
9595
}
9696

97-
const headers = await createAuthHeader(twinID, this.client.mnemonicOrSeed);
97+
const headers = await createAuthHeader(twinID, this.client.mnemonicOrSeed, this.client.keypairType);
9898
const farm : FarmUpdateRequest = { farm_name: name, stellar_address: stellarAddress };
9999
try {
100100
const data = await this.client.patch<any>(`${this.farmUri}/${farmID}`,farm , { headers });

packages/registrar_client/src/modules/nodes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class Nodes {
1919

2020
async registerNode(node: NodeRegistrationRequest): Promise<NodeRegistrationResponse> {
2121
this._validateNodeData(node);
22-
const headers = await createAuthHeader(node.twin_id, this.client.mnemonicOrSeed);
22+
const headers = await createAuthHeader(node.twin_id, this.client.mnemonicOrSeed, this.client.keypairType);
2323
try {
2424
const data = await this.client.post<NodeRegistrationResponse>(`${this.nodeUri}/`, node, { headers });
2525
return data;
@@ -50,7 +50,7 @@ export class Nodes {
5050

5151
async updateNode(nodeID: number, twinID: number, node: UpdateNodeRequest): Promise<any> {
5252
this._validateNodeData(node);
53-
const headers = await createAuthHeader(twinID, this.client.mnemonicOrSeed);
53+
const headers = await createAuthHeader(twinID, this.client.mnemonicOrSeed, this.client.keypairType);
5454
try {
5555
const data = await this.client.patch<Node>(`${this.nodeUri}/${nodeID}`, node, { headers });
5656
return data;
@@ -60,7 +60,7 @@ export class Nodes {
6060
}
6161

6262
async reportNodeUptime(nodeID: number, twinID: number, uptime: UptimeReportRequest): Promise<any> {
63-
const headers = await createAuthHeader(twinID, this.client.mnemonicOrSeed);
63+
const headers = await createAuthHeader(twinID, this.client.mnemonicOrSeed, this.client.keypairType);
6464
try {
6565
const data = await this.client.post<any>(`${this.nodeUri}/${nodeID}/uptime`, uptime, { headers });
6666
return data;

packages/registrar_client/src/utils.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { KeyringPair } from "@polkadot/keyring/types";
77
import { validateMnemonic } from "bip39";
88
import { cryptoWaitReady } from "@polkadot/util-crypto";
99

10-
const SUPPORTED_KEYPAIR_TYPES: KeypairType[] = ["sr25519", "ed25519"];
10+
export const SUPPORTED_KEYPAIR_TYPES: KeypairType[] = ["sr25519", "ed25519"];
1111

1212

1313

@@ -18,7 +18,7 @@ function createSignatureForChallenge(challenge: string, keypair: KeyringPair): s
1818

1919
export async function deriveKeyPair(
2020
mnemonicOrSeed: string,
21-
keypairType: KeypairType = SUPPORTED_KEYPAIR_TYPES[0],
21+
keypairType: KeypairType
2222
): Promise<KeyringPair> {
2323
if (!SUPPORTED_KEYPAIR_TYPES.includes(keypairType)) {
2424
throw new Error(`Unsupported keypair type: ${keypairType}`);
@@ -35,17 +35,18 @@ export async function deriveKeyPair(
3535

3636
export async function createSignatureWithPublicKey(
3737
mnemonicOrSeed: string,
38+
keypairType: KeypairType ,
3839
): Promise<{ signature: string; publicKey: string; timestamp: number }> {
39-
const keypair = await deriveKeyPair(mnemonicOrSeed);
40+
const keypair = await deriveKeyPair(mnemonicOrSeed, keypairType);
4041
const publicKey = base64.fromByteArray(keypair.publicKey);
4142
const timestamp = Math.floor(Date.now() / 1000);
4243
const challenge = `${timestamp}:${publicKey}`;
4344
const signature = createSignatureForChallenge(challenge, keypair);
4445
return { signature, publicKey, timestamp };
4546
}
4647

47-
export async function createAuthHeader(twinID: number, mnemonicOrSeed: string): Promise<AxiosRequestConfig["headers"]> {
48-
const keypair = await deriveKeyPair(mnemonicOrSeed);
48+
export async function createAuthHeader(twinID: number, mnemonicOrSeed: string, keypairType: KeypairType): Promise<AxiosRequestConfig["headers"]> {
49+
const keypair = await deriveKeyPair(mnemonicOrSeed, keypairType);
4950
const timestamp = Math.floor(Date.now() / 1000);
5051
const challenge = `${timestamp}:${twinID}`;
5152
const signature = createSignatureForChallenge(challenge, keypair);

0 commit comments

Comments
 (0)