Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle update twin w seed #224

Merged
merged 9 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions packages/grid_client/src/clients/tf-grid/twins.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as secp from "@noble/secp256k1";
import * as bip39 from "bip39";
import { generatePublicKey } from "@threefold/rmb_direct_client";

import { TFClient } from "./client";

Expand All @@ -10,21 +9,13 @@ class Twins {
this.tfclient = client;
}

getPublicKey(mnemonic: string) {
const seed = bip39.mnemonicToSeedSync(mnemonic);
const privKey = new Uint8Array(seed).slice(0, 32);
const pk = "0x" + Buffer.from(secp.getPublicKey(privKey, true)).toString("hex");
return pk;
}

async create(relay: string) {
const pk = this.getPublicKey(this.tfclient.mnemonic);

const pk = generatePublicKey(this.tfclient.mnemonic);
return this.tfclient.applyExtrinsic(this.tfclient.client.createTwin, [relay, pk], "tfgridModule", ["TwinStored"]);
}

async update(relay: string) {
const pk = this.getPublicKey(this.tfclient.mnemonic);
const pk = generatePublicKey(this.tfclient.mnemonic);

return this.tfclient.applyExtrinsic(this.tfclient.client.updateTwin, [relay, pk], "tfgridModule", ["TwinUpdated"]);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/rmb_direct_client/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./client";
export * from "./util";
31 changes: 26 additions & 5 deletions packages/rmb_direct_client/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,32 @@ export async function createGridCL(chainUrl: string) {
return cl;
}

export function generatePublicKey(mnemonic: string) {
const seed = bip39.mnemonicToSeedSync(mnemonic);
const privKey = new Uint8Array(seed).slice(0, 32);
const pk = "0x" + Buffer.from(secp.getPublicKey(privKey, true)).toString("hex");
return pk;
function isValidSeed(seed: string) {
const hexRegex = /^[0-9a-fA-F]+$/;
return hexRegex.test(seed) ? true : false;
}

function getPublicKey(privateKey: Buffer | string) {
return "0x" + Buffer.from(secp.getPublicKey(privateKey, true)).toString("hex");
}

export function generatePublicKey(secret: string) {
let privKey;

if (bip39.validateMnemonic(secret)) {
const seed = bip39.mnemonicToSeedSync(secret);
privKey = new Uint8Array(seed).slice(0, 32);
}

privKey = secret;
if (secret.startsWith("0x")) {
privKey = secret.substring(2);
}

if (!isValidSeed(privKey) || privKey.length !== 32) {
throw new Error("Invalid seed. Couldn't get public key from the provided seed.");
}
return getPublicKey(privKey);
}

export async function setPublicKey(
Expand Down
10 changes: 5 additions & 5 deletions packages/rmb_direct_client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"homepage": "https://github.com/threefoldtech/tfgrid-sdk-ts/blob/development/packages/rmb_direct_client/README.md",
"author": "amiraabouhadid <amiraabouhadid@gmail.com>",
"license": "MIT",
"types": "dist/es6/client.d.ts",
"files": [
"/dist"
],
Expand Down Expand Up @@ -46,10 +45,11 @@
"ts-node": "^10.9.1",
"typescript": "*"
},
"main": "./dist/node/client.js",
"module": "./dist/es6/client.js",
"main": "./dist/node/index.js",
"module": "./dist/es6/index.js",
"types": "dist/es6/index.d.ts",
"exports": {
"require": "./dist/node/client.js",
"import": "./dist/es6/client.js"
"require": "./dist/node/index.js",
"import": "./dist/es6/index.js"
}
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17882,4 +17882,4 @@ zen-observable-ts@^0.8.21:
zen-observable@^0.8.0:
version "0.8.15"
resolved "https://registry.yarnpkg.com/zen-observable/-/zen-observable-0.8.15.tgz#96415c512d8e3ffd920afd3889604e30b9eaac15"
integrity sha512-PQ2PC7R9rslx84ndNBZB/Dkv8V8fZEpk83RLgXtYd0fwUgEjseMn1Dgajh2x6S8QbZAFa9p2qVCEuYZNgve0dQ==
integrity sha512-PQ2PC7R9rslx84ndNBZB/Dkv8V8fZEpk83RLgXtYd0fwUgEjseMn1Dgajh2x6S8QbZAFa9p2qVCEuYZNgve0dQ==