|
| 1 | +// Copyright (c) 2018-2025 Coinbase, Inc. <https://www.coinbase.com/> |
| 2 | + |
| 3 | +/** |
| 4 | + * Prolink URI encoding/decoding - Node.js version |
| 5 | + * Implements the Compressed RPC Link Format ERC specification |
| 6 | + * Uses Node.js zlib for compression |
| 7 | + */ |
| 8 | + |
| 9 | +import { decodeGenericRpc, encodeGenericRpc } from './shortcuts/generic.js'; |
| 10 | +import { decodeWalletSendCalls, encodeWalletSendCalls } from './shortcuts/sendCalls.js'; |
| 11 | +import { decodeWalletSign, encodeWalletSign } from './shortcuts/sign.js'; |
| 12 | +import type { ProlinkDecoded, ProlinkRequest, RpcLinkPayload } from './types.js'; |
| 13 | +import { decodeBase64url, encodeBase64url } from './utils/base64url.js'; |
| 14 | +import { compressPayload, decompressPayload } from './utils/compression.node.js'; |
| 15 | +import { decodeCapabilities, encodeCapabilities } from './utils/encoding.js'; |
| 16 | +import { decodeRpcLinkPayload, encodeRpcLinkPayload } from './utils/protobuf.js'; |
| 17 | + |
| 18 | +const PROTOCOL_VERSION = 1; |
| 19 | +const SHORTCUT_VERSION = 0; |
| 20 | + |
| 21 | +const SHORTCUT_GENERIC = 0; |
| 22 | +const SHORTCUT_WALLET_SEND_CALLS = 1; |
| 23 | +const SHORTCUT_WALLET_SIGN = 2; |
| 24 | + |
| 25 | +/** |
| 26 | + * Encode a JSON-RPC request to prolink format |
| 27 | + * @param request - JSON-RPC request with method, params, optional chainId and capabilities |
| 28 | + * @returns Base64url-encoded prolink payload |
| 29 | + */ |
| 30 | +export async function encodeProlink(request: ProlinkRequest): Promise<string> { |
| 31 | + let payload: RpcLinkPayload; |
| 32 | + |
| 33 | + // Auto-detect shortcut based on method |
| 34 | + if (request.method === 'wallet_sendCalls') { |
| 35 | + // Validate params structure |
| 36 | + if (!Array.isArray(request.params) || request.params.length === 0) { |
| 37 | + throw new Error('wallet_sendCalls requires params array with at least one element'); |
| 38 | + } |
| 39 | + |
| 40 | + const params = request.params[0]; |
| 41 | + if (typeof params !== 'object' || !params) { |
| 42 | + throw new Error('wallet_sendCalls params[0] must be an object'); |
| 43 | + } |
| 44 | + |
| 45 | + // Extract chainId from params |
| 46 | + const chainIdHex = (params as { chainId?: string }).chainId; |
| 47 | + if (!chainIdHex) { |
| 48 | + throw new Error('wallet_sendCalls requires chainId in params'); |
| 49 | + } |
| 50 | + const chainId = Number.parseInt(chainIdHex, 16); |
| 51 | + |
| 52 | + const walletSendCalls = encodeWalletSendCalls( |
| 53 | + params as Parameters<typeof encodeWalletSendCalls>[0] |
| 54 | + ); |
| 55 | + |
| 56 | + payload = { |
| 57 | + protocolVersion: PROTOCOL_VERSION, |
| 58 | + chainId, |
| 59 | + shortcutId: SHORTCUT_WALLET_SEND_CALLS, |
| 60 | + shortcutVersion: SHORTCUT_VERSION, |
| 61 | + body: { |
| 62 | + case: 'walletSendCalls', |
| 63 | + value: walletSendCalls, |
| 64 | + }, |
| 65 | + capabilities: request.capabilities ? encodeCapabilities(request.capabilities) : undefined, |
| 66 | + }; |
| 67 | + } else if (request.method === 'wallet_sign') { |
| 68 | + // Validate params structure |
| 69 | + if (!Array.isArray(request.params) || request.params.length === 0) { |
| 70 | + throw new Error('wallet_sign requires params array with at least one element'); |
| 71 | + } |
| 72 | + |
| 73 | + const params = request.params[0]; |
| 74 | + if (typeof params !== 'object' || !params) { |
| 75 | + throw new Error('wallet_sign params[0] must be an object'); |
| 76 | + } |
| 77 | + |
| 78 | + // Extract chainId from params |
| 79 | + const chainIdHex = (params as { chainId?: string }).chainId; |
| 80 | + if (!chainIdHex) { |
| 81 | + throw new Error('wallet_sign requires chainId in params'); |
| 82 | + } |
| 83 | + const chainId = Number.parseInt(chainIdHex, 16); |
| 84 | + |
| 85 | + const walletSign = encodeWalletSign(params as Parameters<typeof encodeWalletSign>[0]); |
| 86 | + |
| 87 | + payload = { |
| 88 | + protocolVersion: PROTOCOL_VERSION, |
| 89 | + chainId, |
| 90 | + shortcutId: SHORTCUT_WALLET_SIGN, |
| 91 | + shortcutVersion: SHORTCUT_VERSION, |
| 92 | + body: { |
| 93 | + case: 'walletSign', |
| 94 | + value: walletSign, |
| 95 | + }, |
| 96 | + capabilities: request.capabilities ? encodeCapabilities(request.capabilities) : undefined, |
| 97 | + }; |
| 98 | + } else { |
| 99 | + // Generic JSON-RPC |
| 100 | + const generic = encodeGenericRpc(request.method, request.params); |
| 101 | + |
| 102 | + payload = { |
| 103 | + protocolVersion: PROTOCOL_VERSION, |
| 104 | + chainId: request.chainId, |
| 105 | + shortcutId: SHORTCUT_GENERIC, |
| 106 | + shortcutVersion: SHORTCUT_VERSION, |
| 107 | + body: { |
| 108 | + case: 'generic', |
| 109 | + value: generic, |
| 110 | + }, |
| 111 | + capabilities: request.capabilities ? encodeCapabilities(request.capabilities) : undefined, |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + // Serialize to protobuf |
| 116 | + const protoBytes = encodeRpcLinkPayload(payload); |
| 117 | + |
| 118 | + // Compress (with flag byte) |
| 119 | + const { compressed, flag } = await compressPayload(protoBytes); |
| 120 | + const withFlag = new Uint8Array(compressed.length + 1); |
| 121 | + withFlag[0] = flag; |
| 122 | + withFlag.set(compressed, 1); |
| 123 | + |
| 124 | + // Base64url encode |
| 125 | + return encodeBase64url(withFlag); |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Decode a prolink payload to JSON-RPC request |
| 130 | + * @param payload - Base64url-encoded prolink payload |
| 131 | + * @returns Decoded JSON-RPC request |
| 132 | + */ |
| 133 | +export async function decodeProlink(payload: string): Promise<ProlinkDecoded> { |
| 134 | + // Base64url decode |
| 135 | + const bytes = decodeBase64url(payload); |
| 136 | + |
| 137 | + // Decompress |
| 138 | + const decompressed = await decompressPayload(bytes); |
| 139 | + |
| 140 | + // Deserialize protobuf |
| 141 | + const rpcPayload = decodeRpcLinkPayload(decompressed); |
| 142 | + |
| 143 | + // Validate protocol version |
| 144 | + if (rpcPayload.protocolVersion !== PROTOCOL_VERSION) { |
| 145 | + throw new Error( |
| 146 | + `Unsupported protocol version: ${rpcPayload.protocolVersion} (expected ${PROTOCOL_VERSION})` |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + // Decode capabilities |
| 151 | + const capabilities = rpcPayload.capabilities |
| 152 | + ? decodeCapabilities(rpcPayload.capabilities) |
| 153 | + : undefined; |
| 154 | + |
| 155 | + // Dispatch to shortcut decoder |
| 156 | + if (rpcPayload.shortcutId === SHORTCUT_GENERIC) { |
| 157 | + if (rpcPayload.body.case !== 'generic') { |
| 158 | + throw new Error('Invalid payload: shortcut 0 requires generic body'); |
| 159 | + } |
| 160 | + |
| 161 | + const { method, params } = decodeGenericRpc(rpcPayload.body.value); |
| 162 | + |
| 163 | + return { |
| 164 | + method, |
| 165 | + params, |
| 166 | + chainId: rpcPayload.chainId, |
| 167 | + capabilities, |
| 168 | + }; |
| 169 | + } |
| 170 | + |
| 171 | + if (rpcPayload.shortcutId === SHORTCUT_WALLET_SEND_CALLS) { |
| 172 | + if (rpcPayload.body.case !== 'walletSendCalls') { |
| 173 | + throw new Error('Invalid payload: shortcut 1 requires walletSendCalls body'); |
| 174 | + } |
| 175 | + |
| 176 | + if (!rpcPayload.chainId) { |
| 177 | + throw new Error('wallet_sendCalls requires chainId'); |
| 178 | + } |
| 179 | + |
| 180 | + const params = decodeWalletSendCalls(rpcPayload.body.value, rpcPayload.chainId); |
| 181 | + |
| 182 | + return { |
| 183 | + method: 'wallet_sendCalls', |
| 184 | + params: [params], |
| 185 | + chainId: rpcPayload.chainId, |
| 186 | + capabilities, |
| 187 | + }; |
| 188 | + } |
| 189 | + |
| 190 | + if (rpcPayload.shortcutId === SHORTCUT_WALLET_SIGN) { |
| 191 | + if (rpcPayload.body.case !== 'walletSign') { |
| 192 | + throw new Error('Invalid payload: shortcut 2 requires walletSign body'); |
| 193 | + } |
| 194 | + |
| 195 | + if (!rpcPayload.chainId) { |
| 196 | + throw new Error('wallet_sign requires chainId'); |
| 197 | + } |
| 198 | + |
| 199 | + const params = decodeWalletSign(rpcPayload.body.value, rpcPayload.chainId); |
| 200 | + |
| 201 | + return { |
| 202 | + method: 'wallet_sign', |
| 203 | + params: [params], |
| 204 | + chainId: rpcPayload.chainId, |
| 205 | + capabilities, |
| 206 | + }; |
| 207 | + } |
| 208 | + |
| 209 | + throw new Error(`Unsupported shortcut ID: ${rpcPayload.shortcutId}`); |
| 210 | +} |
| 211 | + |
| 212 | +// Re-export types |
| 213 | +export type { ProlinkDecoded, ProlinkRequest } from './types.js'; |
0 commit comments