import { PrivateKey } from "@bsv/sdk"; import { describe, expect, test } from "vitest"; import { createIdentity, getIdKey, signOpReturn } from "../src/identity"; describe("BAP Identity", () => { const testWif = PrivateKey.fromRandom().toWif(); test("createIdentity returns an identity object", () => { const identity = createIdentity(testWif); expect(identity).toBeDefined(); }); test("getIdKey returns a string BAP identity key", () => { const identity = createIdentity(testWif); const idKey = getIdKey(identity); expect(typeof idKey).toBe("string"); expect(idKey.length).toBeGreaterThan(0); }); test("signOpReturn produces AIP signature data", () => { const identity = createIdentity(testWif); // Example OP_RETURN data (hex encoded arrays) const opReturnData: number[][] = [ Buffer.from("1PuQa7K62MiKCtssSLKy1kh56WWU7MtUR5").toJSON().data, // MAP prefix Buffer.from("SET").toJSON().data, Buffer.from("app").toJSON().data, Buffer.from("clawbook").toJSON().data, ]; const signedData = signOpReturn(identity, opReturnData); expect(Array.isArray(signedData)).toBe(true); expect(signedData.length).toBeGreaterThan(opReturnData.length); // AIP signature format: [...original data, "|", AIP_PREFIX, "BITCOIN_ECDSA", address, signature] const pipeHex = Buffer.from("|").toString("hex"); const aipPrefixHex = Buffer.from( "15PciHG22SNLQJXMoSUaWVi7WSqc7hCfva", ).toString("hex"); // Check for pipe separator const pipeElementHex = Buffer.from( signedData[opReturnData.length], ).toString("hex"); expect(pipeElementHex).toBe(pipeHex); // Check that AIP prefix follows pipe const aipElementHex = Buffer.from( signedData[opReturnData.length + 1], ).toString("hex"); expect(aipElementHex).toBe(aipPrefixHex); }); test("identity is deterministic (same WIF = same idKey)", () => { const wif = PrivateKey.fromRandom().toWif(); const identity1 = createIdentity(wif); const identity2 = createIdentity(wif); const idKey1 = getIdKey(identity1); const idKey2 = getIdKey(identity2); expect(idKey1).toBe(idKey2); }); });