@vordium/sdk 0.1.1 → 0.1.3
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/README.md +15 -2
- package/dist/cjs/client.js +323 -60
- package/dist/cjs/index.js +6 -1
- package/dist/cjs/network.js +53 -1
- package/dist/cjs/nonce.js +419 -1
- package/dist/cjs/orders.js +3 -0
- package/dist/cjs/pins.js +60 -0
- package/dist/cjs/resend.js +188 -0
- package/dist/esm/client.d.ts +85 -9
- package/dist/esm/client.js +323 -63
- package/dist/esm/index.d.ts +3 -1
- package/dist/esm/index.js +3 -1
- package/dist/esm/network.d.ts +23 -4
- package/dist/esm/network.js +51 -1
- package/dist/esm/nonce.d.ts +109 -0
- package/dist/esm/nonce.js +410 -0
- package/dist/esm/orders.js +3 -0
- package/dist/esm/pins.d.ts +29 -0
- package/dist/esm/pins.js +51 -0
- package/dist/esm/resend.d.ts +74 -0
- package/dist/esm/resend.js +180 -0
- package/package.json +1 -1
package/dist/esm/network.d.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { type Transport } from './http.js';
|
|
2
|
+
import { type Pin } from './pins.js';
|
|
2
3
|
/**
|
|
3
|
-
* Where the SDK asks
|
|
4
|
-
*
|
|
5
|
-
* signed
|
|
4
|
+
* Where the SDK asks, and the pin the network is held to. The named networks carry their pins (`PINS`); a preset of
|
|
5
|
+
* your own (another address) names the network it serves (`name: 'mainnet' | 'testnet'`) or carries an explicit `pin`.
|
|
6
|
+
* Before anything is signed, the network spec, the node's `/status` and the JSON-RPC `eth_chainId` must agree with
|
|
7
|
+
* each other and with the pin.
|
|
6
8
|
*/
|
|
7
9
|
export interface NetworkPreset {
|
|
10
|
+
/** 'mainnet' or 'testnet' holds the preset to that network's pin; any other name needs `pin`. */
|
|
8
11
|
name: string;
|
|
9
12
|
/** The network spec (JSON). */
|
|
10
13
|
spec: string;
|
|
@@ -14,16 +17,27 @@ export interface NetworkPreset {
|
|
|
14
17
|
api: string;
|
|
15
18
|
/** The live feeds WebSocket, when the network publishes one. */
|
|
16
19
|
feeds?: string;
|
|
20
|
+
/** An explicit pin, for a network that is not a named one (or the named network's own pin). */
|
|
21
|
+
pin?: Pin;
|
|
17
22
|
}
|
|
18
23
|
export declare const NETWORKS: Record<'mainnet' | 'testnet', NetworkPreset>;
|
|
19
|
-
/** A network the SDK has read and checked. */
|
|
24
|
+
/** A network the SDK has read and checked. `chainId` and `genesis` are what the network reports. */
|
|
20
25
|
export interface Network extends NetworkPreset {
|
|
21
26
|
chainId: number;
|
|
22
27
|
/** 64 lower-case hex, no 0x. */
|
|
23
28
|
genesis: string;
|
|
24
29
|
/** The network spec as read. */
|
|
25
30
|
specRead: Record<string, unknown>;
|
|
31
|
+
/** Why nothing may be signed for this network (its reports differ from its pin, or it has no pin); null when none. */
|
|
32
|
+
pinProblem: string | null;
|
|
26
33
|
}
|
|
34
|
+
/** The pin a preset is held to: its own `pin`, else the pin of the named network it names; null when it has none. */
|
|
35
|
+
export declare function presetPin(p: NetworkPreset): Pin | null;
|
|
36
|
+
/**
|
|
37
|
+
* The pin to sign under for a connected network, after every check. Throws `UnpinnedNetwork` when it has no pin and
|
|
38
|
+
* `PinMismatch` when what it reported differs from the pin; nothing may be signed then.
|
|
39
|
+
*/
|
|
40
|
+
export declare function signingPin(net: Network): Pin;
|
|
27
41
|
export interface NodeIdentity {
|
|
28
42
|
chainId: number;
|
|
29
43
|
genesis: string;
|
|
@@ -37,8 +51,13 @@ export declare function nodeIdentity(api: string, transport?: Transport, timeout
|
|
|
37
51
|
* Read and check a network. Refuses (NetworkMismatch, nothing signed) unless the spec, the node's `/status` and
|
|
38
52
|
* `eth_chainId` name the same chain id, the spec and the node the same genesis, and the spec's `testnet` flag matches
|
|
39
53
|
* the preset (a test network never stands in for the main network, nor the reverse).
|
|
54
|
+
*
|
|
55
|
+
* The reports are then held to the network's pin. When they differ (or the network has no pin) the connection still
|
|
56
|
+
* reads, `pinProblem` says why, `onPinProblem` (when given) is called with it, and every signature is refused
|
|
57
|
+
* (`PinMismatch` / `UnpinnedNetwork`). The SDK prints nothing.
|
|
40
58
|
*/
|
|
41
59
|
export declare function connectNetwork(network?: 'mainnet' | 'testnet' | NetworkPreset, opts?: {
|
|
42
60
|
transport?: Transport;
|
|
43
61
|
timeoutMs?: number;
|
|
62
|
+
onPinProblem?: (message: string) => void;
|
|
44
63
|
}): Promise<Network>;
|
package/dist/esm/network.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { NetworkMismatch, VordiumError } from './errors.js';
|
|
2
2
|
import { fetchTransport } from './http.js';
|
|
3
3
|
import { genesisHex } from './domains.js';
|
|
4
|
+
import { PINS, PinMismatch, UnpinnedNetwork, checkPin, pinDifferences, pinRefusal } from './pins.js';
|
|
4
5
|
export const NETWORKS = {
|
|
5
6
|
mainnet: {
|
|
6
7
|
name: 'mainnet',
|
|
8
|
+
pin: PINS.mainnet,
|
|
7
9
|
spec: 'https://rpc.vordium.com/network-spec.json',
|
|
8
10
|
rpc: 'https://rpc.vordium.com',
|
|
9
11
|
api: 'https://rpc.vordium.com',
|
|
@@ -11,12 +13,44 @@ export const NETWORKS = {
|
|
|
11
13
|
},
|
|
12
14
|
testnet: {
|
|
13
15
|
name: 'testnet',
|
|
16
|
+
pin: PINS.testnet,
|
|
14
17
|
spec: 'https://testnet.vordex.xyz/network-spec.json',
|
|
15
18
|
rpc: 'https://rpc-testnet.vordium.com',
|
|
16
19
|
api: 'https://testnet.vordex.xyz/chain',
|
|
17
20
|
feeds: 'wss://testnet.vordex.xyz/chain/feeds',
|
|
18
21
|
},
|
|
19
22
|
};
|
|
23
|
+
/** The pin a preset is held to: its own `pin`, else the pin of the named network it names; null when it has none. */
|
|
24
|
+
export function presetPin(p) {
|
|
25
|
+
const named = Object.prototype.hasOwnProperty.call(PINS, p.name) ? PINS[p.name] : undefined;
|
|
26
|
+
if (p.pin) {
|
|
27
|
+
const own = checkPin(p.pin);
|
|
28
|
+
if (named && (named.chainId !== own.chainId || named.genesis !== own.genesis)) {
|
|
29
|
+
throw new PinMismatch(`The pin given is not ${p.name}'s pin (chain ${named.chainId}, genesis ${named.genesis}); refusing to sign.`);
|
|
30
|
+
}
|
|
31
|
+
return named ?? own;
|
|
32
|
+
}
|
|
33
|
+
return named ?? null;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The pin to sign under for a connected network, after every check. Throws `UnpinnedNetwork` when it has no pin and
|
|
37
|
+
* `PinMismatch` when what it reported differs from the pin; nothing may be signed then.
|
|
38
|
+
*/
|
|
39
|
+
export function signingPin(net) {
|
|
40
|
+
const pin = presetPin(net);
|
|
41
|
+
if (net.pinProblem)
|
|
42
|
+
throw pin ? new PinMismatch(net.pinProblem) : new UnpinnedNetwork(net.pinProblem);
|
|
43
|
+
if (!pin)
|
|
44
|
+
throw new UnpinnedNetwork(unpinnedText(net));
|
|
45
|
+
const d = pinDifferences(pin, { 'the network': net.chainId }, { 'the network': net.genesis });
|
|
46
|
+
if (d.length)
|
|
47
|
+
throw new PinMismatch(pinRefusal(pin, d));
|
|
48
|
+
return pin;
|
|
49
|
+
}
|
|
50
|
+
function unpinnedText(p) {
|
|
51
|
+
return `Refusing to sign: the network "${p.name}" at ${p.api} is not a named network and has no pin. Name the network it serves `
|
|
52
|
+
+ `(name: 'mainnet' or 'testnet') or give pin: { chainId, genesis }. Nothing was signed.`;
|
|
53
|
+
}
|
|
20
54
|
/** A node identity from a `/status` answer; refused unless it names both. */
|
|
21
55
|
export function readIdentity(status) {
|
|
22
56
|
if (!status || typeof status !== 'object')
|
|
@@ -70,11 +104,16 @@ async function rpcChainId(rpc, transport, timeoutMs) {
|
|
|
70
104
|
* Read and check a network. Refuses (NetworkMismatch, nothing signed) unless the spec, the node's `/status` and
|
|
71
105
|
* `eth_chainId` name the same chain id, the spec and the node the same genesis, and the spec's `testnet` flag matches
|
|
72
106
|
* the preset (a test network never stands in for the main network, nor the reverse).
|
|
107
|
+
*
|
|
108
|
+
* The reports are then held to the network's pin. When they differ (or the network has no pin) the connection still
|
|
109
|
+
* reads, `pinProblem` says why, `onPinProblem` (when given) is called with it, and every signature is refused
|
|
110
|
+
* (`PinMismatch` / `UnpinnedNetwork`). The SDK prints nothing.
|
|
73
111
|
*/
|
|
74
112
|
export async function connectNetwork(network = 'testnet', opts = {}) {
|
|
75
113
|
const p = typeof network === 'string' ? NETWORKS[network] : network;
|
|
76
114
|
if (!p)
|
|
77
115
|
throw new VordiumError('unknown_network', `Unknown network ${String(network)}; known: mainnet, testnet.`);
|
|
116
|
+
const pin = presetPin(p);
|
|
78
117
|
const t = opts.transport ?? fetchTransport;
|
|
79
118
|
const ms = opts.timeoutMs ?? 10000;
|
|
80
119
|
const sr = await t.get(p.spec, ms);
|
|
@@ -103,5 +142,16 @@ export async function connectNetwork(network = 'testnet', opts = {}) {
|
|
|
103
142
|
if (specGenesis !== null && specGenesis !== ident.genesis) {
|
|
104
143
|
throw new NetworkMismatch(`The spec names genesis ${specGenesis.slice(0, 8)}…, the node ${ident.genesis.slice(0, 8)}…; refusing to sign.`);
|
|
105
144
|
}
|
|
106
|
-
|
|
145
|
+
let pinProblem = null;
|
|
146
|
+
if (!pin) {
|
|
147
|
+
pinProblem = unpinnedText(p);
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
const d = pinDifferences(pin, { 'the network spec': specChain, "the node's /status": ident.chainId, eth_chainId: rpcChain }, { 'the network spec': specGenesis, "the node's /status": ident.genesis });
|
|
151
|
+
if (d.length)
|
|
152
|
+
pinProblem = pinRefusal(pin, d);
|
|
153
|
+
}
|
|
154
|
+
if (pinProblem && opts.onPinProblem)
|
|
155
|
+
opts.onPinProblem(pinProblem);
|
|
156
|
+
return { ...p, ...(pin ? { pin } : {}), chainId: ident.chainId, genesis: ident.genesis, specRead: spec, pinProblem };
|
|
107
157
|
}
|
package/dist/esm/nonce.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { VordiumError } from './errors.js';
|
|
1
2
|
/**
|
|
2
3
|
* Rising millisecond nonces. The chain admits an account's batch nonces, client nonces and dead-man-switch nonces
|
|
3
4
|
* through ONE window (milliseconds, within 2 days before / 1 day after block time, each used once), so a client takes
|
|
@@ -13,3 +14,111 @@ export declare class NonceClock {
|
|
|
13
14
|
}
|
|
14
15
|
/** The one nonce clock of `owner` in this process. */
|
|
15
16
|
export declare function nonceClock(owner: string): NonceClock;
|
|
17
|
+
/** Nonce spaces. `orders`: client nonces of orders, batches, cancel-all, modify, the dead-man switch and TWAP orders
|
|
18
|
+
* (one window per account). `account`: session keys, margin mode and agent operations (above the stored nonce). */
|
|
19
|
+
export type NonceSpace = 'orders' | 'account';
|
|
20
|
+
/** How far a nonce may be from block time (null = no limit on that side); `marginMs` keeps it clear of each edge, so a
|
|
21
|
+
* copy that arrives late is still inside. */
|
|
22
|
+
export interface NonceWindow {
|
|
23
|
+
behindMs: number | null;
|
|
24
|
+
aheadMs: number | null;
|
|
25
|
+
marginMs: number;
|
|
26
|
+
}
|
|
27
|
+
/** Client nonces of orders: strictly inside (block time - 2 days, block time + 1 day). */
|
|
28
|
+
export declare const ORDER_WINDOW: NonceWindow;
|
|
29
|
+
/** Session, agent and margin-mode nonces: above the stored one, at most a day ahead of block time. */
|
|
30
|
+
export declare const ACCOUNT_WINDOW: NonceWindow;
|
|
31
|
+
/** The nonce this computer would sign is outside the window the network accepts. Nothing was signed. */
|
|
32
|
+
export declare class ClockOutOfWindow extends VordiumError {
|
|
33
|
+
readonly nonce: number;
|
|
34
|
+
readonly referenceMs: number;
|
|
35
|
+
readonly reference: 'chain' | 'local';
|
|
36
|
+
constructor(message: string, nonce: number, referenceMs: number, reference: 'chain' | 'local');
|
|
37
|
+
}
|
|
38
|
+
/** Throw {@link ClockOutOfWindow} when `nonce` is not safely inside `w` around `refMs` (block time, or this clock). */
|
|
39
|
+
export declare function checkWindow(nonce: number, w: NonceWindow, refMs: number, ref: 'chain' | 'local', localMs?: number): void;
|
|
40
|
+
/**
|
|
41
|
+
* Where an allocator keeps its high-water marks. `update` reads the mark of `key` (0 when none), lets `fn` compute the
|
|
42
|
+
* next one (or throw: then nothing is written), and writes it, all under one lock that also holds across processes when
|
|
43
|
+
* the store is shared.
|
|
44
|
+
*/
|
|
45
|
+
export interface NonceStore {
|
|
46
|
+
readonly persistent: boolean;
|
|
47
|
+
/** Writes what `fn` returns when it differs from the mark (the allocator keeps it rising). `fn` may be called more
|
|
48
|
+
* than once in one update (a value already taken, a lock lost); the value kept is its last call's, and `taken` is
|
|
49
|
+
* the value its previous call returned when that one was already taken. `unique` (default true): every value
|
|
50
|
+
* written is one no other caller received; false for a record that is not a nonce. */
|
|
51
|
+
update(key: string, fn: (last: number, taken?: number) => number, opts?: {
|
|
52
|
+
unique?: boolean;
|
|
53
|
+
}): Promise<number>;
|
|
54
|
+
/** Where the mark of `key` lives, for a message (a file path), or null. */
|
|
55
|
+
where?(key: string): string | null;
|
|
56
|
+
}
|
|
57
|
+
/** A store in this process's memory (the default outside Node.js). */
|
|
58
|
+
export declare function memoryNonceStore(): NonceStore;
|
|
59
|
+
/** The nonce lock could not be taken within its time limit. Nothing was signed. */
|
|
60
|
+
export declare class NonceLockTimeout extends VordiumError {
|
|
61
|
+
constructor(message: string);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* A store in files (Node.js): one file per account and space in `dir` (default `VORDIUM_STATE_DIR`, else
|
|
65
|
+
* `~/.vordium/nonces`), folder 0700, files 0600, behind a lock file so every process on this machine takes turns.
|
|
66
|
+
*
|
|
67
|
+
* The lock file holds its owner's process id and a token, and its holder refreshes its time while it holds it (a hold
|
|
68
|
+
* is short). A lock is stale when its time is older than `staleMs` (its holder stopped refreshing it), or when it names
|
|
69
|
+
* another process that is not running. A lock under this process's own id is never judged by its token: another thread
|
|
70
|
+
* or another copy of this SDK in the same process may hold it. A stale lock is taken over by an atomic rename to a name of the taker's own, after
|
|
71
|
+
* reading its token again; the moved file's token is checked, and a live lock moved by a race is put back. A holder
|
|
72
|
+
* writes the mark only while the lock holds its token and removes only its own lock. Taking the lock gives up after
|
|
73
|
+
* `acquireTimeoutMs` with {@link NonceLockTimeout}; it never waits forever.
|
|
74
|
+
*
|
|
75
|
+
* The lock keeps turns short and orderly; it is not what makes a value unique. A holder whose event loop stalled (a
|
|
76
|
+
* laptop asleep, a stopped process, a long pause) can have its lock taken over while it still works, so every value
|
|
77
|
+
* written is first claimed by an exclusive create of a claim file named after it in the folder `<mark>.claims`. Only
|
|
78
|
+
* the one process whose create succeeds may hand the value out; a value already claimed moves the next one above it.
|
|
79
|
+
* A value is computed above the highest claim present, the highest claim is never removed, and a claim that is not
|
|
80
|
+
* among the highest is removed only once it is older than four times `staleMs`. A holder gives a value up (and takes
|
|
81
|
+
* its turn again) when more than half of `staleMs` passed between reading the claims and claiming it, or since its lock
|
|
82
|
+
* was last seen fresh, so a claim it read past cannot have been removed under it.
|
|
83
|
+
*/
|
|
84
|
+
export declare function fileNonceStore(dir?: string, opts?: {
|
|
85
|
+
staleMs?: number;
|
|
86
|
+
acquireTimeoutMs?: number;
|
|
87
|
+
}): NonceStore;
|
|
88
|
+
/** Files under Node.js, memory elsewhere. */
|
|
89
|
+
export declare function defaultNonceStore(): NonceStore;
|
|
90
|
+
/**
|
|
91
|
+
* Strictly rising millisecond nonces for one account in one nonce space: `max(last + 1, now)`, never the same twice
|
|
92
|
+
* across this process and (with a file store) every process on this machine, never back when the clock goes back, and
|
|
93
|
+
* checked against the network's window before use. One mark serves every network: a nonce that rises on all of them is
|
|
94
|
+
* valid on each.
|
|
95
|
+
*/
|
|
96
|
+
export declare class NonceAllocator {
|
|
97
|
+
readonly space: NonceSpace;
|
|
98
|
+
private readonly store;
|
|
99
|
+
private readonly now;
|
|
100
|
+
readonly account: string;
|
|
101
|
+
readonly window: NonceWindow;
|
|
102
|
+
constructor(account: string, space?: NonceSpace, store?: NonceStore, now?: () => number, window?: NonceWindow);
|
|
103
|
+
get persistent(): boolean;
|
|
104
|
+
private get key();
|
|
105
|
+
/** The next nonce, at least `atLeast`; checked against `chainMs` (block time) when given, else this computer's clock. */
|
|
106
|
+
next(atLeast?: number, chainMs?: number | null): Promise<number>;
|
|
107
|
+
/**
|
|
108
|
+
* When the mark was set back to the clock and `nonce` may also have been handed out before that, the time of the
|
|
109
|
+
* set-back (ms): an order found with this nonce counts as this write's only when it is newer. Else null.
|
|
110
|
+
*/
|
|
111
|
+
rewoundBefore(nonce: number): Promise<number | null>;
|
|
112
|
+
/** The last notice of a mark left outside the window that was set back to the clock (orders space), or null. */
|
|
113
|
+
notice: string | null;
|
|
114
|
+
/** Called with that notice when it happens. */
|
|
115
|
+
onRewind?: (message: string) => void;
|
|
116
|
+
/** Note a nonce the chain already holds, so the next one is above it. */
|
|
117
|
+
seen(nonce: number): Promise<void>;
|
|
118
|
+
/** The highest nonce handed out or seen (0 when none). */
|
|
119
|
+
last(): Promise<number>;
|
|
120
|
+
}
|
|
121
|
+
/** The one allocator of `account` in `space` in this process (default store). */
|
|
122
|
+
export declare function nonceAllocator(account: string, space?: NonceSpace): NonceAllocator;
|
|
123
|
+
/** A nonce source an account handle takes: the in-memory clock or a persisted allocator. */
|
|
124
|
+
export type NonceSource = NonceClock | NonceAllocator;
|