|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import { DappTransport } from '../src/DappTransport.js' |
| 4 | +import { TransportMode } from '../src/types/index.js' |
| 5 | + |
| 6 | +const encodeBase64Utf8 = (value: string) => { |
| 7 | + let binary = '' |
| 8 | + for (const byte of new TextEncoder().encode(value)) { |
| 9 | + binary += String.fromCharCode(byte) |
| 10 | + } |
| 11 | + return btoa(binary) |
| 12 | +} |
| 13 | + |
| 14 | +const decodeBase64Utf8 = (value: string) => { |
| 15 | + const binary = atob(value) |
| 16 | + const bytes = new Uint8Array(binary.length) |
| 17 | + for (let i = 0; i < binary.length; i += 1) { |
| 18 | + bytes[i] = binary.charCodeAt(i) |
| 19 | + } |
| 20 | + return new TextDecoder().decode(bytes) |
| 21 | +} |
| 22 | + |
| 23 | +const createSessionStorage = () => { |
| 24 | + const values = new Map<string, string>() |
| 25 | + return { |
| 26 | + getItem: (key: string) => values.get(key) ?? null, |
| 27 | + setItem: (key: string, value: string) => { |
| 28 | + values.set(key, value) |
| 29 | + }, |
| 30 | + removeItem: (key: string) => { |
| 31 | + values.delete(key) |
| 32 | + }, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +describe('DappTransport redirect URLs', () => { |
| 37 | + it('encodes unicode payloads as UTF-8 base64', async () => { |
| 38 | + const transport = new DappTransport('https://wallet.example', TransportMode.REDIRECT, {}, createSessionStorage()) |
| 39 | + const payload = { message: 'Sign in to Sequence 🌍' } |
| 40 | + |
| 41 | + const redirectUrl = await transport.getRequestRedirectUrl('signMessage', payload, 'https://dapp.example/callback') |
| 42 | + const encodedPayload = new URL(redirectUrl).searchParams.get('payload') |
| 43 | + |
| 44 | + if (!encodedPayload) { |
| 45 | + throw new Error('Expected redirect URL to include a payload') |
| 46 | + } |
| 47 | + expect(JSON.parse(decodeBase64Utf8(encodedPayload))).toEqual(payload) |
| 48 | + }) |
| 49 | + |
| 50 | + it('decodes unicode redirect response payloads', async () => { |
| 51 | + const storage = createSessionStorage() |
| 52 | + const transport = new DappTransport('https://wallet.example', TransportMode.REDIRECT, {}, storage) |
| 53 | + const requestUrl = await transport.getRequestRedirectUrl('signMessage', {}, 'https://dapp.example/callback') |
| 54 | + const id = new URL(requestUrl).searchParams.get('id') |
| 55 | + const payload = { message: 'Signed by Sequence 🌍' } |
| 56 | + const responseUrl = new URL('https://dapp.example/callback') |
| 57 | + |
| 58 | + if (!id) { |
| 59 | + throw new Error('Expected redirect URL to include an id') |
| 60 | + } |
| 61 | + responseUrl.searchParams.set('id', id) |
| 62 | + responseUrl.searchParams.set('payload', encodeBase64Utf8(JSON.stringify(payload))) |
| 63 | + |
| 64 | + await expect(transport.getRedirectResponse(false, responseUrl.toString())).resolves.toEqual({ |
| 65 | + action: 'signMessage', |
| 66 | + payload, |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + it('decodes legacy Latin-1 redirect response payloads', async () => { |
| 71 | + const storage = createSessionStorage() |
| 72 | + const transport = new DappTransport('https://wallet.example', TransportMode.REDIRECT, {}, storage) |
| 73 | + const requestUrl = await transport.getRequestRedirectUrl('signMessage', {}, 'https://dapp.example/callback') |
| 74 | + const id = new URL(requestUrl).searchParams.get('id') |
| 75 | + const payload = { message: 'Signed by Sequence Café' } |
| 76 | + const responseUrl = new URL('https://dapp.example/callback') |
| 77 | + |
| 78 | + if (!id) { |
| 79 | + throw new Error('Expected redirect URL to include an id') |
| 80 | + } |
| 81 | + responseUrl.searchParams.set('id', id) |
| 82 | + responseUrl.searchParams.set('payload', btoa(JSON.stringify(payload))) |
| 83 | + |
| 84 | + await expect(transport.getRedirectResponse(false, responseUrl.toString())).resolves.toEqual({ |
| 85 | + action: 'signMessage', |
| 86 | + payload, |
| 87 | + }) |
| 88 | + }) |
| 89 | +}) |
0 commit comments