Skip to content

Commit e7dd76b

Browse files
committed
feat: add tests
convert buf to array, add default error message
1 parent f5a838f commit e7dd76b

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/mpcCoreKit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,9 +786,9 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
786786

787787
public async sign(data: Buffer, hashed: boolean = false, secp256k1Precompute?: Secp256k1PrecomputedClient): Promise<Buffer> {
788788
if (this.preSigningValidator) {
789-
const result = await this.preSigningValidator({ data, hashed });
789+
const result = await this.preSigningValidator({ data: Uint8Array.from(data), hashed });
790790
if (!result.success || result.error) {
791-
throw Error(result.error);
791+
throw Error(result.error || "preSigningValidator failed");
792792
}
793793
}
794794
this.wasmLib = await this.loadTssWasm();

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.setPreSigningValidator(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.setPreSigningValidator(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)