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 4 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
1 change: 1 addition & 0 deletions packages/rmb_direct_client/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,4 @@ class Client {
}
}
export { Client };
export * from "./util";
AhmedHanafy725 marked this conversation as resolved.
Show resolved Hide resolved
45 changes: 41 additions & 4 deletions packages/rmb_direct_client/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,47 @@ 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");
function isValidHex(hex: string) {
const hexRegex = /^[0-9a-fA-F]+$/;
if (hexRegex.test(hex)) {
return true;
} else {
false;
AhmedHanafy725 marked this conversation as resolved.
Show resolved Hide resolved
}
}

function getPublicKeyFromHexOrFalse(seed: string) {
if (!isValidHex(seed)) {
return false;
}
try {
const pk = "0x" + Buffer.from(secp.getPublicKey(seed, true)).toString("hex");
return pk;
} catch (error) {
return false;
}
}
AhmedHanafy725 marked this conversation as resolved.
Show resolved Hide resolved

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

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

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

if (bip39.validateMnemonic(secret)) {
const seed = bip39.mnemonicToSeedSync(secret);
privKey = new Uint8Array(seed).slice(0, 32);
return getPublicKeyFromBuffer(privKey);
}
const pk = getPublicKeyFromHexOrFalse(secret);
AhmedHanafy725 marked this conversation as resolved.
Show resolved Hide resolved
if (!pk) {
throw new Error("Invalid seed. Couldn't get public key from the provided seed.");
}
return pk;
}

Expand Down
Loading