@totemsdk/chain-provider 0.2.1 → 0.3.1
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/LICENSE +202 -21
- package/NOTICE +60 -0
- package/dist/cjs/index.d.ts +10 -0
- package/dist/cjs/index.js +22 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/providers/composite.d.ts +20 -0
- package/dist/cjs/providers/composite.js +54 -0
- package/dist/cjs/providers/hosted.d.ts +58 -0
- package/dist/cjs/providers/hosted.js +243 -0
- package/dist/cjs/providers/lookup-client.d.ts +62 -0
- package/dist/cjs/providers/lookup-client.js +67 -0
- package/dist/cjs/providers/minima-rpc.d.ts +51 -0
- package/dist/cjs/providers/minima-rpc.js +208 -0
- package/dist/cjs/resolve.d.ts +63 -0
- package/dist/cjs/resolve.js +100 -0
- package/dist/cjs/types.d.ts +170 -0
- package/dist/cjs/types.js +5 -0
- package/dist/cjs/verify-deposit.d.ts +53 -0
- package/dist/cjs/verify-deposit.js +212 -0
- package/dist/index.js +6 -22
- package/dist/providers/composite.js +1 -5
- package/dist/providers/hosted.js +1 -5
- package/dist/providers/lookup-client.js +1 -5
- package/dist/providers/minima-rpc.d.ts +22 -1
- package/dist/providers/minima-rpc.js +92 -11
- package/dist/resolve.js +12 -18
- package/dist/types.d.ts +33 -0
- package/dist/types.js +1 -2
- package/dist/verify-deposit.js +15 -25
- package/package.json +18 -11
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* LookupClientProvider — ChainStateProvider backed by a @totemsdk/lookup-client instance.
|
|
4
3
|
*
|
|
@@ -33,9 +32,7 @@
|
|
|
33
32
|
* );
|
|
34
33
|
* ```
|
|
35
34
|
*/
|
|
36
|
-
|
|
37
|
-
exports.LookupClientProvider = void 0;
|
|
38
|
-
class LookupClientProvider {
|
|
35
|
+
export class LookupClientProvider {
|
|
39
36
|
constructor(_client) {
|
|
40
37
|
this._client = _client;
|
|
41
38
|
}
|
|
@@ -64,4 +61,3 @@ class LookupClientProvider {
|
|
|
64
61
|
return this._client.broadcastTxPoW(txpowHex);
|
|
65
62
|
}
|
|
66
63
|
}
|
|
67
|
-
exports.LookupClientProvider = LookupClientProvider;
|
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
* MinimaRpcProvider — thin wrapper over @totemsdk/minima-rpc.
|
|
3
3
|
*/
|
|
4
4
|
import type { MinimaRpcClient } from '@totemsdk/minima-rpc';
|
|
5
|
-
import type { ChainStateProvider, CoinsQuery, Coin, MMRProof, ChainTip, TokenInfo, TokenSearchQuery, BroadcastResult, DepositVerifier, VerifyDepositParams, DepositVerification } from '../types.js';
|
|
5
|
+
import type { ChainStateProvider, CoinsQuery, Coin, MMRProof, ChainTip, TokenInfo, TokenSearchQuery, AddressInfo, ExportedCoin, BroadcastResult, DepositVerifier, VerifyDepositParams, DepositVerification } from '../types.js';
|
|
6
6
|
export declare class MinimaRpcProvider implements ChainStateProvider, DepositVerifier {
|
|
7
7
|
private readonly client;
|
|
8
8
|
constructor(client: MinimaRpcClient);
|
|
9
9
|
getCoins(query: CoinsQuery): Promise<Coin[]>;
|
|
10
|
+
getAddressInfo(address: string): Promise<AddressInfo | null>;
|
|
11
|
+
exportCoin(coinId: string): Promise<ExportedCoin | null>;
|
|
10
12
|
getCoin(coinId: string): Promise<Coin | null>;
|
|
11
13
|
/**
|
|
12
14
|
* Authoritative live check via `coinexport` (the coinproof endpoint): returns
|
|
@@ -22,9 +24,28 @@ export declare class MinimaRpcProvider implements ChainStateProvider, DepositVer
|
|
|
22
24
|
/** MMR root at tip — the anchor peers verify offline proofs against. */
|
|
23
25
|
getMmrRoot(): Promise<string | null>;
|
|
24
26
|
getProof(coinId: string): Promise<MMRProof>;
|
|
27
|
+
/**
|
|
28
|
+
* Current minimum TxPoW work from the tip block's Magic numbers (`magic`
|
|
29
|
+
* command). This is the live target locally mined transactions must meet;
|
|
30
|
+
* using it avoids hardcoding a difficulty that may be harder than the
|
|
31
|
+
* network currently requires (or easier than it has drifted to).
|
|
32
|
+
*/
|
|
33
|
+
getMinTxPowWork(): Promise<string | null>;
|
|
25
34
|
getTip(): Promise<ChainTip>;
|
|
26
35
|
getToken(tokenId: string): Promise<TokenInfo>;
|
|
27
36
|
searchTokens(query: TokenSearchQuery): Promise<TokenInfo[]>;
|
|
28
37
|
getTokensByCreator(address: string): Promise<TokenInfo[]>;
|
|
38
|
+
/**
|
|
39
|
+
* Submit a raw transaction + witness via the node's explicit-mine path.
|
|
40
|
+
*
|
|
41
|
+
* `sendtransaction` only adds a pre-built TxPoW to the mempool; a TxPoW built
|
|
42
|
+
* outside the node has a placeholder header (block number 0, block difficulty
|
|
43
|
+
* MAX_HASH), which the node's block builder does not reliably promote. The
|
|
44
|
+
* node's own `txnmine` builds the header with the real tip fields and mines
|
|
45
|
+
* it. We then post that mined TxPoW with `sendtransaction`.
|
|
46
|
+
*
|
|
47
|
+
* TxnRow wire format (Java/C++): MiniString(id) || Transaction || Witness.
|
|
48
|
+
*/
|
|
49
|
+
submitTransaction(txBytes: Uint8Array, witnessBytes: Uint8Array, id?: string): Promise<BroadcastResult>;
|
|
29
50
|
broadcastTxPoW(txpowHex: string): Promise<BroadcastResult>;
|
|
30
51
|
}
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* MinimaRpcProvider — thin wrapper over @totemsdk/minima-rpc.
|
|
4
3
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const verify_deposit_js_1 = require("../verify-deposit.js");
|
|
8
|
-
class MinimaRpcProvider {
|
|
4
|
+
import { depositAddressFor, evaluateDeposit, notFoundResult } from '../verify-deposit.js';
|
|
5
|
+
export class MinimaRpcProvider {
|
|
9
6
|
constructor(client) {
|
|
10
7
|
this.client = client;
|
|
11
8
|
}
|
|
@@ -20,7 +17,32 @@ class MinimaRpcProvider {
|
|
|
20
17
|
});
|
|
21
18
|
return result;
|
|
22
19
|
}
|
|
20
|
+
async getAddressInfo(address) {
|
|
21
|
+
const res = await this.client.addressInfo(address);
|
|
22
|
+
if (!res?.publickey || !res?.script)
|
|
23
|
+
return null;
|
|
24
|
+
return { address, publickey: res.publickey, script: res.script };
|
|
25
|
+
}
|
|
26
|
+
async exportCoin(coinId) {
|
|
27
|
+
const res = await this.client.coinExport(coinId);
|
|
28
|
+
if (!res?.data)
|
|
29
|
+
return null;
|
|
30
|
+
return { coinid: coinId, data: res.data };
|
|
31
|
+
}
|
|
23
32
|
async getCoin(coinId) {
|
|
33
|
+
// `coins` only returns wallet-relevant coins, so it never sees coins locked
|
|
34
|
+
// to an app/channel script. `coinexport` is the chain-level primitive and is
|
|
35
|
+
// the only reliable way to resolve an arbitrary coinid (see verifyDeposit).
|
|
36
|
+
try {
|
|
37
|
+
const exported = await this.client.coinExport(coinId);
|
|
38
|
+
const coin = exported?.coinproof?.coin;
|
|
39
|
+
if (coin) {
|
|
40
|
+
return { ...coin, spent: coin.spent === true };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
// fall through to the wallet query
|
|
45
|
+
}
|
|
24
46
|
try {
|
|
25
47
|
const coins = await this.getCoins({ coinId });
|
|
26
48
|
return coins[0] ?? null;
|
|
@@ -41,9 +63,9 @@ class MinimaRpcProvider {
|
|
|
41
63
|
const coin = exported?.coinproof?.coin;
|
|
42
64
|
const spent = coin?.spent === true;
|
|
43
65
|
if (!coin) {
|
|
44
|
-
return
|
|
66
|
+
return notFoundResult(`coin ${params.coinId} not found on chain`);
|
|
45
67
|
}
|
|
46
|
-
return
|
|
68
|
+
return evaluateDeposit(coin, spent, params);
|
|
47
69
|
}
|
|
48
70
|
catch (error) {
|
|
49
71
|
return {
|
|
@@ -60,7 +82,7 @@ class MinimaRpcProvider {
|
|
|
60
82
|
}
|
|
61
83
|
}
|
|
62
84
|
depositAddressFor(lp, opts) {
|
|
63
|
-
return
|
|
85
|
+
return depositAddressFor(lp, opts);
|
|
64
86
|
}
|
|
65
87
|
/** MMR root at tip — the anchor peers verify offline proofs against. */
|
|
66
88
|
async getMmrRoot() {
|
|
@@ -70,6 +92,21 @@ class MinimaRpcProvider {
|
|
|
70
92
|
async getProof(coinId) {
|
|
71
93
|
return this.client.mmrProof(coinId);
|
|
72
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Current minimum TxPoW work from the tip block's Magic numbers (`magic`
|
|
97
|
+
* command). This is the live target locally mined transactions must meet;
|
|
98
|
+
* using it avoids hardcoding a difficulty that may be harder than the
|
|
99
|
+
* network currently requires (or easier than it has drifted to).
|
|
100
|
+
*/
|
|
101
|
+
async getMinTxPowWork() {
|
|
102
|
+
try {
|
|
103
|
+
const info = await this.client.magic();
|
|
104
|
+
return info?.lastblock?.currentmintxpowwork ?? null;
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
73
110
|
async getTip() {
|
|
74
111
|
return this.client.getTip();
|
|
75
112
|
}
|
|
@@ -106,13 +143,58 @@ class MinimaRpcProvider {
|
|
|
106
143
|
async getTokensByCreator(address) {
|
|
107
144
|
return this.searchTokens({ creatorAddress: address });
|
|
108
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* Submit a raw transaction + witness via the node's explicit-mine path.
|
|
148
|
+
*
|
|
149
|
+
* `sendtransaction` only adds a pre-built TxPoW to the mempool; a TxPoW built
|
|
150
|
+
* outside the node has a placeholder header (block number 0, block difficulty
|
|
151
|
+
* MAX_HASH), which the node's block builder does not reliably promote. The
|
|
152
|
+
* node's own `txnmine` builds the header with the real tip fields and mines
|
|
153
|
+
* it. We then post that mined TxPoW with `sendtransaction`.
|
|
154
|
+
*
|
|
155
|
+
* TxnRow wire format (Java/C++): MiniString(id) || Transaction || Witness.
|
|
156
|
+
*/
|
|
157
|
+
async submitTransaction(txBytes, witnessBytes, id = `omnia-${Date.now()}`) {
|
|
158
|
+
try {
|
|
159
|
+
const idBytes = new TextEncoder().encode(id);
|
|
160
|
+
const len = idBytes.length;
|
|
161
|
+
const prefix = new Uint8Array([
|
|
162
|
+
(len >>> 24) & 0xff,
|
|
163
|
+
(len >>> 16) & 0xff,
|
|
164
|
+
(len >>> 8) & 0xff,
|
|
165
|
+
len & 0xff,
|
|
166
|
+
]);
|
|
167
|
+
const row = Buffer.concat([
|
|
168
|
+
Buffer.from(prefix),
|
|
169
|
+
Buffer.from(idBytes),
|
|
170
|
+
Buffer.from(txBytes),
|
|
171
|
+
Buffer.from(witnessBytes),
|
|
172
|
+
]);
|
|
173
|
+
const mined = await this.client.txnMine({ data: Buffer.from(row).toString('hex') });
|
|
174
|
+
if (!mined?.data) {
|
|
175
|
+
return { success: false, message: 'txnmine returned no data' };
|
|
176
|
+
}
|
|
177
|
+
const posted = await this.client.sendTransaction(mined.data);
|
|
178
|
+
return {
|
|
179
|
+
success: true,
|
|
180
|
+
txpowid: mined.txpowid ?? posted?.txpowid,
|
|
181
|
+
message: 'submitted via txnmine + sendtransaction',
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
catch (e) {
|
|
185
|
+
return { success: false, message: String(e) };
|
|
186
|
+
}
|
|
187
|
+
}
|
|
109
188
|
async broadcastTxPoW(txpowHex) {
|
|
110
189
|
try {
|
|
111
|
-
|
|
190
|
+
// `sendtransaction` submits a pre-mined TxPoW to the mempool where the
|
|
191
|
+
// node mines it into a block. `txnminepost` only posts the TxPoW (via the
|
|
192
|
+
// peer NIO path) and does not get it mined.
|
|
193
|
+
const result = await this.client.sendTransaction(txpowHex);
|
|
112
194
|
return {
|
|
113
195
|
success: true,
|
|
114
196
|
txpowid: result?.txpowid,
|
|
115
|
-
message: 'broadcast via MinimaRpc
|
|
197
|
+
message: 'broadcast via MinimaRpc sendtransaction',
|
|
116
198
|
};
|
|
117
199
|
}
|
|
118
200
|
catch (e) {
|
|
@@ -120,4 +202,3 @@ class MinimaRpcProvider {
|
|
|
120
202
|
}
|
|
121
203
|
}
|
|
122
204
|
}
|
|
123
|
-
exports.MinimaRpcProvider = MinimaRpcProvider;
|
package/dist/resolve.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @module @totemsdk/chain-provider/resolve
|
|
4
3
|
*
|
|
@@ -12,18 +11,13 @@
|
|
|
12
11
|
* enforced structurally (HTTPS remote, HTTP loopback only) so a hostile node URL
|
|
13
12
|
* cannot be configured silently.
|
|
14
13
|
*/
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
exports.isSelfHosted = isSelfHosted;
|
|
20
|
-
const minima_rpc_1 = require("@totemsdk/minima-rpc");
|
|
21
|
-
const hosted_js_1 = require("./providers/hosted.js");
|
|
22
|
-
const minima_rpc_js_1 = require("./providers/minima-rpc.js");
|
|
23
|
-
const composite_js_1 = require("./providers/composite.js");
|
|
14
|
+
import { createMinimaRpcClient } from '@totemsdk/minima-rpc';
|
|
15
|
+
import { HostedProvider } from './providers/hosted.js';
|
|
16
|
+
import { MinimaRpcProvider } from './providers/minima-rpc.js';
|
|
17
|
+
import { CompositeProvider } from './providers/composite.js';
|
|
24
18
|
const DEFAULT_RPC_PORT = 9005;
|
|
25
19
|
/** Parse `http(s)://host:port` into the minima-rpc client config shape. */
|
|
26
|
-
function parseMinimaRpcUrl(url) {
|
|
20
|
+
export function parseMinimaRpcUrl(url) {
|
|
27
21
|
let parsed;
|
|
28
22
|
try {
|
|
29
23
|
parsed = new URL(url);
|
|
@@ -48,7 +42,7 @@ function isLoopbackHost(hostname) {
|
|
|
48
42
|
* Consent guard (RFC-013 §11): HTTPS is required for remote hosts; plain `http`
|
|
49
43
|
* is permitted only for loopback. Never ships a build-time localhost bypass.
|
|
50
44
|
*/
|
|
51
|
-
function assertConsentedNodeUrl(url) {
|
|
45
|
+
export function assertConsentedNodeUrl(url) {
|
|
52
46
|
const parsed = new URL(url);
|
|
53
47
|
if (parsed.protocol === 'https:')
|
|
54
48
|
return;
|
|
@@ -61,14 +55,14 @@ function createNodeProvider(config) {
|
|
|
61
55
|
throw new Error('minima-rpc mode requires minimaRpcUrl');
|
|
62
56
|
}
|
|
63
57
|
const { host, port, ssl } = parseMinimaRpcUrl(config.minimaRpcUrl);
|
|
64
|
-
const client =
|
|
58
|
+
const client = createMinimaRpcClient({
|
|
65
59
|
host,
|
|
66
60
|
port,
|
|
67
61
|
ssl,
|
|
68
62
|
...(config.minimaRpcUser ? { username: config.minimaRpcUser } : {}),
|
|
69
63
|
...(config.minimaRpcPass ? { password: config.minimaRpcPass } : {}),
|
|
70
64
|
});
|
|
71
|
-
return new
|
|
65
|
+
return new MinimaRpcProvider(client);
|
|
72
66
|
}
|
|
73
67
|
/**
|
|
74
68
|
* Resolve a {@link ChainStateProvider} from the persisted config.
|
|
@@ -77,8 +71,8 @@ function createNodeProvider(config) {
|
|
|
77
71
|
* - `minima-rpc` → the user's own node.
|
|
78
72
|
* - `composite` → node first, hosted fallback (unless `fallbackToAxia === false`).
|
|
79
73
|
*/
|
|
80
|
-
function resolveChainProvider(config, options) {
|
|
81
|
-
const hosted = options.hostedProvider ?? new
|
|
74
|
+
export function resolveChainProvider(config, options) {
|
|
75
|
+
const hosted = options.hostedProvider ?? new HostedProvider(options.hosted);
|
|
82
76
|
if (config.mode === 'axia') {
|
|
83
77
|
return hosted;
|
|
84
78
|
}
|
|
@@ -92,9 +86,9 @@ function resolveChainProvider(config, options) {
|
|
|
92
86
|
if (config.mode === 'minima-rpc' || config.fallbackToAxia === false) {
|
|
93
87
|
return node;
|
|
94
88
|
}
|
|
95
|
-
return new
|
|
89
|
+
return new CompositeProvider(node, hosted, options.onFallback);
|
|
96
90
|
}
|
|
97
91
|
/** True when the config routes reads/broadcast away from the hosted relay. */
|
|
98
|
-
function isSelfHosted(config) {
|
|
92
|
+
export function isSelfHosted(config) {
|
|
99
93
|
return config.mode === 'minima-rpc' || config.mode === 'composite';
|
|
100
94
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -125,8 +125,28 @@ export interface DepositVerifier {
|
|
|
125
125
|
expectedRoot: Uint8Array;
|
|
126
126
|
}): Promise<boolean>;
|
|
127
127
|
}
|
|
128
|
+
export interface AddressInfo {
|
|
129
|
+
address: string;
|
|
130
|
+
publickey: string;
|
|
131
|
+
script: string;
|
|
132
|
+
}
|
|
133
|
+
export interface ExportedCoin {
|
|
134
|
+
coinid: string;
|
|
135
|
+
data: string;
|
|
136
|
+
}
|
|
128
137
|
export interface ChainStateProvider {
|
|
129
138
|
getCoins(query: CoinsQuery): Promise<Coin[]>;
|
|
139
|
+
/**
|
|
140
|
+
* Ownership material for an address (public key + address script). Required to
|
|
141
|
+
* build a funding witness for a coin the caller owns.
|
|
142
|
+
*/
|
|
143
|
+
getAddressInfo?(address: string): Promise<AddressInfo | null>;
|
|
144
|
+
/**
|
|
145
|
+
* Serialized coin proof (Minima `coinexport`) for a specific coin id. Required
|
|
146
|
+
* to assemble the funding input proof. Never expose this over a remote control
|
|
147
|
+
* plane; it is caller-side material.
|
|
148
|
+
*/
|
|
149
|
+
exportCoin?(coinId: string): Promise<ExportedCoin | null>;
|
|
130
150
|
getCoin(coinId: string): Promise<Coin | null>;
|
|
131
151
|
getProof(coinId: string): Promise<MMRProof>;
|
|
132
152
|
getTip(): Promise<ChainTip>;
|
|
@@ -134,4 +154,17 @@ export interface ChainStateProvider {
|
|
|
134
154
|
searchTokens(query: TokenSearchQuery): Promise<TokenInfo[]>;
|
|
135
155
|
getTokensByCreator(address: string): Promise<TokenInfo[]>;
|
|
136
156
|
broadcastTxPoW(txpowHex: string): Promise<BroadcastResult>;
|
|
157
|
+
/**
|
|
158
|
+
* Live minimum TxPoW work (32-byte hex target) from the node's Magic numbers.
|
|
159
|
+
* Optional: providers that cannot read it let callers fall back to the
|
|
160
|
+
* protocol default. Used to mine transactions to the current network target.
|
|
161
|
+
*/
|
|
162
|
+
getMinTxPowWork?(): Promise<string | null>;
|
|
163
|
+
/**
|
|
164
|
+
* Submit a raw transaction + witness through the node's explicit-mine path:
|
|
165
|
+
* `txnmine` (node builds the TxPoW header with the real block number /
|
|
166
|
+
* difficulty / magic and mines) followed by `sendtransaction`. When absent,
|
|
167
|
+
* callers fall back to SDK-side mining + `broadcastTxPoW`.
|
|
168
|
+
*/
|
|
169
|
+
submitTransaction?(txBytes: Uint8Array, witnessBytes: Uint8Array, id?: string): Promise<BroadcastResult>;
|
|
137
170
|
}
|
package/dist/types.js
CHANGED
package/dist/verify-deposit.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* chain-provider/verify-deposit.ts — Funding-truth verification.
|
|
4
3
|
*
|
|
@@ -8,25 +7,16 @@
|
|
|
8
7
|
* chunk MMR proof against a known root so a peer with a lookup root can
|
|
9
8
|
* re-validate a deposit without reaching the node.
|
|
10
9
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
exports.evaluateDeposit = evaluateDeposit;
|
|
14
|
-
exports.notFoundResult = notFoundResult;
|
|
15
|
-
exports.verifyDeposit = verifyDeposit;
|
|
16
|
-
exports.verifyDepositMmrProof = verifyDepositMmrProof;
|
|
17
|
-
exports.depositAddressFor = depositAddressFor;
|
|
18
|
-
exports.withDepositVerifier = withDepositVerifier;
|
|
19
|
-
exports.compareAmounts = compareAmounts;
|
|
20
|
-
const core_1 = require("@totemsdk/core");
|
|
21
|
-
exports.DEPOSIT_ADDRESS_DOMAIN = 'totemsdk/chain-provider/deposit/v1';
|
|
10
|
+
import { bytesToHex, decodeMx, hexToBytes, sha3_256, toHex, verifyMMRProof } from '@totemsdk/core';
|
|
11
|
+
export const DEPOSIT_ADDRESS_DOMAIN = 'totemsdk/chain-provider/deposit/v1';
|
|
22
12
|
/** Reduce an address to its root bytes: Mx addresses decode, 0x-hex roots pass through. */
|
|
23
13
|
function rootBytesOf(value) {
|
|
24
14
|
const v = value.trim();
|
|
25
15
|
if (/^[0-9a-fA-F]{64}$/.test(v.replace(/^0x/, ''))) {
|
|
26
|
-
return
|
|
16
|
+
return hexToBytes(v);
|
|
27
17
|
}
|
|
28
18
|
try {
|
|
29
|
-
return
|
|
19
|
+
return decodeMx(v);
|
|
30
20
|
}
|
|
31
21
|
catch {
|
|
32
22
|
return null;
|
|
@@ -62,7 +52,7 @@ function isConfirmed(coin) {
|
|
|
62
52
|
* The shared all-gates deposit evaluation. Used by the generic `verifyDeposit`
|
|
63
53
|
* and by `MinimaRpcProvider` (which reads the coin proof via `coinexport`).
|
|
64
54
|
*/
|
|
65
|
-
function evaluateDeposit(coin, spent, params) {
|
|
55
|
+
export function evaluateDeposit(coin, spent, params) {
|
|
66
56
|
const unspent = spent !== true && coin.spent !== true;
|
|
67
57
|
const ownedByOwner = addressesEqual(coin.address, params.ownerAddress) || addressesEqual(coin.miniaddress ?? coin.address, params.ownerAddress);
|
|
68
58
|
const tokenMatches = params.tokenId ? coin.tokenid === params.tokenId : coin.tokenid === '0x00' || coin.tokenid === '0x01';
|
|
@@ -83,7 +73,7 @@ function evaluateDeposit(coin, spent, params) {
|
|
|
83
73
|
coin,
|
|
84
74
|
};
|
|
85
75
|
}
|
|
86
|
-
function notFoundResult(reason) {
|
|
76
|
+
export function notFoundResult(reason) {
|
|
87
77
|
return {
|
|
88
78
|
valid: false,
|
|
89
79
|
exists: false,
|
|
@@ -104,7 +94,7 @@ function notFoundResult(reason) {
|
|
|
104
94
|
* 5. its amount covers `claimedAmount`;
|
|
105
95
|
* 6. when `requireConfirmed` is set, the coin is chain-confirmed.
|
|
106
96
|
*/
|
|
107
|
-
async function verifyDeposit(provider, params) {
|
|
97
|
+
export async function verifyDeposit(provider, params) {
|
|
108
98
|
let coin;
|
|
109
99
|
try {
|
|
110
100
|
coin = await provider.getCoin(params.coinId);
|
|
@@ -133,17 +123,17 @@ async function verifyDeposit(provider, params) {
|
|
|
133
123
|
* deposit commitment without node access. The proof is encoded to the wasm
|
|
134
124
|
* contract (hex `data`, decimal-string `value` per chunk).
|
|
135
125
|
*/
|
|
136
|
-
function verifyDepositMmrProof(leafPubkey, proof, expectedRoot) {
|
|
126
|
+
export function verifyDepositMmrProof(leafPubkey, proof, expectedRoot) {
|
|
137
127
|
const proofJson = JSON.stringify({
|
|
138
128
|
chunks: proof.chunks.map((c) => ({
|
|
139
129
|
isLeft: c.isLeft,
|
|
140
130
|
mmrData: {
|
|
141
|
-
data:
|
|
131
|
+
data: bytesToHex(c.mmrData.data),
|
|
142
132
|
value: c.mmrData.value.toString(),
|
|
143
133
|
},
|
|
144
134
|
})),
|
|
145
135
|
});
|
|
146
|
-
return
|
|
136
|
+
return verifyMMRProof(leafPubkey, proofJson, expectedRoot);
|
|
147
137
|
}
|
|
148
138
|
/**
|
|
149
139
|
* Deterministic, domain-separated funding address for an LP deposit. In
|
|
@@ -151,21 +141,21 @@ function verifyDepositMmrProof(leafPubkey, proof, expectedRoot) {
|
|
|
151
141
|
* funding check is run against; keeping it a pure derivation keeps issue #25
|
|
152
142
|
* constructable and checkable without a live script compiler.
|
|
153
143
|
*/
|
|
154
|
-
function depositAddressFor(lp, opts) {
|
|
144
|
+
export function depositAddressFor(lp, opts) {
|
|
155
145
|
const scope = [
|
|
156
|
-
|
|
146
|
+
DEPOSIT_ADDRESS_DOMAIN,
|
|
157
147
|
lp,
|
|
158
148
|
opts?.poolId ?? '',
|
|
159
149
|
opts?.tokenId ?? '0x00',
|
|
160
150
|
].join('|');
|
|
161
|
-
return `Mx${
|
|
151
|
+
return `Mx${toHex(sha3_256(new TextEncoder().encode(scope))).slice(0, 80)}`;
|
|
162
152
|
}
|
|
163
153
|
/**
|
|
164
154
|
* Default `DepositVerifier` over any `ChainStateProvider`. The live path uses
|
|
165
155
|
* `getCoin`; the MMR-root path returns `null` unless the base provider knows a
|
|
166
156
|
* root (concrete providers override `getMmrRoot`).
|
|
167
157
|
*/
|
|
168
|
-
function withDepositVerifier(provider) {
|
|
158
|
+
export function withDepositVerifier(provider) {
|
|
169
159
|
const verifier = {
|
|
170
160
|
verifyDeposit: (params) => verifyDeposit(provider, params),
|
|
171
161
|
depositAddressFor,
|
|
@@ -194,7 +184,7 @@ function splitAmount(decimalOrMinima) {
|
|
|
194
184
|
* inputs. Neither side is fixed-truncated, so a claim can never be rounded
|
|
195
185
|
* down into passing (AUD-038).
|
|
196
186
|
*/
|
|
197
|
-
function compareAmounts(a, b) {
|
|
187
|
+
export function compareAmounts(a, b) {
|
|
198
188
|
const left = splitAmount(a);
|
|
199
189
|
const right = splitAmount(b);
|
|
200
190
|
if (left.whole < right.whole)
|
package/package.json
CHANGED
|
@@ -1,24 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@totemsdk/chain-provider",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Unified ChainStateProvider interface for Totem SDK — Hosted, Minima RPC, Composite",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"types": "dist/index.d.ts",
|
|
5
|
+
"main": "dist/cjs/index.js",
|
|
6
|
+
"types": "dist/cjs/index.d.ts",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": {
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
"import": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"require": {
|
|
14
|
+
"types": "./dist/cjs/index.d.ts",
|
|
15
|
+
"default": "./dist/cjs/index.js"
|
|
16
|
+
}
|
|
12
17
|
}
|
|
13
18
|
},
|
|
14
19
|
"files": [
|
|
15
20
|
"dist",
|
|
16
21
|
"README.md",
|
|
17
|
-
"LICENSE"
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"NOTICE"
|
|
18
24
|
],
|
|
19
25
|
"dependencies": {
|
|
20
|
-
"@totemsdk/
|
|
21
|
-
"@totemsdk/
|
|
26
|
+
"@totemsdk/core": "^1.4.1",
|
|
27
|
+
"@totemsdk/minima-rpc": "^0.5.1"
|
|
22
28
|
},
|
|
23
29
|
"devDependencies": {
|
|
24
30
|
"@types/jest": "^30.0.0",
|
|
@@ -33,7 +39,7 @@
|
|
|
33
39
|
"publishConfig": {
|
|
34
40
|
"access": "public"
|
|
35
41
|
},
|
|
36
|
-
"license": "
|
|
42
|
+
"license": "Apache-2.0",
|
|
37
43
|
"author": "Totem SDK",
|
|
38
44
|
"homepage": "https://docs.totem.ing",
|
|
39
45
|
"bugs": {
|
|
@@ -58,8 +64,9 @@
|
|
|
58
64
|
"chain-data",
|
|
59
65
|
"strategy-pattern"
|
|
60
66
|
],
|
|
67
|
+
"type": "module",
|
|
61
68
|
"scripts": {
|
|
62
|
-
"build": "tsc",
|
|
69
|
+
"build": "tsc && tsc -p tsconfig.cjs.json && node -e \"require('fs').writeFileSync('dist/cjs/package.json', JSON.stringify({type:'commonjs'})+'\\n')\"",
|
|
63
70
|
"clean": "rm -rf dist",
|
|
64
71
|
"test": "jest"
|
|
65
72
|
}
|