@gibs/bridge-client 1.29.0 → 1.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/block-polling/package.json +6 -0
  2. package/coinbase-accept-list/package.json +6 -0
  3. package/dist/block-polling.d.ts +33 -0
  4. package/dist/block-polling.js +10 -0
  5. package/dist/cjs/block-polling.d.ts +33 -0
  6. package/dist/cjs/block-polling.js +15 -0
  7. package/dist/cjs/client.d.ts +9 -1
  8. package/dist/cjs/client.js +8 -5
  9. package/dist/cjs/coinbase-accept-list.d.ts +101 -0
  10. package/dist/cjs/coinbase-accept-list.js +97 -0
  11. package/dist/cjs/crossing-decision.d.ts +166 -0
  12. package/dist/cjs/crossing-decision.js +169 -0
  13. package/dist/cjs/crossing-vector-guard.d.ts +45 -0
  14. package/dist/cjs/crossing-vector-guard.js +139 -0
  15. package/dist/cjs/exchange-deposit-board.d.ts +50 -0
  16. package/dist/cjs/exchange-deposit-board.js +70 -0
  17. package/dist/cjs/exchange-deposit-chain.d.ts +146 -0
  18. package/dist/cjs/exchange-deposit-chain.js +228 -0
  19. package/dist/cjs/exchange-deposit-constants.d.ts +10 -0
  20. package/dist/cjs/exchange-deposit-constants.js +6 -0
  21. package/dist/cjs/exchange-deposit-factory-calls.d.ts +245 -0
  22. package/dist/cjs/exchange-deposit-factory-calls.js +103 -0
  23. package/dist/cjs/exchange-deposit-factory.d.ts +87 -0
  24. package/dist/cjs/exchange-deposit-factory.js +110 -0
  25. package/dist/cjs/exchange-deposit-fake.d.ts +67 -0
  26. package/dist/cjs/exchange-deposit-fake.js +179 -0
  27. package/dist/cjs/exchange-deposit-index.d.ts +77 -0
  28. package/dist/cjs/exchange-deposit-index.js +40 -0
  29. package/dist/cjs/exchange-deposit-issuance.d.ts +327 -0
  30. package/dist/cjs/exchange-deposit-issuance.js +244 -0
  31. package/dist/cjs/exchange-deposit-path.d.ts +16 -0
  32. package/dist/cjs/exchange-deposit-path.js +15 -0
  33. package/dist/cjs/exchange-deposit-pow.d.ts +163 -0
  34. package/dist/cjs/exchange-deposit-pow.js +101 -0
  35. package/dist/cjs/exchange-deposit-recipient.d.ts +66 -0
  36. package/dist/cjs/exchange-deposit-recipient.js +35 -0
  37. package/dist/cjs/exchange-deposit-self-cross.d.ts +163 -0
  38. package/dist/cjs/exchange-deposit-self-cross.js +60 -0
  39. package/dist/cjs/exchange-deposit-signin.d.ts +275 -0
  40. package/dist/cjs/exchange-deposit-signin.js +166 -0
  41. package/dist/cjs/exchange-deposit-watch.d.ts +133 -0
  42. package/dist/cjs/exchange-deposit-watch.js +58 -0
  43. package/dist/cjs/exchange-deposit.d.ts +440 -0
  44. package/dist/cjs/exchange-deposit.js +154 -0
  45. package/dist/cjs/exchange-sender-guides.d.ts +29 -0
  46. package/dist/cjs/exchange-sender-guides.js +25 -0
  47. package/dist/cjs/image.d.ts +18 -0
  48. package/dist/cjs/image.js +11 -9
  49. package/dist/cjs/index.d.ts +4 -0
  50. package/dist/cjs/index.js +32 -1
  51. package/dist/cjs/kraken-accept-list.d.ts +70 -0
  52. package/dist/cjs/kraken-accept-list.js +24 -0
  53. package/dist/cjs/unlisted-token.d.ts +26 -0
  54. package/dist/cjs/unlisted-token.js +34 -0
  55. package/dist/client.d.ts +9 -1
  56. package/dist/client.js +8 -5
  57. package/dist/coinbase-accept-list.d.ts +101 -0
  58. package/dist/coinbase-accept-list.js +89 -0
  59. package/dist/crossing-decision.d.ts +166 -0
  60. package/dist/crossing-decision.js +162 -0
  61. package/dist/crossing-vector-guard.d.ts +45 -0
  62. package/dist/crossing-vector-guard.js +134 -0
  63. package/dist/exchange-deposit-board.d.ts +50 -0
  64. package/dist/exchange-deposit-board.js +28 -0
  65. package/dist/exchange-deposit-chain.d.ts +146 -0
  66. package/dist/exchange-deposit-chain.js +218 -0
  67. package/dist/exchange-deposit-constants.d.ts +10 -0
  68. package/dist/exchange-deposit-constants.js +3 -0
  69. package/dist/exchange-deposit-factory-calls.d.ts +245 -0
  70. package/dist/exchange-deposit-factory-calls.js +97 -0
  71. package/dist/exchange-deposit-factory.d.ts +87 -0
  72. package/dist/exchange-deposit-factory.js +104 -0
  73. package/dist/exchange-deposit-fake.d.ts +67 -0
  74. package/dist/exchange-deposit-fake.js +171 -0
  75. package/dist/exchange-deposit-index.d.ts +77 -0
  76. package/dist/exchange-deposit-index.js +34 -0
  77. package/dist/exchange-deposit-issuance.d.ts +327 -0
  78. package/dist/exchange-deposit-issuance.js +224 -0
  79. package/dist/exchange-deposit-path.d.ts +16 -0
  80. package/dist/exchange-deposit-path.js +11 -0
  81. package/dist/exchange-deposit-pow.d.ts +163 -0
  82. package/dist/exchange-deposit-pow.js +93 -0
  83. package/dist/exchange-deposit-recipient.d.ts +66 -0
  84. package/dist/exchange-deposit-recipient.js +28 -0
  85. package/dist/exchange-deposit-self-cross.d.ts +163 -0
  86. package/dist/exchange-deposit-self-cross.js +46 -0
  87. package/dist/exchange-deposit-signin.d.ts +275 -0
  88. package/dist/exchange-deposit-signin.js +147 -0
  89. package/dist/exchange-deposit-watch.d.ts +133 -0
  90. package/dist/exchange-deposit-watch.js +44 -0
  91. package/dist/exchange-deposit.d.ts +440 -0
  92. package/dist/exchange-deposit.js +138 -0
  93. package/dist/exchange-sender-guides.d.ts +29 -0
  94. package/dist/exchange-sender-guides.js +21 -0
  95. package/dist/image.d.ts +18 -0
  96. package/dist/image.js +8 -8
  97. package/dist/index.d.ts +4 -0
  98. package/dist/index.js +4 -0
  99. package/dist/kraken-accept-list.d.ts +70 -0
  100. package/dist/kraken-accept-list.js +17 -0
  101. package/dist/unlisted-token.d.ts +26 -0
  102. package/dist/unlisted-token.js +30 -0
  103. package/exchange-deposit/package.json +6 -0
  104. package/exchange-deposit-board/package.json +6 -0
  105. package/exchange-deposit-chain/package.json +6 -0
  106. package/exchange-deposit-factory/package.json +6 -0
  107. package/exchange-deposit-fake/package.json +6 -0
  108. package/exchange-deposit-issuance/package.json +6 -0
  109. package/exchange-deposit-path/package.json +6 -0
  110. package/exchange-deposit-pow/package.json +6 -0
  111. package/exchange-deposit-recipient/package.json +6 -0
  112. package/exchange-deposit-self-cross/package.json +6 -0
  113. package/exchange-deposit-signin/package.json +6 -0
  114. package/exchange-deposit-watch/package.json +6 -0
  115. package/exchange-sender-guides/package.json +6 -0
  116. package/kraken-accept-list/package.json +6 -0
  117. package/llms.txt +1 -0
  118. package/package.json +487 -6
  119. package/unlisted-token/package.json +6 -0
@@ -0,0 +1,275 @@
1
+ import { type Address, type Hex, type TypedDataDefinition } from 'viem';
2
+ import { type ExchangeDepositSeed } from './exchange-deposit-index.js';
3
+ /**
4
+ * Sign-in for deposit addresses. A reader signs one fixed typed-data request (Ethereum Improvement Proposal 712) with the connected wallet, and the
5
+ * signature becomes the seed of every deposit address of that recipient on that chain and factory. A smart-contract
6
+ * wallet cannot give the same signature twice, so it gets a random seed instead, kept in this browser.
7
+ *
8
+ * Nothing here talks to a chain or a server. The wallet call is the interface's; this file holds the rules: the typed
9
+ * data, the seed, the offline check of a signature, and the stored record. The signature never leaves the browser: it
10
+ * is the seed of the deposit addresses, so a remote node that saw it could derive them.
11
+ */
12
+ /**
13
+ * The version of the sign-in. Version 1 signed a plain text message. Version 2 signs typed data (Ethereum Improvement Proposal 712). A
14
+ * change to the request or the rules bumps it, and so changes every address. Nothing was released to production with
15
+ * version 1: its records, kept on staging only, are ignored because the storage key and the seed tag both change.
16
+ */
17
+ export declare const EXCHANGE_DEPOSIT_SIGNIN_VERSION = 2;
18
+ /** The name of the one type the sign-in signs. A wallet shows it as the title of the request. */
19
+ declare const SIGNIN_PRIMARY_TYPE = "DepositSignIn";
20
+ /**
21
+ * The fields of the sign-in type. A wallet shows each field by name. The order and the names are part of the type, so
22
+ * they never change without a version bump.
23
+ */
24
+ export declare const EXCHANGE_DEPOSIT_SIGNIN_TYPES: {
25
+ readonly DepositSignIn: readonly [{
26
+ readonly name: "recipient";
27
+ readonly type: "address";
28
+ }, {
29
+ readonly name: "purpose";
30
+ readonly type: "string";
31
+ }, {
32
+ readonly name: "notice";
33
+ readonly type: "string";
34
+ }];
35
+ };
36
+ /** The inputs that name the sign-in request. The recipient is checksummed, so a lower case recipient gives the same request. */
37
+ export interface ExchangeDepositSigninInput {
38
+ /** The PulseChain address the funds reach. */
39
+ readonly recipient: Address;
40
+ /** The chain of the factory, which is the chain the reader signs on. It is the domain's chain id. */
41
+ readonly chainId: bigint;
42
+ /** The deposit factory on that chain. It is the domain's verifying contract. */
43
+ readonly factoryAddress: Address;
44
+ }
45
+ /** The typed-data request the wallet signs for the sign-in. */
46
+ export type ExchangeDepositSigninTypedData = TypedDataDefinition<typeof EXCHANGE_DEPOSIT_SIGNIN_TYPES, typeof SIGNIN_PRIMARY_TYPE>;
47
+ /**
48
+ * The typed-data request the wallet signs. The domain is fixed: the name, the version, the factory's chain and the factory.
49
+ * The message carries no nonce, no timestamp, no origin and no web address: the same build is served from several
50
+ * hosts, and a host in the request would give each host other addresses. So the signature, and the seed from it, is the
51
+ * same on every visit and from every origin.
52
+ *
53
+ * @param input.recipient - the PulseChain address the funds reach
54
+ * @param input.chainId - the chain of the factory
55
+ * @param input.factoryAddress - the deposit factory on that chain
56
+ * @returns the typed data to sign, with the addresses checksummed
57
+ */
58
+ export declare const exchangeDepositSigninTypedData: ({ recipient, chainId, factoryAddress, }: ExchangeDepositSigninInput) => ExchangeDepositSigninTypedData;
59
+ /**
60
+ * The seed of an ordinary wallet's signature. The signature is split into `r` and `s`, and `s` is made low, so a wallet
61
+ * that returns the same key's signature as its twin (`n - s`), or returns the recovery id as 0/1 or 27/28, gives the
62
+ * same seed. Only `r` and `s` enter the hash; the recovery id does not.
63
+ *
64
+ * @param signature - the 65-byte signature of the sign-in typed data
65
+ * @returns the 32-byte seed
66
+ * @throws when the signature is not a valid 65-byte signature
67
+ */
68
+ export declare const exchangeDepositSeedOfSignature: (signature: Hex) => ExchangeDepositSeed;
69
+ /**
70
+ * The seed of a smart-contract wallet: the 32 random bytes the interface drew. The caller supplies the bytes, so this
71
+ * file never touches a random source and a test can pass fixed bytes.
72
+ *
73
+ * @param randomBytes - exactly 32 random bytes
74
+ * @throws when there are not 32 bytes
75
+ */
76
+ export declare const exchangeDepositSeedOfRandomBytes: (randomBytes: Uint8Array) => ExchangeDepositSeed;
77
+ /**
78
+ * Whether a typed-data signature was made by the signer. The check is offline: it recovers the address from the
79
+ * signature and the typed data, in this browser, and sends nothing. It takes no client, so no remote node sees the
80
+ * signature. That matters because an ordinary wallet's signature is the seed of the deposit addresses. A node
81
+ * operator who saw it could derive them. A signature that does not verify is refused before it is stored, so a junk
82
+ * value never becomes the permanent seed. A signature that cannot be parsed is not from the signer.
83
+ *
84
+ * A smart-contract wallet never reaches this check: it takes a random seed instead (see the interface), so only
85
+ * ordinary-wallet signatures are verified here.
86
+ *
87
+ * @param input.signature - the signature the wallet returned
88
+ * @param input.typedData - the typed data the wallet was asked to sign
89
+ * @param input.signer - the address the signature must come from
90
+ * @returns whether the signer made the signature; false for a malformed signature
91
+ */
92
+ export declare const exchangeDepositSignatureIsFrom: ({ signature, typedData, signer, }: {
93
+ signature: Hex;
94
+ typedData: ExchangeDepositSigninTypedData;
95
+ signer: Address;
96
+ }) => Promise<boolean>;
97
+ /**
98
+ * The key of a stored sign-in: the chain, the factory, the recipient and the signer. Each signer has its own record,
99
+ * so a second wallet signing in for the same recipient never overwrites the first wallet's seed. A smart-contract
100
+ * wallet's random seed is lost from the browser if it is overwritten.
101
+ *
102
+ * @param input - the chain, the factory, the recipient and the signer
103
+ */
104
+ export declare const exchangeDepositSigninStorageKey: ({ chainId, factoryAddress, recipient, signer, }: {
105
+ chainId: bigint;
106
+ factoryAddress: Address;
107
+ recipient: Address;
108
+ signer: Address;
109
+ }) => string;
110
+ /**
111
+ * What is kept in the browser for one sign-in: the seed only, never the signature. An ordinary wallet's seed is derived
112
+ * from its signature (method 'signature'), so a return visit need not sign again. A smart-contract wallet's seed is
113
+ * random (method 'random'). Anyone who reads the seed can work out every address of the recipient, so it is as
114
+ * private as the signature it came from.
115
+ */
116
+ export interface StoredSigninRecord {
117
+ /** The wallet the seed belongs to. */
118
+ readonly signer: Address;
119
+ /** How the seed was made: from an ordinary wallet's signature, or drawn at random for a smart-contract wallet. */
120
+ readonly method: 'signature' | 'random';
121
+ /** The 32-byte seed the addresses grow from. */
122
+ readonly seed: ExchangeDepositSeed;
123
+ /**
124
+ * The highest counter shown for each token and fee. Optional: a record written before this field existed has none.
125
+ * Written only through {@link withRecordedHighestCounter}, so the signer, the method and the seed never change.
126
+ */
127
+ readonly highestIssuedCounters?: readonly HighestIssuedCounter[];
128
+ }
129
+ /**
130
+ * The highest counter this browser has shown for one token and fee. Only the highest per token and fee is kept, because
131
+ * the interface offers the counter after it and never one below it.
132
+ */
133
+ export interface HighestIssuedCounter {
134
+ /** The token of the addresses. */
135
+ readonly tokenAddress: Address;
136
+ /** The exact fee of the addresses, in 1e18 fixed point. */
137
+ readonly feeRateE18: bigint;
138
+ /** The highest counter shown for the token and fee. */
139
+ readonly counter: number;
140
+ }
141
+ /** The part of the browser's storage the sign-in reads and writes. `window.localStorage` satisfies it. */
142
+ export type ExchangeDepositSigninStorage = Pick<Storage, 'getItem' | 'setItem' | 'removeItem'>;
143
+ /**
144
+ * Parses a stored record. A value that is not JSON, or not a record of this version, reads as no record. Nothing is
145
+ * repaired, so a corrupt value sends the reader through sign-in again.
146
+ *
147
+ * @param text - the stored text, or null when nothing is stored
148
+ */
149
+ export declare const parseStoredSigninRecord: (text: string | null) => StoredSigninRecord | null;
150
+ /**
151
+ * The record as stored text.
152
+ *
153
+ * @param record - the record to store
154
+ */
155
+ export declare const serializeStoredSigninRecord: (record: StoredSigninRecord) => string;
156
+ /**
157
+ * The highest counter a record holds for one token and fee, or null when it holds none.
158
+ *
159
+ * @param input.record - the stored record
160
+ * @param input.tokenAddress - the token of the addresses
161
+ * @param input.feeRateE18 - the exact fee of the addresses
162
+ */
163
+ export declare const recordedHighestCounterOf: ({ record, tokenAddress, feeRateE18, }: {
164
+ record: StoredSigninRecord;
165
+ tokenAddress: Address;
166
+ feeRateE18: bigint;
167
+ }) => number | null;
168
+ /**
169
+ * The record with the counter raised for one token and fee. A record that already holds the same or a higher counter is
170
+ * returned as it is, so a write happens only when the counter grows. Only the counter field changes: the signer, the
171
+ * method and the seed stay exactly as they were.
172
+ *
173
+ * @param input.record - the stored record
174
+ * @param input.tokenAddress - the token of the addresses
175
+ * @param input.feeRateE18 - the exact fee of the addresses
176
+ * @param input.counter - the counter shown
177
+ */
178
+ export declare const withRecordedHighestCounter: ({ record, tokenAddress, feeRateE18, counter, }: {
179
+ record: StoredSigninRecord;
180
+ tokenAddress: Address;
181
+ feeRateE18: bigint;
182
+ counter: number;
183
+ }) => StoredSigninRecord;
184
+ /**
185
+ * The seed a record holds.
186
+ *
187
+ * @param record - the stored record
188
+ */
189
+ export declare const exchangeDepositSeedOfRecord: (record: StoredSigninRecord) => ExchangeDepositSeed;
190
+ /**
191
+ * Whether a record was made by the signer. A record from another signer is never used, because that signer's
192
+ * addresses differ.
193
+ *
194
+ * @param input.record - the stored record
195
+ * @param input.signer - the connected wallet
196
+ */
197
+ export declare const signinRecordIsOf: ({ record, signer }: {
198
+ record: StoredSigninRecord;
199
+ signer: Address;
200
+ }) => boolean;
201
+ /**
202
+ * Reads the stored record of a key from the storage, or null when none is usable.
203
+ *
204
+ * @param input.storage - the browser storage, or a test double
205
+ * @param input.key - the key from {@link exchangeDepositSigninStorageKey}
206
+ */
207
+ export declare const readStoredSigninRecord: ({ storage, key, }: {
208
+ storage: ExchangeDepositSigninStorage;
209
+ key: string;
210
+ }) => StoredSigninRecord | null;
211
+ /**
212
+ * Stores a record under a key.
213
+ *
214
+ * @param input.storage - the browser storage, or a test double
215
+ * @param input.key - the key from {@link exchangeDepositSigninStorageKey}
216
+ * @param input.record - the record to store
217
+ */
218
+ export declare const writeStoredSigninRecord: ({ storage, key, record, }: {
219
+ storage: ExchangeDepositSigninStorage;
220
+ key: string;
221
+ record: StoredSigninRecord;
222
+ }) => void;
223
+ /**
224
+ * The record a delegated ordinary key gets when its signature does not verify. The key delegates to code that gave a
225
+ * signature the key does not recover to, so that signature cannot be the seed. The key then gets a random seed, kept in
226
+ * this browser, as a smart-contract wallet does. A stored signature record is never replaced by a random one, because
227
+ * its addresses may hold funds: that case reports `differs` and the stored record stays.
228
+ *
229
+ * @param input.signer - the connected delegated key
230
+ * @param input.existing - the stored record for this signer and key, or null when none is stored
231
+ * @param input.randomBytes - 32 fresh random bytes, drawn only when nothing is stored
232
+ */
233
+ export declare const signinOutcomeOfUnverifiedDelegation: ({ signer, existing, randomBytes, }: {
234
+ signer: Address;
235
+ existing: StoredSigninRecord | null;
236
+ randomBytes: Uint8Array;
237
+ }) => ExchangeDepositSigninOutcome;
238
+ /**
239
+ * The result of a sign-in that the interface can keep. `signed` is the record to store and use. `differs` means an
240
+ * ordinary wallet signed something other than the stored signature, so its seed would make other addresses: the stored
241
+ * sign-in is kept and nothing is written.
242
+ */
243
+ export type ExchangeDepositSigninOutcome = {
244
+ readonly kind: 'signed';
245
+ readonly record: StoredSigninRecord;
246
+ } | {
247
+ readonly kind: 'differs';
248
+ };
249
+ /**
250
+ * The record an ordinary wallet's signature gives. The first signature is kept. A later signature, from a sign-in
251
+ * again, must give the same seed as the stored one; if it does not, the stored record stays and the result is `differs`.
252
+ *
253
+ * @param input.signer - the connected wallet that signed
254
+ * @param input.signature - the signature of the sign-in typed data
255
+ * @param input.existing - the stored record for this signer and key, or null when none is stored
256
+ */
257
+ export declare const signinOutcomeOfSignature: ({ signer, signature, existing, }: {
258
+ signer: Address;
259
+ signature: Hex;
260
+ existing: StoredSigninRecord | null;
261
+ }) => ExchangeDepositSigninOutcome;
262
+ /**
263
+ * The record a smart-contract wallet gets. Its signature is not repeatable, so it never signs to get a seed: the seed
264
+ * is random, drawn once, and kept. A stored random record is never replaced, because its addresses may hold funds.
265
+ *
266
+ * @param input.signer - the connected smart-contract wallet
267
+ * @param input.existing - the stored record for this signer and key, or null when none is stored
268
+ * @param input.randomBytes - 32 fresh random bytes, drawn only when nothing is stored
269
+ */
270
+ export declare const signinOutcomeOfRandomSeed: ({ signer, existing, randomBytes, }: {
271
+ signer: Address;
272
+ existing: StoredSigninRecord | null;
273
+ randomBytes: Uint8Array;
274
+ }) => ExchangeDepositSigninOutcome;
275
+ export {};
@@ -0,0 +1,147 @@
1
+ import { bytesToHex, encodeAbiParameters, getAddress, isAddress, isAddressEqual, isHex, keccak256, parseSignature, recoverTypedDataAddress, toHex, } from 'viem';
2
+ import { sameAddress } from './exchange-deposit-recipient.js';
3
+ export const EXCHANGE_DEPOSIT_SIGNIN_VERSION = 2;
4
+ const SEED_DOMAIN_TAG = keccak256(toHex(`gibs-deposit-seed-v${EXCHANGE_DEPOSIT_SIGNIN_VERSION}`));
5
+ const SECP256K1_HALF_ORDER = 0x7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0n;
6
+ const SECP256K1_ORDER = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n;
7
+ const SEED_BYTE_LENGTH = 32;
8
+ const SIGNIN_PRIMARY_TYPE = 'DepositSignIn';
9
+ export const EXCHANGE_DEPOSIT_SIGNIN_TYPES = {
10
+ DepositSignIn: [
11
+ { name: 'recipient', type: 'address' },
12
+ { name: 'purpose', type: 'string' },
13
+ { name: 'notice', type: 'string' },
14
+ ],
15
+ };
16
+ const SIGNIN_PURPOSE = 'Create Gibs deposit addresses';
17
+ const SIGNIN_NOTICE = 'Signing sends nothing and costs no gas. The signature is the secret that makes your deposit addresses, so keep it private. Only sign this on the Gibs Finance app. It cannot move your funds.';
18
+ export const exchangeDepositSigninTypedData = ({ recipient, chainId, factoryAddress, }) => ({
19
+ domain: {
20
+ name: 'Gibs Finance',
21
+ version: '1',
22
+ chainId,
23
+ verifyingContract: getAddress(factoryAddress),
24
+ },
25
+ types: EXCHANGE_DEPOSIT_SIGNIN_TYPES,
26
+ primaryType: SIGNIN_PRIMARY_TYPE,
27
+ message: {
28
+ recipient: getAddress(recipient),
29
+ purpose: SIGNIN_PURPOSE,
30
+ notice: SIGNIN_NOTICE,
31
+ },
32
+ });
33
+ export const exchangeDepositSeedOfSignature = (signature) => {
34
+ const { r, s } = parseSignature(signature);
35
+ const sValue = BigInt(s);
36
+ const lowS = sValue > SECP256K1_HALF_ORDER ? SECP256K1_ORDER - sValue : sValue;
37
+ return keccak256(encodeAbiParameters([{ type: 'bytes32' }, { type: 'bytes32' }, { type: 'bytes32' }], [SEED_DOMAIN_TAG, toHex(BigInt(r), { size: SEED_BYTE_LENGTH }), toHex(lowS, { size: SEED_BYTE_LENGTH })]));
38
+ };
39
+ export const exchangeDepositSeedOfRandomBytes = (randomBytes) => {
40
+ if (randomBytes.length !== SEED_BYTE_LENGTH) {
41
+ throw new Error(`a random seed needs ${SEED_BYTE_LENGTH} bytes, not ${randomBytes.length}`);
42
+ }
43
+ return bytesToHex(randomBytes);
44
+ };
45
+ export const exchangeDepositSignatureIsFrom = async ({ signature, typedData, signer, }) => {
46
+ try {
47
+ return isAddressEqual(await recoverTypedDataAddress({ ...typedData, signature }), signer);
48
+ }
49
+ catch {
50
+ return false;
51
+ }
52
+ };
53
+ const STORAGE_KEY_PREFIX = `gibs.exchange-deposit.signin.v${EXCHANGE_DEPOSIT_SIGNIN_VERSION}`;
54
+ export const exchangeDepositSigninStorageKey = ({ chainId, factoryAddress, recipient, signer, }) => `${STORAGE_KEY_PREFIX}:${chainId.toString()}:${factoryAddress.toLowerCase()}:${recipient.toLowerCase()}:${signer.toLowerCase()}`;
55
+ export const parseStoredSigninRecord = (text) => {
56
+ if (text === null)
57
+ return null;
58
+ const parsed = parseJsonOrNull(text);
59
+ if (parsed === null || typeof parsed !== 'object')
60
+ return null;
61
+ const { signer, method, seed, highestIssuedCounters } = parsed;
62
+ if (typeof signer !== 'string' || !isAddress(signer))
63
+ return null;
64
+ if (method !== 'signature' && method !== 'random')
65
+ return null;
66
+ if (!isHexOfLength(seed, SEED_BYTE_LENGTH))
67
+ return null;
68
+ const record = { signer, method, seed };
69
+ const counters = parseHighestIssuedCounters(highestIssuedCounters);
70
+ if (counters === undefined)
71
+ return record;
72
+ return { ...record, highestIssuedCounters: counters };
73
+ };
74
+ const parseHighestIssuedCounters = (value) => {
75
+ if (value === undefined)
76
+ return undefined;
77
+ if (!Array.isArray(value))
78
+ return [];
79
+ return value.map(parseHighestIssuedCounterRow).filter((row) => row !== null);
80
+ };
81
+ const parseHighestIssuedCounterRow = (row) => {
82
+ if (row === null || typeof row !== 'object')
83
+ return null;
84
+ const { tokenAddress, feeRateE18, counter } = row;
85
+ if (typeof tokenAddress !== 'string' || !isAddress(tokenAddress))
86
+ return null;
87
+ if (typeof feeRateE18 !== 'string' || !/^[0-9]+$/.test(feeRateE18))
88
+ return null;
89
+ if (typeof counter !== 'number' || !Number.isSafeInteger(counter) || counter < 0)
90
+ return null;
91
+ return { tokenAddress: getAddress(tokenAddress), feeRateE18: BigInt(feeRateE18), counter };
92
+ };
93
+ const parseJsonOrNull = (text) => {
94
+ try {
95
+ return JSON.parse(text);
96
+ }
97
+ catch {
98
+ return null;
99
+ }
100
+ };
101
+ const isHexOfLength = (value, byteLength) => typeof value === 'string' && isHex(value) && value.length === 2 + byteLength * 2;
102
+ export const serializeStoredSigninRecord = (record) => JSON.stringify({
103
+ ...record,
104
+ highestIssuedCounters: record.highestIssuedCounters?.map((counter) => ({
105
+ tokenAddress: counter.tokenAddress,
106
+ feeRateE18: counter.feeRateE18.toString(),
107
+ counter: counter.counter,
108
+ })),
109
+ });
110
+ const isCounterForSetting = (row, setting) => sameAddress(row.tokenAddress, setting.tokenAddress) && row.feeRateE18 === setting.feeRateE18;
111
+ export const recordedHighestCounterOf = ({ record, tokenAddress, feeRateE18, }) => {
112
+ const matching = (record.highestIssuedCounters ?? []).filter((row) => isCounterForSetting(row, { tokenAddress, feeRateE18 }));
113
+ if (matching.length === 0)
114
+ return null;
115
+ return Math.max(...matching.map((row) => row.counter));
116
+ };
117
+ export const withRecordedHighestCounter = ({ record, tokenAddress, feeRateE18, counter, }) => {
118
+ const recorded = recordedHighestCounterOf({ record, tokenAddress, feeRateE18 });
119
+ if (recorded !== null && recorded >= counter)
120
+ return record;
121
+ const others = (record.highestIssuedCounters ?? []).filter((row) => !isCounterForSetting(row, { tokenAddress, feeRateE18 }));
122
+ return { ...record, highestIssuedCounters: [...others, { tokenAddress: getAddress(tokenAddress), feeRateE18, counter }] };
123
+ };
124
+ export const exchangeDepositSeedOfRecord = (record) => record.seed;
125
+ export const signinRecordIsOf = ({ record, signer }) => sameAddress(record.signer, signer);
126
+ export const readStoredSigninRecord = ({ storage, key, }) => parseStoredSigninRecord(storage.getItem(key));
127
+ export const writeStoredSigninRecord = ({ storage, key, record, }) => storage.setItem(key, serializeStoredSigninRecord(record));
128
+ export const signinOutcomeOfUnverifiedDelegation = ({ signer, existing, randomBytes, }) => {
129
+ if (existing?.method === 'signature')
130
+ return { kind: 'differs' };
131
+ return signinOutcomeOfRandomSeed({ signer, existing, randomBytes });
132
+ };
133
+ export const signinOutcomeOfSignature = ({ signer, signature, existing, }) => {
134
+ const seed = exchangeDepositSeedOfSignature(signature);
135
+ if (existing === null)
136
+ return { kind: 'signed', record: { signer, method: 'signature', seed } };
137
+ if (existing.method !== 'signature')
138
+ return { kind: 'differs' };
139
+ if (seed !== existing.seed)
140
+ return { kind: 'differs' };
141
+ return { kind: 'signed', record: existing };
142
+ };
143
+ export const signinOutcomeOfRandomSeed = ({ signer, existing, randomBytes, }) => {
144
+ if (existing !== null && existing.method === 'random')
145
+ return { kind: 'signed', record: existing };
146
+ return { kind: 'signed', record: { signer, method: 'random', seed: exchangeDepositSeedOfRandomBytes(randomBytes) } };
147
+ };
@@ -0,0 +1,133 @@
1
+ import { type Address, type Hex } from 'viem';
2
+ import { type FactorySettingsTuple } from './exchange-deposit-factory-calls.js';
3
+ export { factorySettingsTupleOf, type FactorySettingsTuple } from './exchange-deposit-factory-calls.js';
4
+ export { exchangeDepositFactoryOf } from './exchange-deposit-factory.js';
5
+ /**
6
+ * The watch message: the public settings of a deposit address, posted to the board so validators can watch for a
7
+ * crossing. The body is PUBLIC by design. Nothing in it is secret: the address is derived from these settings, and the
8
+ * seed stays with the reader. A validator recomputes the address from the settings and drops any message that does not match.
9
+ *
10
+ * The message is ABI-encoded with the factory's own tuple, so the decoded settings can be passed straight to the
11
+ * factory's `depositAddress` view.
12
+ */
13
+ /** The category name. Its hash is the board category every watch message is posted under. */
14
+ export declare const WATCH_CATEGORY_NAME = "gibs-deposit-watch-message-v1";
15
+ /** The board category for every watch message. Fixed, so a validator subscribes once. */
16
+ export declare const WATCH_CATEGORY: Hex;
17
+ /** The page version the message format belongs to. A validator reads the version before it decodes the settings. */
18
+ export declare const WATCH_PAGE_VERSION = 1n;
19
+ /** How long validators watch a post, from the time the board first shows it: 24 hours, in seconds. */
20
+ export declare const WATCH_WINDOW_SECONDS = 86400;
21
+ /** The least time between two posts for one address in one browser: one hour, in milliseconds. */
22
+ export declare const WATCH_REPOST_INTERVAL_MS = 3600000;
23
+ /** What a watch message says about one deposit address. */
24
+ export interface WatchMessage {
25
+ /** The chain the factory is on. */
26
+ readonly chainId: bigint;
27
+ /** The factory whose `depositAddress` view derives the address. */
28
+ readonly factory: Address;
29
+ /** The page version the format belongs to. */
30
+ readonly pageVersion: bigint;
31
+ /** The factory's settings tuple: recipient, token, fee, partner, rate, index and unwrap, with the group the factory takes. */
32
+ readonly settings: FactorySettingsTuple;
33
+ }
34
+ /**
35
+ * The body of a watch message.
36
+ *
37
+ * @param message - the settings to publish
38
+ */
39
+ export declare const encodeWatchMessage: (message: WatchMessage) => Hex;
40
+ /**
41
+ * The watch message in a body. A body that is not a watch message throws, so the validator drops it.
42
+ *
43
+ * @param body - the body the board holds
44
+ * @throws when the body is not an encoded watch message
45
+ */
46
+ export declare const decodeWatchMessage: (body: Hex) => WatchMessage;
47
+ /**
48
+ * Whether a validator should watch the address. The watch message names the address the settings derive, so the
49
+ * validator recomputes it through `depositAddressOf` and compares it with the address it was given.
50
+ *
51
+ * @param input.message - the decoded watch message
52
+ * @param input.claimedAddress - the deposit address the reader shows
53
+ * @param input.depositAddressOf - the factory's `depositAddress` view, for the message's factory and chain
54
+ */
55
+ export declare const watchMessageRecomputes: ({ message, claimedAddress, depositAddressOf, }: {
56
+ message: WatchMessage;
57
+ claimedAddress: Address;
58
+ depositAddressOf: (settings: FactorySettingsTuple) => Promise<Address>;
59
+ }) => Promise<boolean>;
60
+ /**
61
+ * Whether a board post carries a watch message on the board chain. Only the mainnet factory's chain has a board in this
62
+ * repository. A Sepolia factory has none, so its addresses get no post and no watch warning.
63
+ *
64
+ * @param chainId - the chain of the factory
65
+ */
66
+ export declare const watchPostsOnChain: (chainId: bigint) => boolean;
67
+ /**
68
+ * Whether an address at this fee rate gets a watch post. A no-fee address is crossed by its owner alone, so no validator
69
+ * crosses it, and a post would promise a crossing that never comes.
70
+ *
71
+ * @param input.feeRateE18 - the fee rate of the address, in 1e18 fixed point
72
+ */
73
+ export declare const watchPostsForFeeRate: ({ feeRateE18 }: {
74
+ readonly feeRateE18: bigint;
75
+ }) => boolean;
76
+ /**
77
+ * Whether a post for one address is due. A browser posts at most once an hour per address, so a reload does not spam.
78
+ *
79
+ * @param input.postedAtMs - when this browser last posted for the address, or null when it never has
80
+ * @param input.nowMs - the current time
81
+ */
82
+ export declare const watchPostDue: ({ postedAtMs, nowMs }: {
83
+ postedAtMs: number | null;
84
+ nowMs: number;
85
+ }) => boolean;
86
+ /**
87
+ * The browser storage key of the last post for one address. It names the chain, the factory and the address.
88
+ *
89
+ * @param input.chainId - the chain of the factory
90
+ * @param input.factory - the factory
91
+ * @param input.depositAddress - the deposit address
92
+ */
93
+ export declare const watchPostedAtKeyOf: ({ chainId, factory, depositAddress, }: {
94
+ chainId: bigint;
95
+ factory: Address;
96
+ depositAddress: Address;
97
+ }) => string;
98
+ /** The part of browser storage the throttle reads and writes. */
99
+ export interface WatchStorage {
100
+ getItem: (key: string) => string | null;
101
+ setItem: (key: string, value: string) => void;
102
+ }
103
+ /**
104
+ * When this browser last posted for an address, or null when it never has (or the stored value is not a time).
105
+ *
106
+ * @param storage - the browser storage
107
+ * @param key - the key from {@link watchPostedAtKeyOf}
108
+ */
109
+ export declare const readWatchPostedAtMs: (storage: WatchStorage, key: string) => number | null;
110
+ /** The outcome of one attempt to post a watch message. */
111
+ export type WatchPostOutcome = 'posted' | 'not-on-board' | 'recently-posted';
112
+ /**
113
+ * Posts the watch message for one address, unless the chain has no board or this browser posted for the address within
114
+ * the hour. The time is recorded only after the post succeeds, so a failed post does not block the next attempt. A failed
115
+ * post rejects, and the caller shows the failure.
116
+ *
117
+ * @param input.chainId - the chain of the factory
118
+ * @param input.factory - the factory
119
+ * @param input.depositAddress - the address the message describes
120
+ * @param input.message - the watch message to post
121
+ * @param input.storage - the browser storage that holds the last post time
122
+ * @param input.nowMs - the current time
123
+ * @param input.post - posts the encoded body
124
+ */
125
+ export declare const postWatchMessageIfDue: ({ chainId, factory, depositAddress, message, storage, nowMs, post, }: {
126
+ chainId: bigint;
127
+ factory: Address;
128
+ depositAddress: Address;
129
+ message: WatchMessage;
130
+ storage: WatchStorage;
131
+ nowMs: number;
132
+ post: (body: Hex) => Promise<unknown>;
133
+ }) => Promise<WatchPostOutcome>;
@@ -0,0 +1,44 @@
1
+ import { decodeAbiParameters, encodeAbiParameters, keccak256, toHex } from 'viem';
2
+ import { EXCHANGE_DEPOSIT_BOARD_MAINNET_CHAIN_ID } from './exchange-deposit-board.js';
3
+ import { NO_FEE_RATE_E18 } from './exchange-deposit-self-cross.js';
4
+ import { SETTINGS_COMPONENTS } from './exchange-deposit-factory-calls.js';
5
+ export { factorySettingsTupleOf } from './exchange-deposit-factory-calls.js';
6
+ export { exchangeDepositFactoryOf } from './exchange-deposit-factory.js';
7
+ export const WATCH_CATEGORY_NAME = 'gibs-deposit-watch-message-v1';
8
+ export const WATCH_CATEGORY = keccak256(toHex(WATCH_CATEGORY_NAME));
9
+ export const WATCH_PAGE_VERSION = 1n;
10
+ export const WATCH_WINDOW_SECONDS = 86_400;
11
+ export const WATCH_REPOST_INTERVAL_MS = 3_600_000;
12
+ const WATCH_MESSAGE_PARAMETERS = [
13
+ { name: 'chainId', type: 'uint256' },
14
+ { name: 'factory', type: 'address' },
15
+ { name: 'pageVersion', type: 'uint256' },
16
+ { name: 'settings', type: 'tuple', components: SETTINGS_COMPONENTS },
17
+ ];
18
+ export const encodeWatchMessage = (message) => encodeAbiParameters(WATCH_MESSAGE_PARAMETERS, [message.chainId, message.factory, message.pageVersion, message.settings]);
19
+ export const decodeWatchMessage = (body) => {
20
+ const [chainId, factory, pageVersion, settings] = decodeAbiParameters(WATCH_MESSAGE_PARAMETERS, body);
21
+ return { chainId, factory, pageVersion, settings };
22
+ };
23
+ export const watchMessageRecomputes = async ({ message, claimedAddress, depositAddressOf, }) => (await depositAddressOf(message.settings)).toLowerCase() === claimedAddress.toLowerCase();
24
+ export const watchPostsOnChain = (chainId) => chainId === EXCHANGE_DEPOSIT_BOARD_MAINNET_CHAIN_ID;
25
+ export const watchPostsForFeeRate = ({ feeRateE18 }) => feeRateE18 !== NO_FEE_RATE_E18;
26
+ export const watchPostDue = ({ postedAtMs, nowMs }) => postedAtMs === null || nowMs - postedAtMs >= WATCH_REPOST_INTERVAL_MS;
27
+ export const watchPostedAtKeyOf = ({ chainId, factory, depositAddress, }) => `gibs.exchange-deposit.watch-posted.v1:${chainId.toString()}:${factory.toLowerCase()}:${depositAddress.toLowerCase()}`;
28
+ export const readWatchPostedAtMs = (storage, key) => {
29
+ const stored = storage.getItem(key);
30
+ if (stored === null)
31
+ return null;
32
+ const value = Number(stored);
33
+ return Number.isFinite(value) ? value : null;
34
+ };
35
+ export const postWatchMessageIfDue = async ({ chainId, factory, depositAddress, message, storage, nowMs, post, }) => {
36
+ if (!watchPostsOnChain(chainId))
37
+ return 'not-on-board';
38
+ const key = watchPostedAtKeyOf({ chainId, factory, depositAddress });
39
+ if (!watchPostDue({ postedAtMs: readWatchPostedAtMs(storage, key), nowMs }))
40
+ return 'recently-posted';
41
+ await post(encodeWatchMessage(message));
42
+ storage.setItem(key, String(nowMs));
43
+ return 'posted';
44
+ };