Skip to content

Commit 3aceec7

Browse files
Merge pull request #214 from Web3Auth/feat/preSigningValidator
Feat/pre signing hook
2 parents 3c625fd + 6e4a72a commit 3aceec7

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/interfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,5 @@ export interface Secp256k1PrecomputedClient {
495495
client: Client;
496496
serverCoeffs: Record<string, string>;
497497
}
498+
499+
export type PreSigningHookType = (params: { data: Uint8Array; hashed: boolean }) => Promise<{ success: boolean; error?: string; data?: string }>;

src/mpcCoreKit.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
JWTLoginParams,
4444
MPCKeyDetails,
4545
OAuthLoginParams,
46+
PreSigningHookType,
4647
Secp256k1PrecomputedClient,
4748
SessionData,
4849
SubVerifierDetailsParams,
@@ -99,6 +100,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
99100

100101
private atomicCallStackCounter: number = 0;
101102

103+
private preSigningHook?: PreSigningHookType;
104+
102105
constructor(options: Web3AuthOptions) {
103106
if (!options.web3AuthClientId) {
104107
throw CoreKitError.clientIdInvalid();
@@ -456,6 +459,10 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
456459
}
457460
}
458461

462+
setPreSigningHook(preSigningHook: PreSigningHookType) {
463+
this.preSigningHook = preSigningHook;
464+
}
465+
459466
public async handleRedirectResult(): Promise<void> {
460467
this.checkReady();
461468

@@ -778,6 +785,12 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
778785
}
779786

780787
public async sign(data: Buffer, hashed: boolean = false, secp256k1Precompute?: Secp256k1PrecomputedClient): Promise<Buffer> {
788+
if (this.preSigningHook) {
789+
const result = await this.preSigningHook({ data: Uint8Array.from(data), hashed });
790+
if (!result.success || result.error) {
791+
throw Error(result.error || "preSigningValidator failed");
792+
}
793+
}
781794
this.wasmLib = await this.loadTssWasm();
782795
if (this.keyType === KeyType.secp256k1) {
783796
const sig = await this.sign_ECDSA_secp256k1(data, hashed, secp256k1Precompute);

tests/login.spec.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import assert from "node:assert";
1+
import assert, { fail } from "node:assert";
22
import test from "node:test";
33

44
import { EllipticPoint } from "@tkey/common-types";
@@ -166,8 +166,40 @@ variable.forEach((testVariable) => {
166166
const signature2 = sigToRSV(await coreKitInstance.sign(msgBuffer));
167167
const pubkey2 = secp256k1.recoverPubKey(msgHash, signature2, signature2.v) as EllipticPoint;
168168
assert(pubkey2.eq(publicKeyPoint));
169+
170+
// should fail to sign due to preSignValidation
171+
{
172+
coreKitInstance.setPreSigningHook(async ({ data }) => {
173+
return {
174+
success: false,
175+
data: Buffer.from(data).toString("hex")
176+
}
177+
});
178+
179+
try {
180+
await coreKitInstance.sign(msgHash, true);
181+
fail("Should fail signing")
182+
} catch (err) {
183+
assert(err);
184+
}
185+
};
186+
187+
// should succeed to sign
188+
{
189+
coreKitInstance.setPreSigningHook(async ({ data }) => {
190+
return {
191+
success: true,
192+
data: Buffer.from(data).toString("hex")
193+
}
194+
});
195+
const signature = sigToRSV(await coreKitInstance.sign(msgHash, true));
196+
const pubkey = secp256k1.recoverPubKey(msgHash, signature, signature.v) as EllipticPoint;
197+
const publicKeyPoint = bufferToElliptic(coreKitInstance.getPubKey());
198+
assert(pubkey.eq(publicKeyPoint));
199+
};
169200
});
170201

202+
171203
await t.test("#Login and sign with different account/wallet index", async function () {
172204
const vid = stringGen(10);
173205
const coreKitInstance = newCoreKitInstance();

0 commit comments

Comments
 (0)