@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
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,14 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@totemsdk/chain-provider",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
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": [
|
|
@@ -17,8 +22,8 @@
|
|
|
17
22
|
"LICENSE"
|
|
18
23
|
],
|
|
19
24
|
"dependencies": {
|
|
20
|
-
"@totemsdk/minima-rpc": "^0.
|
|
21
|
-
"@totemsdk/core": "^1.
|
|
25
|
+
"@totemsdk/minima-rpc": "^0.4.0",
|
|
26
|
+
"@totemsdk/core": "^1.4.0"
|
|
22
27
|
},
|
|
23
28
|
"devDependencies": {
|
|
24
29
|
"@types/jest": "^30.0.0",
|
|
@@ -58,8 +63,9 @@
|
|
|
58
63
|
"chain-data",
|
|
59
64
|
"strategy-pattern"
|
|
60
65
|
],
|
|
66
|
+
"type": "module",
|
|
61
67
|
"scripts": {
|
|
62
|
-
"build": "tsc",
|
|
68
|
+
"build": "tsc && tsc -p tsconfig.cjs.json && node -e \"require('fs').writeFileSync('dist/cjs/package.json', JSON.stringify({type:'commonjs'})+'\\n')\"",
|
|
63
69
|
"clean": "rm -rf dist",
|
|
64
70
|
"test": "jest"
|
|
65
71
|
}
|