@gibs/bridge-client 1.30.1 → 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.
- package/coinbase-accept-list/package.json +6 -0
- package/dist/cjs/client.d.ts +9 -1
- package/dist/cjs/client.js +8 -5
- package/dist/cjs/coinbase-accept-list.d.ts +101 -0
- package/dist/cjs/coinbase-accept-list.js +97 -0
- package/dist/cjs/crossing-decision.d.ts +166 -0
- package/dist/cjs/crossing-decision.js +169 -0
- package/dist/cjs/crossing-vector-guard.d.ts +45 -0
- package/dist/cjs/crossing-vector-guard.js +139 -0
- package/dist/cjs/exchange-deposit-board.d.ts +50 -0
- package/dist/cjs/exchange-deposit-board.js +70 -0
- package/dist/cjs/exchange-deposit-chain.d.ts +146 -0
- package/dist/cjs/exchange-deposit-chain.js +228 -0
- package/dist/cjs/exchange-deposit-constants.d.ts +10 -0
- package/dist/cjs/exchange-deposit-constants.js +6 -0
- package/dist/cjs/exchange-deposit-factory-calls.d.ts +245 -0
- package/dist/cjs/exchange-deposit-factory-calls.js +103 -0
- package/dist/cjs/exchange-deposit-factory.d.ts +87 -0
- package/dist/cjs/exchange-deposit-factory.js +110 -0
- package/dist/cjs/exchange-deposit-fake.d.ts +67 -0
- package/dist/cjs/exchange-deposit-fake.js +179 -0
- package/dist/cjs/exchange-deposit-index.d.ts +77 -0
- package/dist/cjs/exchange-deposit-index.js +40 -0
- package/dist/cjs/exchange-deposit-issuance.d.ts +327 -0
- package/dist/cjs/exchange-deposit-issuance.js +244 -0
- package/dist/cjs/exchange-deposit-path.d.ts +16 -0
- package/dist/cjs/exchange-deposit-path.js +15 -0
- package/dist/cjs/exchange-deposit-pow.d.ts +163 -0
- package/dist/cjs/exchange-deposit-pow.js +101 -0
- package/dist/cjs/exchange-deposit-recipient.d.ts +66 -0
- package/dist/cjs/exchange-deposit-recipient.js +35 -0
- package/dist/cjs/exchange-deposit-self-cross.d.ts +163 -0
- package/dist/cjs/exchange-deposit-self-cross.js +60 -0
- package/dist/cjs/exchange-deposit-signin.d.ts +275 -0
- package/dist/cjs/exchange-deposit-signin.js +166 -0
- package/dist/cjs/exchange-deposit-watch.d.ts +133 -0
- package/dist/cjs/exchange-deposit-watch.js +58 -0
- package/dist/cjs/exchange-deposit.d.ts +440 -0
- package/dist/cjs/exchange-deposit.js +154 -0
- package/dist/cjs/exchange-sender-guides.d.ts +29 -0
- package/dist/cjs/exchange-sender-guides.js +25 -0
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.js +24 -1
- package/dist/cjs/kraken-accept-list.d.ts +70 -0
- package/dist/cjs/kraken-accept-list.js +24 -0
- package/dist/client.d.ts +9 -1
- package/dist/client.js +8 -5
- package/dist/coinbase-accept-list.d.ts +101 -0
- package/dist/coinbase-accept-list.js +89 -0
- package/dist/crossing-decision.d.ts +166 -0
- package/dist/crossing-decision.js +162 -0
- package/dist/crossing-vector-guard.d.ts +45 -0
- package/dist/crossing-vector-guard.js +134 -0
- package/dist/exchange-deposit-board.d.ts +50 -0
- package/dist/exchange-deposit-board.js +28 -0
- package/dist/exchange-deposit-chain.d.ts +146 -0
- package/dist/exchange-deposit-chain.js +218 -0
- package/dist/exchange-deposit-constants.d.ts +10 -0
- package/dist/exchange-deposit-constants.js +3 -0
- package/dist/exchange-deposit-factory-calls.d.ts +245 -0
- package/dist/exchange-deposit-factory-calls.js +97 -0
- package/dist/exchange-deposit-factory.d.ts +87 -0
- package/dist/exchange-deposit-factory.js +104 -0
- package/dist/exchange-deposit-fake.d.ts +67 -0
- package/dist/exchange-deposit-fake.js +171 -0
- package/dist/exchange-deposit-index.d.ts +77 -0
- package/dist/exchange-deposit-index.js +34 -0
- package/dist/exchange-deposit-issuance.d.ts +327 -0
- package/dist/exchange-deposit-issuance.js +224 -0
- package/dist/exchange-deposit-path.d.ts +16 -0
- package/dist/exchange-deposit-path.js +11 -0
- package/dist/exchange-deposit-pow.d.ts +163 -0
- package/dist/exchange-deposit-pow.js +93 -0
- package/dist/exchange-deposit-recipient.d.ts +66 -0
- package/dist/exchange-deposit-recipient.js +28 -0
- package/dist/exchange-deposit-self-cross.d.ts +163 -0
- package/dist/exchange-deposit-self-cross.js +46 -0
- package/dist/exchange-deposit-signin.d.ts +275 -0
- package/dist/exchange-deposit-signin.js +147 -0
- package/dist/exchange-deposit-watch.d.ts +133 -0
- package/dist/exchange-deposit-watch.js +44 -0
- package/dist/exchange-deposit.d.ts +440 -0
- package/dist/exchange-deposit.js +138 -0
- package/dist/exchange-sender-guides.d.ts +29 -0
- package/dist/exchange-sender-guides.js +21 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/kraken-accept-list.d.ts +70 -0
- package/dist/kraken-accept-list.js +17 -0
- package/exchange-deposit/package.json +6 -0
- package/exchange-deposit-board/package.json +6 -0
- package/exchange-deposit-chain/package.json +6 -0
- package/exchange-deposit-factory/package.json +6 -0
- package/exchange-deposit-fake/package.json +6 -0
- package/exchange-deposit-issuance/package.json +6 -0
- package/exchange-deposit-path/package.json +6 -0
- package/exchange-deposit-pow/package.json +6 -0
- package/exchange-deposit-recipient/package.json +6 -0
- package/exchange-deposit-self-cross/package.json +6 -0
- package/exchange-deposit-signin/package.json +6 -0
- package/exchange-deposit-watch/package.json +6 -0
- package/exchange-sender-guides/package.json +6 -0
- package/kraken-accept-list/package.json +6 -0
- package/package.json +429 -4
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { type Address } from 'viem';
|
|
2
|
+
/**
|
|
3
|
+
* The fee rate of an address that takes no fee. The validators do not cross it, so the reader crosses it alone and
|
|
4
|
+
* pays the Ethereum gas. Zero is 0 percent, in 1e18 fixed point.
|
|
5
|
+
*/
|
|
6
|
+
export declare const NO_FEE_RATE_E18 = 0n;
|
|
7
|
+
/**
|
|
8
|
+
* How many times the flush gas the reader's ether must cover before "No fee" becomes the default. The margin leaves
|
|
9
|
+
* room for the gas price to rise between the page's read and the press.
|
|
10
|
+
*/
|
|
11
|
+
export declare const SELF_CROSS_GAS_MARGIN_MULTIPLE = 2n;
|
|
12
|
+
/** The deposit amount that the "per $100" figure states a fee for, when the reader's amount is not known. */
|
|
13
|
+
export declare const PER_HUNDRED_DOLLARS = 100;
|
|
14
|
+
/** The flush of one address, read from the gas table of its network. */
|
|
15
|
+
export type SelfCrossInput = {
|
|
16
|
+
/** The Ethereum chain the flush runs on. */
|
|
17
|
+
readonly chainId: bigint;
|
|
18
|
+
/** The token of the deposit. The zero address is ether. */
|
|
19
|
+
readonly tokenAddress: Address;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* The gas limit of one flush, before the refund, from the network's table. Use it for the limit and for the
|
|
23
|
+
* affordability check, because the node needs that much available while the flush runs.
|
|
24
|
+
*
|
|
25
|
+
* @param input.chainId - the Ethereum chain the flush runs on
|
|
26
|
+
* @param input.tokenAddress - the token of the deposit
|
|
27
|
+
* @throws when the interface chose no gas table for the chain
|
|
28
|
+
*/
|
|
29
|
+
export declare const selfCrossGasLimitOf: ({ chainId, tokenAddress }: SelfCrossInput) => bigint;
|
|
30
|
+
/**
|
|
31
|
+
* The gas one flush really uses, after the refund. Use it for the cost the reader sees.
|
|
32
|
+
*
|
|
33
|
+
* @param input.chainId - the Ethereum chain the flush runs on
|
|
34
|
+
* @param input.tokenAddress - the token of the deposit
|
|
35
|
+
* @throws when the interface chose no gas table for the chain
|
|
36
|
+
*/
|
|
37
|
+
export declare const selfCrossPaidGasOf: ({ chainId, tokenAddress }: SelfCrossInput) => bigint;
|
|
38
|
+
/**
|
|
39
|
+
* The cost, in wei of ether, of crossing one address alone at a gas price.
|
|
40
|
+
*
|
|
41
|
+
* @param input.chainId - the Ethereum chain the flush runs on
|
|
42
|
+
* @param input.tokenAddress - the token of the deposit
|
|
43
|
+
* @param input.gasPriceWei - the gas price per unit, in wei
|
|
44
|
+
*/
|
|
45
|
+
export declare const selfCrossCostWeiOf: ({ chainId, tokenAddress, gasPriceWei, }: SelfCrossInput & {
|
|
46
|
+
readonly gasPriceWei: bigint;
|
|
47
|
+
}) => bigint;
|
|
48
|
+
/**
|
|
49
|
+
* Whether the reader's ether on the network covers the flush gas, with the margin, at the gas price now. An unread
|
|
50
|
+
* gas price is never affordable.
|
|
51
|
+
*
|
|
52
|
+
* @param input.balanceWei - the reader's ether on the network, in wei
|
|
53
|
+
* @param input.chainId - the Ethereum chain the flush runs on
|
|
54
|
+
* @param input.tokenAddress - the token of the deposit
|
|
55
|
+
* @param input.gasPriceWei - the gas price now, in wei per unit, or null when the page has not read it
|
|
56
|
+
*/
|
|
57
|
+
export declare const isSelfCrossAffordable: ({ balanceWei, chainId, tokenAddress, gasPriceWei, }: SelfCrossInput & {
|
|
58
|
+
readonly balanceWei: bigint;
|
|
59
|
+
readonly gasPriceWei: bigint | null;
|
|
60
|
+
}) => boolean;
|
|
61
|
+
/**
|
|
62
|
+
* The fee rate an address starts with. "No fee" is the default only when the reader can pay the gas to cross it
|
|
63
|
+
* alone. Otherwise the default is the highest preset, as before, and the page says why.
|
|
64
|
+
*
|
|
65
|
+
* @param input.isSelfCrossAffordable - the result of {@link isSelfCrossAffordable}
|
|
66
|
+
*/
|
|
67
|
+
export declare const exchangeDepositDefaultFeeRateOf: ({ isSelfCrossAffordable: affordable, }: {
|
|
68
|
+
readonly isSelfCrossAffordable: boolean;
|
|
69
|
+
}) => bigint;
|
|
70
|
+
/**
|
|
71
|
+
* The dollar amount of a fee. With the deposit's dollar value it is the fee on that value. Without it, the fee on
|
|
72
|
+
* {@link PER_HUNDRED_DOLLARS}, which the interface shows as "per $100".
|
|
73
|
+
*
|
|
74
|
+
* @param input.feeRateE18 - the fee rate, in 1e18 fixed point
|
|
75
|
+
* @param input.depositUsd - the deposit's dollar value, or null when the amount is not known
|
|
76
|
+
*/
|
|
77
|
+
export declare const feeDollarsOf: ({ feeRateE18, depositUsd, }: {
|
|
78
|
+
readonly feeRateE18: bigint;
|
|
79
|
+
readonly depositUsd: number | null;
|
|
80
|
+
}) => number;
|
|
81
|
+
/**
|
|
82
|
+
* The dollar cost of crossing one address alone at a gas price, using the ether price in dollars.
|
|
83
|
+
*
|
|
84
|
+
* @param input.chainId - the Ethereum chain the flush runs on
|
|
85
|
+
* @param input.tokenAddress - the token of the deposit
|
|
86
|
+
* @param input.gasPriceWei - the gas price per unit, in wei
|
|
87
|
+
* @param input.etherUsd - one ether in dollars
|
|
88
|
+
*/
|
|
89
|
+
export declare const selfCrossDollarsOf: ({ chainId, tokenAddress, gasPriceWei, etherUsd, }: SelfCrossInput & {
|
|
90
|
+
readonly gasPriceWei: bigint;
|
|
91
|
+
readonly etherUsd: number;
|
|
92
|
+
}) => number;
|
|
93
|
+
/** Why "No fee" is or is not the default: it is the default only when the reader can pay the flush. */
|
|
94
|
+
export type SelfCrossReason = 'affordable' | 'no-balance' | 'short-of-ether' | 'no-gas-price';
|
|
95
|
+
/** The affordability of crossing one address alone, read from the page's live figures. */
|
|
96
|
+
export type SelfCrossState = {
|
|
97
|
+
/** Why the default is or is not "No fee". */
|
|
98
|
+
readonly reason: SelfCrossReason;
|
|
99
|
+
/** Whether "No fee" is the default. True only for the reason `affordable`. */
|
|
100
|
+
readonly affordable: boolean;
|
|
101
|
+
/** The gas price per unit, in wei: the base fee plus the tip. Null when the page has not read the fees. */
|
|
102
|
+
readonly gasPriceWei: bigint | null;
|
|
103
|
+
/** The gas one flush costs at that price, in wei. Null when the gas price is not known. */
|
|
104
|
+
readonly costWei: bigint | null;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* The gas price a crossing pays now, in wei per unit: the Ethereum base fee plus the tip.
|
|
108
|
+
*
|
|
109
|
+
* @param input.baseFeeWei - the base fee, in wei per gas
|
|
110
|
+
* @param input.priorityFeeWei - the tip, in wei per gas
|
|
111
|
+
*/
|
|
112
|
+
export declare const gasPriceWeiOf: ({ baseFeeWei, priorityFeeWei, }: {
|
|
113
|
+
readonly baseFeeWei: bigint;
|
|
114
|
+
readonly priorityFeeWei: bigint;
|
|
115
|
+
}) => bigint;
|
|
116
|
+
/**
|
|
117
|
+
* The affordability of crossing one address alone, from the reader's ether on Ethereum and the live gas price. The
|
|
118
|
+
* reason is checked in order: no gas price, then no balance (no wallet, or not read yet), then the ether short of the
|
|
119
|
+
* margin. The interface shows the reason and never decides it.
|
|
120
|
+
*
|
|
121
|
+
* @param input.balanceWei - the reader's ether on Ethereum, in wei, or null when there is no wallet or it is not read
|
|
122
|
+
* @param input.chainId - the Ethereum chain the flush runs on
|
|
123
|
+
* @param input.tokenAddress - the token of the deposit
|
|
124
|
+
* @param input.gasPriceWei - the gas price now, in wei per unit, or null when the page has not read it
|
|
125
|
+
*/
|
|
126
|
+
export declare const selfCrossStateOf: ({ balanceWei, chainId, tokenAddress, gasPriceWei, }: SelfCrossInput & {
|
|
127
|
+
readonly balanceWei: bigint | null;
|
|
128
|
+
readonly gasPriceWei: bigint | null;
|
|
129
|
+
}) => SelfCrossState;
|
|
130
|
+
/** The state of one live read: not answered yet, answered, or refused. */
|
|
131
|
+
export type SelfCrossRead = 'pending' | 'loaded' | 'failed';
|
|
132
|
+
/** The state of the balance read. With no wallet there is nothing to read, so the state is `no-wallet`. */
|
|
133
|
+
export type SelfCrossBalanceRead = SelfCrossRead | 'no-wallet';
|
|
134
|
+
/**
|
|
135
|
+
* Whether the page has decided the starting fee rate. `deciding` means a read is still open, so the page shows no
|
|
136
|
+
* address. `no-fee` and `default-rate` are final once the page latches them.
|
|
137
|
+
*/
|
|
138
|
+
export type SelfCrossDecision = 'deciding' | 'no-fee' | 'default-rate';
|
|
139
|
+
/**
|
|
140
|
+
* The starting fee decision from the state of the reads. It waits while the gas or the balance is still pending. A
|
|
141
|
+
* failed read settles on the default rate at once, because the reader cannot be shown a figure that was never read.
|
|
142
|
+
* With no wallet the balance is absent, so the default rate stands.
|
|
143
|
+
*
|
|
144
|
+
* @param input.gasRead - the state of the gas price read
|
|
145
|
+
* @param input.balanceRead - the state of the balance read, or `no-wallet`
|
|
146
|
+
* @param input.affordable - whether the reader can cross alone, from {@link selfCrossStateOf}; read only when both reads loaded
|
|
147
|
+
*/
|
|
148
|
+
export declare const selfCrossDecisionOf: ({ gasRead, balanceRead, affordable, }: {
|
|
149
|
+
readonly gasRead: SelfCrossRead;
|
|
150
|
+
readonly balanceRead: SelfCrossBalanceRead;
|
|
151
|
+
readonly affordable: boolean;
|
|
152
|
+
}) => SelfCrossDecision;
|
|
153
|
+
/**
|
|
154
|
+
* The decision the page keeps. Once a decision is settled it stays, whatever the live reads say later. Only a
|
|
155
|
+
* reader's own choice of fee changes the rate after that.
|
|
156
|
+
*
|
|
157
|
+
* @param input.settled - the decision the page already settled, or `deciding` when none
|
|
158
|
+
* @param input.live - the decision from the reads now, from {@link selfCrossDecisionOf}
|
|
159
|
+
*/
|
|
160
|
+
export declare const settledSelfCrossDecisionOf: ({ settled, live, }: {
|
|
161
|
+
readonly settled: SelfCrossDecision;
|
|
162
|
+
readonly live: SelfCrossDecision;
|
|
163
|
+
}) => SelfCrossDecision;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.settledSelfCrossDecisionOf = exports.selfCrossDecisionOf = exports.selfCrossStateOf = exports.gasPriceWeiOf = exports.selfCrossDollarsOf = exports.feeDollarsOf = exports.exchangeDepositDefaultFeeRateOf = exports.isSelfCrossAffordable = exports.selfCrossCostWeiOf = exports.selfCrossPaidGasOf = exports.selfCrossGasLimitOf = exports.PER_HUNDRED_DOLLARS = exports.SELF_CROSS_GAS_MARGIN_MULTIPLE = exports.NO_FEE_RATE_E18 = void 0;
|
|
4
|
+
const viem_1 = require("viem");
|
|
5
|
+
const exchange_deposit_js_1 = require("./exchange-deposit.js");
|
|
6
|
+
const exchange_deposit_constants_js_1 = require("./exchange-deposit-constants.js");
|
|
7
|
+
exports.NO_FEE_RATE_E18 = 0n;
|
|
8
|
+
exports.SELF_CROSS_GAS_MARGIN_MULTIPLE = 2n;
|
|
9
|
+
exports.PER_HUNDRED_DOLLARS = 100;
|
|
10
|
+
const isEtherAddress = (tokenAddress) => tokenAddress.toLowerCase() === viem_1.zeroAddress;
|
|
11
|
+
const selfCrossGasLimitOf = ({ chainId, tokenAddress }) => {
|
|
12
|
+
const { beforeRefund } = (0, exchange_deposit_js_1.exchangeDepositGasTableOf)(chainId).gasModel;
|
|
13
|
+
return isEtherAddress(tokenAddress) ? beforeRefund.etherFlushGas : beforeRefund.tokenFlushGas;
|
|
14
|
+
};
|
|
15
|
+
exports.selfCrossGasLimitOf = selfCrossGasLimitOf;
|
|
16
|
+
const selfCrossPaidGasOf = ({ chainId, tokenAddress }) => {
|
|
17
|
+
const { paid } = (0, exchange_deposit_js_1.exchangeDepositGasTableOf)(chainId).gasModel;
|
|
18
|
+
return isEtherAddress(tokenAddress) ? paid.etherFlushGas : paid.tokenFlushGas;
|
|
19
|
+
};
|
|
20
|
+
exports.selfCrossPaidGasOf = selfCrossPaidGasOf;
|
|
21
|
+
const selfCrossCostWeiOf = ({ chainId, tokenAddress, gasPriceWei, }) => (0, exports.selfCrossPaidGasOf)({ chainId, tokenAddress }) * gasPriceWei;
|
|
22
|
+
exports.selfCrossCostWeiOf = selfCrossCostWeiOf;
|
|
23
|
+
const isSelfCrossAffordable = ({ balanceWei, chainId, tokenAddress, gasPriceWei, }) => {
|
|
24
|
+
if (gasPriceWei === null)
|
|
25
|
+
return false;
|
|
26
|
+
const requiredWei = (0, exports.selfCrossGasLimitOf)({ chainId, tokenAddress }) * gasPriceWei * exports.SELF_CROSS_GAS_MARGIN_MULTIPLE;
|
|
27
|
+
return balanceWei >= requiredWei;
|
|
28
|
+
};
|
|
29
|
+
exports.isSelfCrossAffordable = isSelfCrossAffordable;
|
|
30
|
+
const exchangeDepositDefaultFeeRateOf = ({ isSelfCrossAffordable: affordable, }) => (affordable ? exports.NO_FEE_RATE_E18 : exchange_deposit_js_1.EXCHANGE_DEPOSIT_DEFAULT_FEE_RATE_E18);
|
|
31
|
+
exports.exchangeDepositDefaultFeeRateOf = exchangeDepositDefaultFeeRateOf;
|
|
32
|
+
const feeDollarsOf = ({ feeRateE18, depositUsd, }) => {
|
|
33
|
+
const basisUsd = depositUsd ?? exports.PER_HUNDRED_DOLLARS;
|
|
34
|
+
return (basisUsd * Number(feeRateE18)) / Number(exchange_deposit_js_1.FEE_RATE_SCALE);
|
|
35
|
+
};
|
|
36
|
+
exports.feeDollarsOf = feeDollarsOf;
|
|
37
|
+
const selfCrossDollarsOf = ({ chainId, tokenAddress, gasPriceWei, etherUsd, }) => (Number((0, exports.selfCrossCostWeiOf)({ chainId, tokenAddress, gasPriceWei })) / Number(exchange_deposit_constants_js_1.WEI_PER_ETHER)) * etherUsd;
|
|
38
|
+
exports.selfCrossDollarsOf = selfCrossDollarsOf;
|
|
39
|
+
const gasPriceWeiOf = ({ baseFeeWei, priorityFeeWei, }) => baseFeeWei + priorityFeeWei;
|
|
40
|
+
exports.gasPriceWeiOf = gasPriceWeiOf;
|
|
41
|
+
const selfCrossStateOf = ({ balanceWei, chainId, tokenAddress, gasPriceWei, }) => {
|
|
42
|
+
if (gasPriceWei === null)
|
|
43
|
+
return { reason: 'no-gas-price', affordable: false, gasPriceWei, costWei: null };
|
|
44
|
+
const costWei = (0, exports.selfCrossCostWeiOf)({ chainId, tokenAddress, gasPriceWei });
|
|
45
|
+
if (balanceWei === null)
|
|
46
|
+
return { reason: 'no-balance', affordable: false, gasPriceWei, costWei };
|
|
47
|
+
const affordable = (0, exports.isSelfCrossAffordable)({ balanceWei, chainId, tokenAddress, gasPriceWei });
|
|
48
|
+
return { reason: affordable ? 'affordable' : 'short-of-ether', affordable, gasPriceWei, costWei };
|
|
49
|
+
};
|
|
50
|
+
exports.selfCrossStateOf = selfCrossStateOf;
|
|
51
|
+
const selfCrossDecisionOf = ({ gasRead, balanceRead, affordable, }) => {
|
|
52
|
+
if (gasRead === 'failed' || balanceRead === 'failed')
|
|
53
|
+
return 'default-rate';
|
|
54
|
+
if (gasRead === 'pending' || balanceRead === 'pending')
|
|
55
|
+
return 'deciding';
|
|
56
|
+
return affordable ? 'no-fee' : 'default-rate';
|
|
57
|
+
};
|
|
58
|
+
exports.selfCrossDecisionOf = selfCrossDecisionOf;
|
|
59
|
+
const settledSelfCrossDecisionOf = ({ settled, live, }) => (settled === 'deciding' ? live : settled);
|
|
60
|
+
exports.settledSelfCrossDecisionOf = settledSelfCrossDecisionOf;
|
|
@@ -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,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.signinOutcomeOfRandomSeed = exports.signinOutcomeOfSignature = exports.signinOutcomeOfUnverifiedDelegation = exports.writeStoredSigninRecord = exports.readStoredSigninRecord = exports.signinRecordIsOf = exports.exchangeDepositSeedOfRecord = exports.withRecordedHighestCounter = exports.recordedHighestCounterOf = exports.serializeStoredSigninRecord = exports.parseStoredSigninRecord = exports.exchangeDepositSigninStorageKey = exports.exchangeDepositSignatureIsFrom = exports.exchangeDepositSeedOfRandomBytes = exports.exchangeDepositSeedOfSignature = exports.exchangeDepositSigninTypedData = exports.EXCHANGE_DEPOSIT_SIGNIN_TYPES = exports.EXCHANGE_DEPOSIT_SIGNIN_VERSION = void 0;
|
|
4
|
+
const viem_1 = require("viem");
|
|
5
|
+
const exchange_deposit_recipient_js_1 = require("./exchange-deposit-recipient.js");
|
|
6
|
+
exports.EXCHANGE_DEPOSIT_SIGNIN_VERSION = 2;
|
|
7
|
+
const SEED_DOMAIN_TAG = (0, viem_1.keccak256)((0, viem_1.toHex)(`gibs-deposit-seed-v${exports.EXCHANGE_DEPOSIT_SIGNIN_VERSION}`));
|
|
8
|
+
const SECP256K1_HALF_ORDER = 0x7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0n;
|
|
9
|
+
const SECP256K1_ORDER = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n;
|
|
10
|
+
const SEED_BYTE_LENGTH = 32;
|
|
11
|
+
const SIGNIN_PRIMARY_TYPE = 'DepositSignIn';
|
|
12
|
+
exports.EXCHANGE_DEPOSIT_SIGNIN_TYPES = {
|
|
13
|
+
DepositSignIn: [
|
|
14
|
+
{ name: 'recipient', type: 'address' },
|
|
15
|
+
{ name: 'purpose', type: 'string' },
|
|
16
|
+
{ name: 'notice', type: 'string' },
|
|
17
|
+
],
|
|
18
|
+
};
|
|
19
|
+
const SIGNIN_PURPOSE = 'Create Gibs deposit addresses';
|
|
20
|
+
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.';
|
|
21
|
+
const exchangeDepositSigninTypedData = ({ recipient, chainId, factoryAddress, }) => ({
|
|
22
|
+
domain: {
|
|
23
|
+
name: 'Gibs Finance',
|
|
24
|
+
version: '1',
|
|
25
|
+
chainId,
|
|
26
|
+
verifyingContract: (0, viem_1.getAddress)(factoryAddress),
|
|
27
|
+
},
|
|
28
|
+
types: exports.EXCHANGE_DEPOSIT_SIGNIN_TYPES,
|
|
29
|
+
primaryType: SIGNIN_PRIMARY_TYPE,
|
|
30
|
+
message: {
|
|
31
|
+
recipient: (0, viem_1.getAddress)(recipient),
|
|
32
|
+
purpose: SIGNIN_PURPOSE,
|
|
33
|
+
notice: SIGNIN_NOTICE,
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
exports.exchangeDepositSigninTypedData = exchangeDepositSigninTypedData;
|
|
37
|
+
const exchangeDepositSeedOfSignature = (signature) => {
|
|
38
|
+
const { r, s } = (0, viem_1.parseSignature)(signature);
|
|
39
|
+
const sValue = BigInt(s);
|
|
40
|
+
const lowS = sValue > SECP256K1_HALF_ORDER ? SECP256K1_ORDER - sValue : sValue;
|
|
41
|
+
return (0, viem_1.keccak256)((0, viem_1.encodeAbiParameters)([{ type: 'bytes32' }, { type: 'bytes32' }, { type: 'bytes32' }], [SEED_DOMAIN_TAG, (0, viem_1.toHex)(BigInt(r), { size: SEED_BYTE_LENGTH }), (0, viem_1.toHex)(lowS, { size: SEED_BYTE_LENGTH })]));
|
|
42
|
+
};
|
|
43
|
+
exports.exchangeDepositSeedOfSignature = exchangeDepositSeedOfSignature;
|
|
44
|
+
const exchangeDepositSeedOfRandomBytes = (randomBytes) => {
|
|
45
|
+
if (randomBytes.length !== SEED_BYTE_LENGTH) {
|
|
46
|
+
throw new Error(`a random seed needs ${SEED_BYTE_LENGTH} bytes, not ${randomBytes.length}`);
|
|
47
|
+
}
|
|
48
|
+
return (0, viem_1.bytesToHex)(randomBytes);
|
|
49
|
+
};
|
|
50
|
+
exports.exchangeDepositSeedOfRandomBytes = exchangeDepositSeedOfRandomBytes;
|
|
51
|
+
const exchangeDepositSignatureIsFrom = async ({ signature, typedData, signer, }) => {
|
|
52
|
+
try {
|
|
53
|
+
return (0, viem_1.isAddressEqual)(await (0, viem_1.recoverTypedDataAddress)({ ...typedData, signature }), signer);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
exports.exchangeDepositSignatureIsFrom = exchangeDepositSignatureIsFrom;
|
|
60
|
+
const STORAGE_KEY_PREFIX = `gibs.exchange-deposit.signin.v${exports.EXCHANGE_DEPOSIT_SIGNIN_VERSION}`;
|
|
61
|
+
const exchangeDepositSigninStorageKey = ({ chainId, factoryAddress, recipient, signer, }) => `${STORAGE_KEY_PREFIX}:${chainId.toString()}:${factoryAddress.toLowerCase()}:${recipient.toLowerCase()}:${signer.toLowerCase()}`;
|
|
62
|
+
exports.exchangeDepositSigninStorageKey = exchangeDepositSigninStorageKey;
|
|
63
|
+
const parseStoredSigninRecord = (text) => {
|
|
64
|
+
if (text === null)
|
|
65
|
+
return null;
|
|
66
|
+
const parsed = parseJsonOrNull(text);
|
|
67
|
+
if (parsed === null || typeof parsed !== 'object')
|
|
68
|
+
return null;
|
|
69
|
+
const { signer, method, seed, highestIssuedCounters } = parsed;
|
|
70
|
+
if (typeof signer !== 'string' || !(0, viem_1.isAddress)(signer))
|
|
71
|
+
return null;
|
|
72
|
+
if (method !== 'signature' && method !== 'random')
|
|
73
|
+
return null;
|
|
74
|
+
if (!isHexOfLength(seed, SEED_BYTE_LENGTH))
|
|
75
|
+
return null;
|
|
76
|
+
const record = { signer, method, seed };
|
|
77
|
+
const counters = parseHighestIssuedCounters(highestIssuedCounters);
|
|
78
|
+
if (counters === undefined)
|
|
79
|
+
return record;
|
|
80
|
+
return { ...record, highestIssuedCounters: counters };
|
|
81
|
+
};
|
|
82
|
+
exports.parseStoredSigninRecord = parseStoredSigninRecord;
|
|
83
|
+
const parseHighestIssuedCounters = (value) => {
|
|
84
|
+
if (value === undefined)
|
|
85
|
+
return undefined;
|
|
86
|
+
if (!Array.isArray(value))
|
|
87
|
+
return [];
|
|
88
|
+
return value.map(parseHighestIssuedCounterRow).filter((row) => row !== null);
|
|
89
|
+
};
|
|
90
|
+
const parseHighestIssuedCounterRow = (row) => {
|
|
91
|
+
if (row === null || typeof row !== 'object')
|
|
92
|
+
return null;
|
|
93
|
+
const { tokenAddress, feeRateE18, counter } = row;
|
|
94
|
+
if (typeof tokenAddress !== 'string' || !(0, viem_1.isAddress)(tokenAddress))
|
|
95
|
+
return null;
|
|
96
|
+
if (typeof feeRateE18 !== 'string' || !/^[0-9]+$/.test(feeRateE18))
|
|
97
|
+
return null;
|
|
98
|
+
if (typeof counter !== 'number' || !Number.isSafeInteger(counter) || counter < 0)
|
|
99
|
+
return null;
|
|
100
|
+
return { tokenAddress: (0, viem_1.getAddress)(tokenAddress), feeRateE18: BigInt(feeRateE18), counter };
|
|
101
|
+
};
|
|
102
|
+
const parseJsonOrNull = (text) => {
|
|
103
|
+
try {
|
|
104
|
+
return JSON.parse(text);
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
const isHexOfLength = (value, byteLength) => typeof value === 'string' && (0, viem_1.isHex)(value) && value.length === 2 + byteLength * 2;
|
|
111
|
+
const serializeStoredSigninRecord = (record) => JSON.stringify({
|
|
112
|
+
...record,
|
|
113
|
+
highestIssuedCounters: record.highestIssuedCounters?.map((counter) => ({
|
|
114
|
+
tokenAddress: counter.tokenAddress,
|
|
115
|
+
feeRateE18: counter.feeRateE18.toString(),
|
|
116
|
+
counter: counter.counter,
|
|
117
|
+
})),
|
|
118
|
+
});
|
|
119
|
+
exports.serializeStoredSigninRecord = serializeStoredSigninRecord;
|
|
120
|
+
const isCounterForSetting = (row, setting) => (0, exchange_deposit_recipient_js_1.sameAddress)(row.tokenAddress, setting.tokenAddress) && row.feeRateE18 === setting.feeRateE18;
|
|
121
|
+
const recordedHighestCounterOf = ({ record, tokenAddress, feeRateE18, }) => {
|
|
122
|
+
const matching = (record.highestIssuedCounters ?? []).filter((row) => isCounterForSetting(row, { tokenAddress, feeRateE18 }));
|
|
123
|
+
if (matching.length === 0)
|
|
124
|
+
return null;
|
|
125
|
+
return Math.max(...matching.map((row) => row.counter));
|
|
126
|
+
};
|
|
127
|
+
exports.recordedHighestCounterOf = recordedHighestCounterOf;
|
|
128
|
+
const withRecordedHighestCounter = ({ record, tokenAddress, feeRateE18, counter, }) => {
|
|
129
|
+
const recorded = (0, exports.recordedHighestCounterOf)({ record, tokenAddress, feeRateE18 });
|
|
130
|
+
if (recorded !== null && recorded >= counter)
|
|
131
|
+
return record;
|
|
132
|
+
const others = (record.highestIssuedCounters ?? []).filter((row) => !isCounterForSetting(row, { tokenAddress, feeRateE18 }));
|
|
133
|
+
return { ...record, highestIssuedCounters: [...others, { tokenAddress: (0, viem_1.getAddress)(tokenAddress), feeRateE18, counter }] };
|
|
134
|
+
};
|
|
135
|
+
exports.withRecordedHighestCounter = withRecordedHighestCounter;
|
|
136
|
+
const exchangeDepositSeedOfRecord = (record) => record.seed;
|
|
137
|
+
exports.exchangeDepositSeedOfRecord = exchangeDepositSeedOfRecord;
|
|
138
|
+
const signinRecordIsOf = ({ record, signer }) => (0, exchange_deposit_recipient_js_1.sameAddress)(record.signer, signer);
|
|
139
|
+
exports.signinRecordIsOf = signinRecordIsOf;
|
|
140
|
+
const readStoredSigninRecord = ({ storage, key, }) => (0, exports.parseStoredSigninRecord)(storage.getItem(key));
|
|
141
|
+
exports.readStoredSigninRecord = readStoredSigninRecord;
|
|
142
|
+
const writeStoredSigninRecord = ({ storage, key, record, }) => storage.setItem(key, (0, exports.serializeStoredSigninRecord)(record));
|
|
143
|
+
exports.writeStoredSigninRecord = writeStoredSigninRecord;
|
|
144
|
+
const signinOutcomeOfUnverifiedDelegation = ({ signer, existing, randomBytes, }) => {
|
|
145
|
+
if (existing?.method === 'signature')
|
|
146
|
+
return { kind: 'differs' };
|
|
147
|
+
return (0, exports.signinOutcomeOfRandomSeed)({ signer, existing, randomBytes });
|
|
148
|
+
};
|
|
149
|
+
exports.signinOutcomeOfUnverifiedDelegation = signinOutcomeOfUnverifiedDelegation;
|
|
150
|
+
const signinOutcomeOfSignature = ({ signer, signature, existing, }) => {
|
|
151
|
+
const seed = (0, exports.exchangeDepositSeedOfSignature)(signature);
|
|
152
|
+
if (existing === null)
|
|
153
|
+
return { kind: 'signed', record: { signer, method: 'signature', seed } };
|
|
154
|
+
if (existing.method !== 'signature')
|
|
155
|
+
return { kind: 'differs' };
|
|
156
|
+
if (seed !== existing.seed)
|
|
157
|
+
return { kind: 'differs' };
|
|
158
|
+
return { kind: 'signed', record: existing };
|
|
159
|
+
};
|
|
160
|
+
exports.signinOutcomeOfSignature = signinOutcomeOfSignature;
|
|
161
|
+
const signinOutcomeOfRandomSeed = ({ signer, existing, randomBytes, }) => {
|
|
162
|
+
if (existing !== null && existing.method === 'random')
|
|
163
|
+
return { kind: 'signed', record: existing };
|
|
164
|
+
return { kind: 'signed', record: { signer, method: 'random', seed: (0, exports.exchangeDepositSeedOfRandomBytes)(randomBytes) } };
|
|
165
|
+
};
|
|
166
|
+
exports.signinOutcomeOfRandomSeed = signinOutcomeOfRandomSeed;
|