@gemini-wallet/core
Version:
Core SDK for Gemini Wallet integration with popup communication
125 lines (94 loc) • 4.46 kB
text/typescript
import { describe, expect, test } from "bun:test";
import {
calculateV1Address,
calculateWalletAddress,
generateAuthenticatorIdHash,
validateWebAuthnKey,
} from "./calculateWalletAddress";
describe("calculateWalletAddress", () => {
test("should calculate exact wallet address", () => {
const publicKey =
"0x900fb1e17b7766916a8dad6f8a26b3dbc4fe4f9b1ea5f2d20b7cb31e44c5ff54e63df1865b444a4e7b74a33ef8e3a269f77a6ba5afd072fc641ad5c7f9d626c7" as const;
const credentialId = "XJ980eHLIRtTop-iX4-wAtSUQ-GxPv_6JIprPE2nN-RBgfJKZPWEWzC-amiRxzfjpks_7q7A8Q";
const calculatedAddress = calculateWalletAddress({
credentialId,
publicKey,
});
const expectedAddress = "0xb5E764Ddf8dD5A3613779132E95f389328149b0C";
expect(calculatedAddress.toLowerCase()).toBe(expectedAddress.toLowerCase());
});
test("should calculate V1 wallet address", () => {
const publicKey =
"0x900fb1e17b7766916a8dad6f8a26b3dbc4fe4f9b1ea5f2d20b7cb31e44c5ff54e63df1865b444a4e7b74a33ef8e3a269f77a6ba5afd072fc641ad5c7f9d626c7" as const;
const credentialId = "XJ980eHLIRtTop-iX4-wAtSUQ-GxPv_6JIprPE2nN-RBgfJKZPWEWzC-amiRxzfjpks_7q7A8Q";
const calculatedAddress = calculateV1Address({
credentialId,
publicKey,
});
const expectedAddress = "0xce97D39F2c1f19d0F3B44f735Cd7A8a6FB29F9E3";
expect(calculatedAddress.toLowerCase()).toBe(expectedAddress.toLowerCase());
});
test("should validate WebAuthn keys correctly", () => {
const publicKey =
"0x900fb1e17b7766916a8dad6f8a26b3dbc4fe4f9b1ea5f2d20b7cb31e44c5ff54e63df1865b444a4e7b74a33ef8e3a269f77a6ba5afd072fc641ad5c7f9d626c7";
const pubKeyX = `0x${publicKey.slice(2, 66)}`;
const pubKeyY = `0x${publicKey.slice(66, 130)}`;
const webAuthnData = {
pubKeyX: BigInt(pubKeyX),
pubKeyY: BigInt(pubKeyY),
};
const isValid = validateWebAuthnKey(webAuthnData);
expect(isValid).toBe(true);
console.log("WebAuthn key validation passed");
});
test("should generate correct authenticator ID hash", () => {
const credentialId = "XJ980eHLIRtTop-iX4-wAtSUQ-GxPv_6JIprPE2nN-RBgfJKZPWEWzC-amiRxzfjpks_7q7A8Q";
const hash = generateAuthenticatorIdHash(credentialId);
const expectedHash = "0xa919a485eff73c853844904a444f102f42d302320d3fee7c64136b0f4ef8357c";
console.log("Generated hash:", hash);
console.log("Expected hash:", expectedHash);
expect(hash.toLowerCase()).toBe(expectedHash.toLowerCase());
});
test("should throw error for invalid public key", () => {
expect(() => {
calculateWalletAddress({
credentialId: "test",
publicKey: "0xinvalid",
});
}).toThrow("Invalid public key: must be 64-byte hex string (0x + 128 chars)");
});
test("should use default index of 0", () => {
const publicKey =
"0x900fb1e17b7766916a8dad6f8a26b3dbc4fe4f9b1ea5f2d20b7cb31e44c5ff54e63df1865b444a4e7b74a33ef8e3a269f77a6ba5afd072fc641ad5c7f9d626c7" as const;
const credentialId = "XJ980eHLIRtTop-iX4-wAtSUQ-GxPv_6JIprPE2nN-RBgfJKZPWEWzC-amiRxzfjpks_7q7A8Q";
const address1 = calculateWalletAddress({ credentialId, publicKey });
const address2 = calculateWalletAddress({
credentialId,
index: 0n,
publicKey,
});
expect(address1).toBe(address2);
});
test("should calculate address for second test wallet", () => {
const publicKey =
"0x69933403b13f813f8417b5ef0716f39151dd58702aead4f7e991b5fb80bc868f54baf92948c91613d52a891534927c10a4b6b19bbffef9815459ebd77ea690a6" as const;
const credentialId = "2X4LvYKqkmbs89vIzAMcOFtw58y4uBIjWRMZUlJ43zc";
const calculatedAddress = calculateWalletAddress({
credentialId,
publicKey,
});
const expectedAddress = "0xdd294FD857f00E533aa9bCBfDd49c76c842238F0";
expect(calculatedAddress.toLowerCase()).toBe(expectedAddress.toLowerCase());
});
test("should calculate V1 address for second test wallet", () => {
const publicKey =
"0x69933403b13f813f8417b5ef0716f39151dd58702aead4f7e991b5fb80bc868f54baf92948c91613d52a891534927c10a4b6b19bbffef9815459ebd77ea690a6" as const;
const credentialId = "2X4LvYKqkmbs89vIzAMcOFtw58y4uBIjWRMZUlJ43zc";
const calculatedAddress = calculateV1Address({
credentialId,
publicKey,
});
const expectedAddress = "0x3B3CA0de38c7aa794775E183f4A0D428251d6781";
expect(calculatedAddress.toLowerCase()).toBe(expectedAddress.toLowerCase());
});
});