@totemsdk/chain-provider 0.2.1 → 0.3.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/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 +49 -0
- package/dist/cjs/providers/minima-rpc.js +196 -0
- package/dist/cjs/resolve.d.ts +63 -0
- package/dist/cjs/resolve.js +100 -0
- package/dist/cjs/types.d.ts +150 -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 +19 -0
- package/dist/providers/minima-rpc.js +80 -11
- package/dist/resolve.js +12 -18
- package/dist/types.d.ts +13 -0
- package/dist/types.js +1 -2
- package/dist/verify-deposit.js +15 -25
- package/package.json +15 -9
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* chain-provider/verify-deposit.ts — Funding-truth verification.
|
|
4
|
+
*
|
|
5
|
+
* "Verified every time means on-chain check," so this module is the single
|
|
6
|
+
* place a deposit's funding is checked: an unspent coin, owned by the LP,
|
|
7
|
+
* for the claimed token and amount. The offline variant anchors a legacy
|
|
8
|
+
* chunk MMR proof against a known root so a peer with a lookup root can
|
|
9
|
+
* re-validate a deposit without reaching the node.
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.DEPOSIT_ADDRESS_DOMAIN = void 0;
|
|
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';
|
|
22
|
+
/** Reduce an address to its root bytes: Mx addresses decode, 0x-hex roots pass through. */
|
|
23
|
+
function rootBytesOf(value) {
|
|
24
|
+
const v = value.trim();
|
|
25
|
+
if (/^[0-9a-fA-F]{64}$/.test(v.replace(/^0x/, ''))) {
|
|
26
|
+
return (0, core_1.hexToBytes)(v);
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
return (0, core_1.decodeMx)(v);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function bytesEqual(a, b) {
|
|
36
|
+
return a.length === b.length && a.every((x, i) => x === b[i]);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Normalize ownership comparison (#2): totem-node reports `address`/`miniaddress`
|
|
40
|
+
* as 0x-hex script roots while LPs hold Mx addresses. Either form compares equal.
|
|
41
|
+
*/
|
|
42
|
+
function addressesEqual(a, b) {
|
|
43
|
+
if (a === b)
|
|
44
|
+
return true;
|
|
45
|
+
const ra = rootBytesOf(a);
|
|
46
|
+
const rb = rootBytesOf(b);
|
|
47
|
+
return !!ra && !!rb && bytesEqual(ra, rb);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Is the coin chain-confirmed? (#7) An unspent coin in the node's mempool has
|
|
51
|
+
* mmrentry '0' and can still be double-spent. treat anything without a positive
|
|
52
|
+
* mmrentry as unconfirmed.
|
|
53
|
+
*/
|
|
54
|
+
function isConfirmed(coin) {
|
|
55
|
+
const entry = coin.mmrentry;
|
|
56
|
+
if (entry === undefined || entry === null)
|
|
57
|
+
return false;
|
|
58
|
+
const num = parseInt(String(entry), 10);
|
|
59
|
+
return Number.isFinite(num) && num > 0;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The shared all-gates deposit evaluation. Used by the generic `verifyDeposit`
|
|
63
|
+
* and by `MinimaRpcProvider` (which reads the coin proof via `coinexport`).
|
|
64
|
+
*/
|
|
65
|
+
function evaluateDeposit(coin, spent, params) {
|
|
66
|
+
const unspent = spent !== true && coin.spent !== true;
|
|
67
|
+
const ownedByOwner = addressesEqual(coin.address, params.ownerAddress) || addressesEqual(coin.miniaddress ?? coin.address, params.ownerAddress);
|
|
68
|
+
const tokenMatches = params.tokenId ? coin.tokenid === params.tokenId : coin.tokenid === '0x00' || coin.tokenid === '0x01';
|
|
69
|
+
const amountSufficient = params.claimedAmount === undefined || compareAmounts(coin.amount, params.claimedAmount) >= 0;
|
|
70
|
+
const confirmed = isConfirmed(coin);
|
|
71
|
+
const gates = [unspent, ownedByOwner, tokenMatches, amountSufficient];
|
|
72
|
+
if (params.requireConfirmed)
|
|
73
|
+
gates.push(confirmed);
|
|
74
|
+
return {
|
|
75
|
+
valid: gates.every(Boolean),
|
|
76
|
+
exists: true,
|
|
77
|
+
unspent,
|
|
78
|
+
confirmed,
|
|
79
|
+
ownedByOwner,
|
|
80
|
+
tokenMatches,
|
|
81
|
+
amountSufficient,
|
|
82
|
+
reason: gates.every(Boolean) ? undefined : 'deposit funding check failed',
|
|
83
|
+
coin,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function notFoundResult(reason) {
|
|
87
|
+
return {
|
|
88
|
+
valid: false,
|
|
89
|
+
exists: false,
|
|
90
|
+
unspent: false,
|
|
91
|
+
confirmed: false,
|
|
92
|
+
ownedByOwner: false,
|
|
93
|
+
tokenMatches: false,
|
|
94
|
+
amountSufficient: false,
|
|
95
|
+
reason,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Live deposit check against a chain provider. All gates must hold:
|
|
100
|
+
* 1. the coin exists;
|
|
101
|
+
* 2. it is unspent (confirmed on-chain, never a declared flag);
|
|
102
|
+
* 3. it is owned by `ownerAddress` (Mx or 0x-root form);
|
|
103
|
+
* 4. its tokenid matches (base MINIMA = '0x00' when none is requested);
|
|
104
|
+
* 5. its amount covers `claimedAmount`;
|
|
105
|
+
* 6. when `requireConfirmed` is set, the coin is chain-confirmed.
|
|
106
|
+
*/
|
|
107
|
+
async function verifyDeposit(provider, params) {
|
|
108
|
+
let coin;
|
|
109
|
+
try {
|
|
110
|
+
coin = await provider.getCoin(params.coinId);
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
return {
|
|
114
|
+
valid: false,
|
|
115
|
+
exists: false,
|
|
116
|
+
unspent: false,
|
|
117
|
+
confirmed: false,
|
|
118
|
+
ownedByOwner: false,
|
|
119
|
+
tokenMatches: false,
|
|
120
|
+
amountSufficient: false,
|
|
121
|
+
reason: 'unable to reach chain provider',
|
|
122
|
+
error,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
if (!coin) {
|
|
126
|
+
return notFoundResult(`coin ${params.coinId} not found on chain`);
|
|
127
|
+
}
|
|
128
|
+
return evaluateDeposit(coin, coin.spent === true, params);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Offline verification of a legacy chunk MMR proof against an expected root.
|
|
132
|
+
* Pure function — a peer holding (leafPubkey, proof, root) can re-validate a
|
|
133
|
+
* deposit commitment without node access. The proof is encoded to the wasm
|
|
134
|
+
* contract (hex `data`, decimal-string `value` per chunk).
|
|
135
|
+
*/
|
|
136
|
+
function verifyDepositMmrProof(leafPubkey, proof, expectedRoot) {
|
|
137
|
+
const proofJson = JSON.stringify({
|
|
138
|
+
chunks: proof.chunks.map((c) => ({
|
|
139
|
+
isLeft: c.isLeft,
|
|
140
|
+
mmrData: {
|
|
141
|
+
data: (0, core_1.bytesToHex)(c.mmrData.data),
|
|
142
|
+
value: c.mmrData.value.toString(),
|
|
143
|
+
},
|
|
144
|
+
})),
|
|
145
|
+
});
|
|
146
|
+
return (0, core_1.verifyMMRProof)(leafPubkey, proofJson, expectedRoot);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Deterministic, domain-separated funding address for an LP deposit. In
|
|
150
|
+
* production this is the channel/pool script address the LP pays into and the
|
|
151
|
+
* funding check is run against; keeping it a pure derivation keeps issue #25
|
|
152
|
+
* constructable and checkable without a live script compiler.
|
|
153
|
+
*/
|
|
154
|
+
function depositAddressFor(lp, opts) {
|
|
155
|
+
const scope = [
|
|
156
|
+
exports.DEPOSIT_ADDRESS_DOMAIN,
|
|
157
|
+
lp,
|
|
158
|
+
opts?.poolId ?? '',
|
|
159
|
+
opts?.tokenId ?? '0x00',
|
|
160
|
+
].join('|');
|
|
161
|
+
return `Mx${(0, core_1.toHex)((0, core_1.sha3_256)(new TextEncoder().encode(scope))).slice(0, 80)}`;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Default `DepositVerifier` over any `ChainStateProvider`. The live path uses
|
|
165
|
+
* `getCoin`; the MMR-root path returns `null` unless the base provider knows a
|
|
166
|
+
* root (concrete providers override `getMmrRoot`).
|
|
167
|
+
*/
|
|
168
|
+
function withDepositVerifier(provider) {
|
|
169
|
+
const verifier = {
|
|
170
|
+
verifyDeposit: (params) => verifyDeposit(provider, params),
|
|
171
|
+
depositAddressFor,
|
|
172
|
+
getMmrRoot: async () => null,
|
|
173
|
+
verifyMmrDeposit: (params) => Promise.resolve(verifyDepositMmrProof(params.leafPubkey, params.proof, params.expectedRoot)),
|
|
174
|
+
};
|
|
175
|
+
// Prototype-chain the wrapper onto the provider so class methods stay
|
|
176
|
+
// reachable and `this` still resolves against the original instance (AUD-039).
|
|
177
|
+
return Object.assign(Object.create(provider), verifier);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Parse a decimal amount — possibly a MiniNumber "decimal.uuid" string — into
|
|
181
|
+
* its whole part and full fractional digit string. The numeric prefix is used
|
|
182
|
+
* and fractional precision is preserved, never truncated (AUD-038).
|
|
183
|
+
*/
|
|
184
|
+
function splitAmount(decimalOrMinima) {
|
|
185
|
+
const match = /^([0-9]+(?:\.[0-9]+)?)/.exec(decimalOrMinima.trim());
|
|
186
|
+
const num = match ? match[1] : decimalOrMinima.trim();
|
|
187
|
+
if (!num || !/^[0-9]+(\.[0-9]+)?$/.test(num))
|
|
188
|
+
return { whole: 0n, frac: '' };
|
|
189
|
+
const [whole, frac] = num.split('.');
|
|
190
|
+
return { whole: BigInt(whole), frac: frac ?? '' };
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Compare two decimal amounts exactly at a common scale derived from the
|
|
194
|
+
* inputs. Neither side is fixed-truncated, so a claim can never be rounded
|
|
195
|
+
* down into passing (AUD-038).
|
|
196
|
+
*/
|
|
197
|
+
function compareAmounts(a, b) {
|
|
198
|
+
const left = splitAmount(a);
|
|
199
|
+
const right = splitAmount(b);
|
|
200
|
+
if (left.whole < right.whole)
|
|
201
|
+
return -1;
|
|
202
|
+
if (left.whole > right.whole)
|
|
203
|
+
return 1;
|
|
204
|
+
const scale = Math.max(left.frac.length, right.frac.length);
|
|
205
|
+
const leftFrac = BigInt(left.frac.padEnd(scale, '0') || '0');
|
|
206
|
+
const rightFrac = BigInt(right.frac.padEnd(scale, '0') || '0');
|
|
207
|
+
if (leftFrac < rightFrac)
|
|
208
|
+
return -1;
|
|
209
|
+
if (leftFrac > rightFrac)
|
|
210
|
+
return 1;
|
|
211
|
+
return 0;
|
|
212
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,22 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Object.defineProperty(exports, "verifyDeposit", { enumerable: true, get: function () { return verify_deposit_js_1.verifyDeposit; } });
|
|
8
|
-
Object.defineProperty(exports, "verifyDepositMmrProof", { enumerable: true, get: function () { return verify_deposit_js_1.verifyDepositMmrProof; } });
|
|
9
|
-
Object.defineProperty(exports, "withDepositVerifier", { enumerable: true, get: function () { return verify_deposit_js_1.withDepositVerifier; } });
|
|
10
|
-
var hosted_js_1 = require("./providers/hosted.js");
|
|
11
|
-
Object.defineProperty(exports, "HostedProvider", { enumerable: true, get: function () { return hosted_js_1.HostedProvider; } });
|
|
12
|
-
var minima_rpc_js_1 = require("./providers/minima-rpc.js");
|
|
13
|
-
Object.defineProperty(exports, "MinimaRpcProvider", { enumerable: true, get: function () { return minima_rpc_js_1.MinimaRpcProvider; } });
|
|
14
|
-
var lookup_client_js_1 = require("./providers/lookup-client.js");
|
|
15
|
-
Object.defineProperty(exports, "LookupClientProvider", { enumerable: true, get: function () { return lookup_client_js_1.LookupClientProvider; } });
|
|
16
|
-
var composite_js_1 = require("./providers/composite.js");
|
|
17
|
-
Object.defineProperty(exports, "CompositeProvider", { enumerable: true, get: function () { return composite_js_1.CompositeProvider; } });
|
|
18
|
-
var resolve_js_1 = require("./resolve.js");
|
|
19
|
-
Object.defineProperty(exports, "resolveChainProvider", { enumerable: true, get: function () { return resolve_js_1.resolveChainProvider; } });
|
|
20
|
-
Object.defineProperty(exports, "parseMinimaRpcUrl", { enumerable: true, get: function () { return resolve_js_1.parseMinimaRpcUrl; } });
|
|
21
|
-
Object.defineProperty(exports, "assertConsentedNodeUrl", { enumerable: true, get: function () { return resolve_js_1.assertConsentedNodeUrl; } });
|
|
22
|
-
Object.defineProperty(exports, "isSelfHosted", { enumerable: true, get: function () { return resolve_js_1.isSelfHosted; } });
|
|
1
|
+
export { DEPOSIT_ADDRESS_DOMAIN, depositAddressFor, verifyDeposit, verifyDepositMmrProof, withDepositVerifier, } from './verify-deposit.js';
|
|
2
|
+
export { HostedProvider } from './providers/hosted.js';
|
|
3
|
+
export { MinimaRpcProvider } from './providers/minima-rpc.js';
|
|
4
|
+
export { LookupClientProvider } from './providers/lookup-client.js';
|
|
5
|
+
export { CompositeProvider } from './providers/composite.js';
|
|
6
|
+
export { resolveChainProvider, parseMinimaRpcUrl, assertConsentedNodeUrl, isSelfHosted, } from './resolve.js';
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* CompositeProvider — tries primary provider, falls back to secondary on any error.
|
|
4
3
|
*/
|
|
5
|
-
|
|
6
|
-
exports.CompositeProvider = void 0;
|
|
7
|
-
class CompositeProvider {
|
|
4
|
+
export class CompositeProvider {
|
|
8
5
|
constructor(primary, fallback, onFallback) {
|
|
9
6
|
this.primary = primary;
|
|
10
7
|
this.fallback = fallback;
|
|
@@ -51,4 +48,3 @@ class CompositeProvider {
|
|
|
51
48
|
return this.withArg('broadcastTxPoW', txpowHex);
|
|
52
49
|
}
|
|
53
50
|
}
|
|
54
|
-
exports.CompositeProvider = CompositeProvider;
|
package/dist/providers/hosted.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* HostedProvider — wraps Axia REST + RPC API endpoints.
|
|
4
3
|
*
|
|
@@ -10,8 +9,6 @@
|
|
|
10
9
|
* For dApp developers using custom project API keys the `/v1/wallet/*` routes
|
|
11
10
|
* require x-api-key: 'totem-shared'. Use the apiKey config option accordingly.
|
|
12
11
|
*/
|
|
13
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.HostedProvider = void 0;
|
|
15
12
|
function sanitizeRpcParam(value, name) {
|
|
16
13
|
if (value.length > 1024) {
|
|
17
14
|
throw new Error(`RPC parameter ${name} exceeds maximum length (1024)`);
|
|
@@ -25,7 +22,7 @@ function sanitizeRpcParam(value, name) {
|
|
|
25
22
|
}
|
|
26
23
|
return trimmed;
|
|
27
24
|
}
|
|
28
|
-
class HostedProvider {
|
|
25
|
+
export class HostedProvider {
|
|
29
26
|
constructor(config) {
|
|
30
27
|
this.baseUrl = config.baseUrl.replace(/\/$/, '');
|
|
31
28
|
if (!this.baseUrl.startsWith('https://')) {
|
|
@@ -240,4 +237,3 @@ class HostedProvider {
|
|
|
240
237
|
};
|
|
241
238
|
}
|
|
242
239
|
}
|
|
243
|
-
exports.HostedProvider = HostedProvider;
|
|
@@ -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;
|
|
@@ -22,9 +22,28 @@ export declare class MinimaRpcProvider implements ChainStateProvider, DepositVer
|
|
|
22
22
|
/** MMR root at tip — the anchor peers verify offline proofs against. */
|
|
23
23
|
getMmrRoot(): Promise<string | null>;
|
|
24
24
|
getProof(coinId: string): Promise<MMRProof>;
|
|
25
|
+
/**
|
|
26
|
+
* Current minimum TxPoW work from the tip block's Magic numbers (`magic`
|
|
27
|
+
* command). This is the live target locally mined transactions must meet;
|
|
28
|
+
* using it avoids hardcoding a difficulty that may be harder than the
|
|
29
|
+
* network currently requires (or easier than it has drifted to).
|
|
30
|
+
*/
|
|
31
|
+
getMinTxPowWork(): Promise<string | null>;
|
|
25
32
|
getTip(): Promise<ChainTip>;
|
|
26
33
|
getToken(tokenId: string): Promise<TokenInfo>;
|
|
27
34
|
searchTokens(query: TokenSearchQuery): Promise<TokenInfo[]>;
|
|
28
35
|
getTokensByCreator(address: string): Promise<TokenInfo[]>;
|
|
36
|
+
/**
|
|
37
|
+
* Submit a raw transaction + witness via the node's explicit-mine path.
|
|
38
|
+
*
|
|
39
|
+
* `sendtransaction` only adds a pre-built TxPoW to the mempool; a TxPoW built
|
|
40
|
+
* outside the node has a placeholder header (block number 0, block difficulty
|
|
41
|
+
* MAX_HASH), which the node's block builder does not reliably promote. The
|
|
42
|
+
* node's own `txnmine` builds the header with the real tip fields and mines
|
|
43
|
+
* it. We then post that mined TxPoW with `sendtransaction`.
|
|
44
|
+
*
|
|
45
|
+
* TxnRow wire format (Java/C++): MiniString(id) || Transaction || Witness.
|
|
46
|
+
*/
|
|
47
|
+
submitTransaction(txBytes: Uint8Array, witnessBytes: Uint8Array, id?: string): Promise<BroadcastResult>;
|
|
29
48
|
broadcastTxPoW(txpowHex: string): Promise<BroadcastResult>;
|
|
30
49
|
}
|
|
@@ -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
|
}
|
|
@@ -21,6 +18,19 @@ class MinimaRpcProvider {
|
|
|
21
18
|
return result;
|
|
22
19
|
}
|
|
23
20
|
async getCoin(coinId) {
|
|
21
|
+
// `coins` only returns wallet-relevant coins, so it never sees coins locked
|
|
22
|
+
// to an app/channel script. `coinexport` is the chain-level primitive and is
|
|
23
|
+
// the only reliable way to resolve an arbitrary coinid (see verifyDeposit).
|
|
24
|
+
try {
|
|
25
|
+
const exported = await this.client.coinExport(coinId);
|
|
26
|
+
const coin = exported?.coinproof?.coin;
|
|
27
|
+
if (coin) {
|
|
28
|
+
return { ...coin, spent: coin.spent === true };
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// fall through to the wallet query
|
|
33
|
+
}
|
|
24
34
|
try {
|
|
25
35
|
const coins = await this.getCoins({ coinId });
|
|
26
36
|
return coins[0] ?? null;
|
|
@@ -41,9 +51,9 @@ class MinimaRpcProvider {
|
|
|
41
51
|
const coin = exported?.coinproof?.coin;
|
|
42
52
|
const spent = coin?.spent === true;
|
|
43
53
|
if (!coin) {
|
|
44
|
-
return
|
|
54
|
+
return notFoundResult(`coin ${params.coinId} not found on chain`);
|
|
45
55
|
}
|
|
46
|
-
return
|
|
56
|
+
return evaluateDeposit(coin, spent, params);
|
|
47
57
|
}
|
|
48
58
|
catch (error) {
|
|
49
59
|
return {
|
|
@@ -60,7 +70,7 @@ class MinimaRpcProvider {
|
|
|
60
70
|
}
|
|
61
71
|
}
|
|
62
72
|
depositAddressFor(lp, opts) {
|
|
63
|
-
return
|
|
73
|
+
return depositAddressFor(lp, opts);
|
|
64
74
|
}
|
|
65
75
|
/** MMR root at tip — the anchor peers verify offline proofs against. */
|
|
66
76
|
async getMmrRoot() {
|
|
@@ -70,6 +80,21 @@ class MinimaRpcProvider {
|
|
|
70
80
|
async getProof(coinId) {
|
|
71
81
|
return this.client.mmrProof(coinId);
|
|
72
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* Current minimum TxPoW work from the tip block's Magic numbers (`magic`
|
|
85
|
+
* command). This is the live target locally mined transactions must meet;
|
|
86
|
+
* using it avoids hardcoding a difficulty that may be harder than the
|
|
87
|
+
* network currently requires (or easier than it has drifted to).
|
|
88
|
+
*/
|
|
89
|
+
async getMinTxPowWork() {
|
|
90
|
+
try {
|
|
91
|
+
const info = await this.client.magic();
|
|
92
|
+
return info?.lastblock?.currentmintxpowwork ?? null;
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
73
98
|
async getTip() {
|
|
74
99
|
return this.client.getTip();
|
|
75
100
|
}
|
|
@@ -106,13 +131,58 @@ class MinimaRpcProvider {
|
|
|
106
131
|
async getTokensByCreator(address) {
|
|
107
132
|
return this.searchTokens({ creatorAddress: address });
|
|
108
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* Submit a raw transaction + witness via the node's explicit-mine path.
|
|
136
|
+
*
|
|
137
|
+
* `sendtransaction` only adds a pre-built TxPoW to the mempool; a TxPoW built
|
|
138
|
+
* outside the node has a placeholder header (block number 0, block difficulty
|
|
139
|
+
* MAX_HASH), which the node's block builder does not reliably promote. The
|
|
140
|
+
* node's own `txnmine` builds the header with the real tip fields and mines
|
|
141
|
+
* it. We then post that mined TxPoW with `sendtransaction`.
|
|
142
|
+
*
|
|
143
|
+
* TxnRow wire format (Java/C++): MiniString(id) || Transaction || Witness.
|
|
144
|
+
*/
|
|
145
|
+
async submitTransaction(txBytes, witnessBytes, id = `omnia-${Date.now()}`) {
|
|
146
|
+
try {
|
|
147
|
+
const idBytes = new TextEncoder().encode(id);
|
|
148
|
+
const len = idBytes.length;
|
|
149
|
+
const prefix = new Uint8Array([
|
|
150
|
+
(len >>> 24) & 0xff,
|
|
151
|
+
(len >>> 16) & 0xff,
|
|
152
|
+
(len >>> 8) & 0xff,
|
|
153
|
+
len & 0xff,
|
|
154
|
+
]);
|
|
155
|
+
const row = Buffer.concat([
|
|
156
|
+
Buffer.from(prefix),
|
|
157
|
+
Buffer.from(idBytes),
|
|
158
|
+
Buffer.from(txBytes),
|
|
159
|
+
Buffer.from(witnessBytes),
|
|
160
|
+
]);
|
|
161
|
+
const mined = await this.client.txnMine({ data: Buffer.from(row).toString('hex') });
|
|
162
|
+
if (!mined?.data) {
|
|
163
|
+
return { success: false, message: 'txnmine returned no data' };
|
|
164
|
+
}
|
|
165
|
+
const posted = await this.client.sendTransaction(mined.data);
|
|
166
|
+
return {
|
|
167
|
+
success: true,
|
|
168
|
+
txpowid: mined.txpowid ?? posted?.txpowid,
|
|
169
|
+
message: 'submitted via txnmine + sendtransaction',
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
catch (e) {
|
|
173
|
+
return { success: false, message: String(e) };
|
|
174
|
+
}
|
|
175
|
+
}
|
|
109
176
|
async broadcastTxPoW(txpowHex) {
|
|
110
177
|
try {
|
|
111
|
-
|
|
178
|
+
// `sendtransaction` submits a pre-mined TxPoW to the mempool where the
|
|
179
|
+
// node mines it into a block. `txnminepost` only posts the TxPoW (via the
|
|
180
|
+
// peer NIO path) and does not get it mined.
|
|
181
|
+
const result = await this.client.sendTransaction(txpowHex);
|
|
112
182
|
return {
|
|
113
183
|
success: true,
|
|
114
184
|
txpowid: result?.txpowid,
|
|
115
|
-
message: 'broadcast via MinimaRpc
|
|
185
|
+
message: 'broadcast via MinimaRpc sendtransaction',
|
|
116
186
|
};
|
|
117
187
|
}
|
|
118
188
|
catch (e) {
|
|
@@ -120,4 +190,3 @@ class MinimaRpcProvider {
|
|
|
120
190
|
}
|
|
121
191
|
}
|
|
122
192
|
}
|
|
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
|
@@ -134,4 +134,17 @@ export interface ChainStateProvider {
|
|
|
134
134
|
searchTokens(query: TokenSearchQuery): Promise<TokenInfo[]>;
|
|
135
135
|
getTokensByCreator(address: string): Promise<TokenInfo[]>;
|
|
136
136
|
broadcastTxPoW(txpowHex: string): Promise<BroadcastResult>;
|
|
137
|
+
/**
|
|
138
|
+
* Live minimum TxPoW work (32-byte hex target) from the node's Magic numbers.
|
|
139
|
+
* Optional: providers that cannot read it let callers fall back to the
|
|
140
|
+
* protocol default. Used to mine transactions to the current network target.
|
|
141
|
+
*/
|
|
142
|
+
getMinTxPowWork?(): Promise<string | null>;
|
|
143
|
+
/**
|
|
144
|
+
* Submit a raw transaction + witness through the node's explicit-mine path:
|
|
145
|
+
* `txnmine` (node builds the TxPoW header with the real block number /
|
|
146
|
+
* difficulty / magic and mines) followed by `sendtransaction`. When absent,
|
|
147
|
+
* callers fall back to SDK-side mining + `broadcastTxPoW`.
|
|
148
|
+
*/
|
|
149
|
+
submitTransaction?(txBytes: Uint8Array, witnessBytes: Uint8Array, id?: string): Promise<BroadcastResult>;
|
|
137
150
|
}
|
package/dist/types.js
CHANGED