import { describe, it, expect } from 'vitest' import { reviveBip122Account } from './BridgeChild' /** * The child half of the bip122 account wire contract. * * The parent flattens accounts to plain data (`toBip122WireAccount`) because * structured clone drops the wallet's prototype accessors. This rebuilds the * `Uint8Array` the injected adapter expects, so a framed adapter sees the same * shape as a direct one and nothing downstream has to know which it is. */ const ADDRESS = 'bc1qjxztncefrl5czfyfxd449t8tht634g9cnaz5ka' type Revived = { address?: string; publicKey?: Uint8Array; purpose?: string } describe('reviveBip122Account', () => { it('rebuilds publicKey as a Uint8Array from the wire array', () => { const revived = reviveBip122Account({ address: ADDRESS, publicKey: [3, 65, 234, 69] }) as Revived // `bytesToHex` in the adapter iterates this — a plain array is what threw // "bytes is not iterable" before the wire contract existed. expect(revived.publicKey).toBeInstanceOf(Uint8Array) expect(Array.from(revived.publicKey!)).toEqual([3, 65, 234, 69]) }) it('preserves the other fields the adapter reads', () => { const revived = reviveBip122Account({ address: ADDRESS, publicKey: [1, 2], purpose: 'payment' }) as Revived expect(revived.address).toBe(ADDRESS) expect(revived.purpose).toBe('payment') }) it('leaves an already-typed publicKey untouched', () => { // Same-realm callers (and any future transport that preserves typed // arrays) must not pay for a needless copy. const publicKey = Uint8Array.from([9, 9]) const revived = reviveBip122Account({ address: ADDRESS, publicKey }) as Revived expect(revived.publicKey).toBe(publicKey) }) it('yields an empty Uint8Array when publicKey is absent', () => { const revived = reviveBip122Account({ address: ADDRESS }) as Revived expect(revived.publicKey).toBeInstanceOf(Uint8Array) expect(revived.publicKey).toHaveLength(0) }) it('hands back an unmarshaled account untouched so it keeps failing loudly', () => { // An older parent (post-#237, pre-marshaling-fix) sends the wallet's own // account objects, which structured clone empties to `{}`. Reviving one // would invent `publicKey: Uint8Array []`, which the adapter hexes to `''` // and accepts — committing a session with `address: undefined`. That // pairing is broken regardless; it must stay a loud `bytesToHex` throw // rather than a silent address mismatch downstream. const flattened = {} expect(reviveBip122Account(flattened)).toBe(flattened) expect( (reviveBip122Account(flattened) as Revived).publicKey ).toBeUndefined() }) it('passes non-object values straight through', () => { // Guards the map over a malformed accounts array: a stray null must not // become `{ publicKey: Uint8Array [] }`, which would look like a real // account to callers. expect(reviveBip122Account(null)).toBeNull() expect(reviveBip122Account(undefined)).toBeUndefined() }) it('round-trips the exact shape the parent puts on the wire', () => { // Mirrors uwc-bridge-parent's `toBip122WireAccount` output, through a real // structured clone, so the two halves are proven to agree. const wire = structuredClone({ address: ADDRESS, publicKey: [3, 65, 234, 69, 78], purpose: 'payment' }) const revived = reviveBip122Account(wire) as Revived expect(revived.address).toBe(ADDRESS) expect(revived.purpose).toBe('payment') expect(revived.publicKey).toBeInstanceOf(Uint8Array) expect(Array.from(revived.publicKey!)).toEqual([3, 65, 234, 69, 78]) }) })