UNPKG

@gemini-wallet/core

Version:

Core SDK for Gemini Wallet integration with popup communication

86 lines (63 loc) 3.54 kB
import { describe, expect, test } from "bun:test"; import { 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({ publicKey, credentialId, }); const expectedAddress = "0xce97D39F2c1f19d0F3B44f735Cd7A8a6FB29F9E3"; console.log("Function API - Calculated:", calculatedAddress); console.log("Function API - Expected:", expectedAddress); 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({ publicKey: "0xinvalid", credentialId: "test", }); }).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({ publicKey, credentialId }); const address2 = calculateWalletAddress({ publicKey, credentialId, index: 0n }); expect(address1).toBe(address2); }); test("should calculate address for second test wallet", () => { const publicKey = "0x69933403b13f813f8417b5ef0716f39151dd58702aead4f7e991b5fb80bc868f54baf92948c91613d52a891534927c10a4b6b19bbffef9815459ebd77ea690a6" as const; const credentialId = "2X4LvYKqkmbs89vIzAMcOFtw58y4uBIjWRMZUlJ43zc"; const calculatedAddress = calculateWalletAddress({ publicKey, credentialId, }); const expectedAddress = "0x3B3CA0de38c7aa794775E183f4A0D428251d6781"; console.log("Second wallet - Calculated:", calculatedAddress); console.log("Second wallet - Expected:", expectedAddress); expect(calculatedAddress.toLowerCase()).toBe(expectedAddress.toLowerCase()); }); });