Skip to content

Commit a207727

Browse files
committed
wip bip340 key tweaking
1 parent aa4cade commit a207727

File tree

9 files changed

+77
-37
lines changed

9 files changed

+77
-37
lines changed

demo/redirect-flow-example/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/redirect-flow-example/src/App.tsx

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import{ tssLib as tssLibFrostBip340 } from "@toruslabs/tss-frost-lib-bip340";
2626
import "./App.css";
2727
import jwt, { Algorithm } from "jsonwebtoken";
2828
import { flow } from "./flow";
29+
import { ec as EC } from "elliptic";
2930

3031
type TssLib = typeof tssLibDkls | typeof tssLibFrost | typeof tssLibFrostBip340;
3132

@@ -401,16 +402,29 @@ function App() {
401402
uiConsole(sig.toString("hex"));
402403
}
403404
};
405+
406+
const signWithKeyTweak = async (): Promise<any> => {
407+
if (coreKitInstance.sigType === SIG_TYPE.ECDSA_SECP256K1) {
408+
throw new Error("Not supported for this signature type");
409+
} else if (coreKitInstance.sigType === SIG_TYPE.ED25519 || coreKitInstance.sigType === SIG_TYPE.BIP340) {
410+
const msg = Buffer.from("hello signer!");
411+
const keyTweak = (() => {
412+
const ec = new EC(coreKitInstance.keyType);
413+
return ec.genKeyPair().getPrivate();
414+
})();
415+
const sig = await coreKitInstance.sign(msg, { keyTweak });
416+
uiConsole(sig.toString("hex"));
417+
}
418+
};
419+
404420
const signMessageWithPrecomputedTss = async (): Promise<any> => {
405421
if (coreKitInstance.keyType === "secp256k1") {
406422
const precomputedTssClient = await coreKitInstance.precompute_secp256k1();
407423
const msg = Buffer.from("hello signer!");
408-
const sig = await coreKitInstance.sign(msg, false, precomputedTssClient);
409-
uiConsole(sig.toString("hex"));
410-
} else if (coreKitInstance.keyType === "ed25519") {
411-
const msg = Buffer.from("hello signer!");
412-
const sig = await coreKitInstance.sign(msg);
424+
const sig = await coreKitInstance.sign(msg, { secp256k1Precompute: precomputedTssClient });
413425
uiConsole(sig.toString("hex"));
426+
} else {
427+
throw new Error("Not supported for this key type");
414428
}
415429
};
416430

@@ -419,15 +433,13 @@ function App() {
419433
const [precomputedTssClient, precomputedTssClient2] = await Promise.all([coreKitInstance.precompute_secp256k1(), coreKitInstance.precompute_secp256k1()]);
420434

421435
const msg = Buffer.from("hello signer!");
422-
const sig = await coreKitInstance.sign(msg, false, precomputedTssClient);
436+
const sig = await coreKitInstance.sign(msg, { secp256k1Precompute: precomputedTssClient });
423437
const msg2 = Buffer.from("hello signer2!");
424438

425-
const sig2 = await coreKitInstance.sign(msg2, false, precomputedTssClient2);
439+
const sig2 = await coreKitInstance.sign(msg2, { secp256k1Precompute: precomputedTssClient2 });
426440
uiConsole("Sig1: ", sig.toString("hex"), "Sig2: ", sig2.toString("hex"));
427-
} else if (coreKitInstance.keyType === "ed25519") {
428-
const msg = Buffer.from("hello signer!");
429-
const sig = await coreKitInstance.sign(msg);
430-
uiConsole(sig.toString("hex"));
441+
} else {
442+
throw new Error("Not supported for this key type");
431443
}
432444
};
433445
const switchChainSepolia = async () => {
@@ -746,8 +758,12 @@ function App() {
746758
Sign Message
747759
</button>
748760

761+
<button onClick={signWithKeyTweak} className="card">
762+
Sign with Key Tweak
763+
</button>
764+
749765
<button onClick={signMessageWithPrecomputedTss} className="card">
750-
Sign Msgwith precomputed TSS
766+
Sign with precomputed TSS
751767
</button>
752768

753769
<button onClick={signMultipleMessagesWithPrecomputedTss} className="card">

package-lock.json

Lines changed: 22 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
"@toruslabs/session-manager": "^3.1.0",
6363
"@toruslabs/torus.js": "^15.1.1",
6464
"@toruslabs/tss-client": "^3.3.0-alpha.0",
65-
"@toruslabs/tss-frost-client": "^1.0.0-alpha.0",
66-
"@toruslabs/tss-frost-common": "^1.0.1",
65+
"@toruslabs/tss-frost-client": "file:toruslabs-tss-frost-client-1.0.0-alpha.1.tgz",
66+
"@toruslabs/tss-frost-common": "file:toruslabs-tss-frost-common-1.1.0-alpha.0.tgz",
6767
"bn.js": "^5.2.1",
6868
"bowser": "^2.11.0",
6969
"elliptic": "^6.5.7",

src/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ export type SigType = WEB3AUTH_SIG_TYPE;
463463
export interface CoreKitSigner {
464464
keyType: KeyType;
465465
sigType: SigType;
466-
sign(data: Buffer, hashed?: boolean): Promise<Buffer>;
466+
sign(data: Buffer, opts?: { hashed?: boolean }): Promise<Buffer>;
467467
getPubKey(): Buffer;
468468
}
469469

src/mpcCoreKit.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -768,13 +768,27 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
768768
};
769769
}
770770

771-
public async sign(data: Buffer, hashed: boolean = false, secp256k1Precompute?: Secp256k1PrecomputedClient): Promise<Buffer> {
771+
public async sign(
772+
data: Buffer,
773+
opts?: {
774+
hashed?: boolean;
775+
secp256k1Precompute?: Secp256k1PrecomputedClient;
776+
keyTweak?: BN;
777+
}
778+
): Promise<Buffer> {
772779
this.wasmLib = await this.loadTssWasm();
773780
if (this._sigType === "ecdsa-secp256k1") {
774-
const sig = await this.sign_ECDSA_secp256k1(data, hashed, secp256k1Precompute);
781+
if (opts?.keyTweak) {
782+
throw CoreKitError.default("key tweaking not supported for ecdsa-secp256k1");
783+
}
784+
const sig = await this.sign_ECDSA_secp256k1(data, opts?.hashed, opts?.secp256k1Precompute);
775785
return Buffer.concat([sig.r, sig.s, Buffer.from([sig.v])]);
776786
} else if (this._sigType === "ed25519" || this._sigType === "bip340") {
777-
return this.sign_frost(data, hashed);
787+
if (opts?.hashed) {
788+
throw CoreKitError.default(`hashed data not supported for bip340`);
789+
}
790+
791+
return this.sign_frost(data, opts?.keyTweak);
778792
}
779793
throw CoreKitError.default(`sign not supported for key type ${this.keyType}`);
780794
}
@@ -1407,11 +1421,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
14071421
}
14081422
}
14091423

1410-
private async sign_frost(data: Buffer, hashed: boolean = false): Promise<Buffer> {
1411-
if (hashed) {
1412-
throw CoreKitError.default(`hashed data not supported for ${this._sigType}`);
1413-
}
1414-
1424+
private async sign_frost(data: Buffer, keyTweak?: BN): Promise<Buffer> {
14151425
const nodeDetails = fetchLocalConfig(this.options.web3AuthNetwork, this.keyType, this._sigType);
14161426
if (!nodeDetails.torusNodeTSSEndpoints) {
14171427
throw CoreKitError.default("could not fetch tss node endpoints");
@@ -1439,7 +1449,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
14391449
const { serverCoefficients, clientCoefficient } = deriveShareCoefficients(ec, serverXCoords, clientXCoord, this.state.tssShareIndex);
14401450

14411451
// Get pub key.
1442-
const tssPubKey = await this.getPubKey();
1452+
const tssPubKey = this.getPubKey();
14431453
const tssPubKeyPoint = ec.keyFromPublic(tssPubKey).getPublic();
14441454

14451455
// Get client key share and adjust by coefficient.
@@ -1469,7 +1479,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
14691479
clientShareAdjustedHex,
14701480
pubKeyHex,
14711481
data,
1472-
serverCoefficientsHex
1482+
serverCoefficientsHex,
1483+
keyTweak?.toString("hex")
14731484
);
14741485

14751486
log.info(`signature: ${signature}`);

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export function makeEthereumSigner(kit: CoreKitSigner): EthereumSigner {
201201
}
202202
return {
203203
sign: async (msgHash: Buffer) => {
204-
const sig = await kit.sign(msgHash, true);
204+
const sig = await kit.sign(msgHash, { hashed: true });
205205
return sigToRSV(sig);
206206
},
207207
getPublic: async () => {
72.4 KB
Binary file not shown.
4.37 KB
Binary file not shown.

0 commit comments

Comments
 (0)