@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
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* HostedProvider — wraps Axia REST + RPC API endpoints.
|
|
4
|
+
*
|
|
5
|
+
* Endpoint map (all require x-api-key header):
|
|
6
|
+
* GET /v1/wallet/utxos/:address — coin list (totem-shared key)
|
|
7
|
+
* POST /v1/wallet/rpc — Minima RPC commands (totem-shared key)
|
|
8
|
+
* POST /api/meg/postminedtxn — broadcast mined TxPoW (no auth required)
|
|
9
|
+
*
|
|
10
|
+
* For dApp developers using custom project API keys the `/v1/wallet/*` routes
|
|
11
|
+
* require x-api-key: 'totem-shared'. Use the apiKey config option accordingly.
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.HostedProvider = void 0;
|
|
15
|
+
function sanitizeRpcParam(value, name) {
|
|
16
|
+
if (value.length > 1024) {
|
|
17
|
+
throw new Error(`RPC parameter ${name} exceeds maximum length (1024)`);
|
|
18
|
+
}
|
|
19
|
+
if (/[\x00-\x1f\x7f]/.test(value)) {
|
|
20
|
+
throw new Error(`RPC parameter ${name} contains control characters`);
|
|
21
|
+
}
|
|
22
|
+
const trimmed = value.trim();
|
|
23
|
+
if (trimmed.length === 0) {
|
|
24
|
+
throw new Error(`RPC parameter ${name} is empty after trimming`);
|
|
25
|
+
}
|
|
26
|
+
return trimmed;
|
|
27
|
+
}
|
|
28
|
+
class HostedProvider {
|
|
29
|
+
constructor(config) {
|
|
30
|
+
this.baseUrl = config.baseUrl.replace(/\/$/, '');
|
|
31
|
+
if (!this.baseUrl.startsWith('https://')) {
|
|
32
|
+
throw new Error('HostedProvider requires HTTPS — credentials sent via x-api-key header');
|
|
33
|
+
}
|
|
34
|
+
this.apiKey = config.apiKey;
|
|
35
|
+
this.timeoutMs = config.timeoutMs ?? 30000;
|
|
36
|
+
}
|
|
37
|
+
async fetchJson(path, init) {
|
|
38
|
+
const controller = new AbortController();
|
|
39
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
40
|
+
try {
|
|
41
|
+
const res = await fetch(`${this.baseUrl}${path}`, {
|
|
42
|
+
...init,
|
|
43
|
+
signal: controller.signal,
|
|
44
|
+
headers: {
|
|
45
|
+
'x-api-key': this.apiKey,
|
|
46
|
+
'Content-Type': 'application/json',
|
|
47
|
+
...(init?.headers ?? {}),
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
if (!res.ok) {
|
|
51
|
+
throw new Error(`HostedProvider HTTP ${res.status} for ${path}`);
|
|
52
|
+
}
|
|
53
|
+
return res.json();
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
clearTimeout(timer);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Send a Minima command to POST /v1/wallet/rpc.
|
|
61
|
+
* Returns the `response` field of the Minima envelope.
|
|
62
|
+
*/
|
|
63
|
+
async rpc(command) {
|
|
64
|
+
const controller = new AbortController();
|
|
65
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
66
|
+
try {
|
|
67
|
+
const res = await fetch(`${this.baseUrl}/v1/wallet/rpc`, {
|
|
68
|
+
method: 'POST',
|
|
69
|
+
signal: controller.signal,
|
|
70
|
+
headers: {
|
|
71
|
+
'Content-Type': 'text/plain',
|
|
72
|
+
'x-api-key': this.apiKey,
|
|
73
|
+
},
|
|
74
|
+
body: command,
|
|
75
|
+
});
|
|
76
|
+
if (!res.ok)
|
|
77
|
+
throw new Error(`HostedProvider RPC "${command}" HTTP ${res.status}`);
|
|
78
|
+
const json = await res.json();
|
|
79
|
+
if (json.status === false)
|
|
80
|
+
throw new Error(`RPC "${command}" failed: ${json.error ?? 'unknown'}`);
|
|
81
|
+
return json.response;
|
|
82
|
+
}
|
|
83
|
+
finally {
|
|
84
|
+
clearTimeout(timer);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Get coins (UTXOs) for an address.
|
|
89
|
+
* Maps GET /v1/wallet/utxos/:address → Coin[].
|
|
90
|
+
*/
|
|
91
|
+
async getCoins(query) {
|
|
92
|
+
if (!query.address && !query.coinId)
|
|
93
|
+
return [];
|
|
94
|
+
if (query.coinId) {
|
|
95
|
+
const coin = await this.getCoin(query.coinId);
|
|
96
|
+
return coin ? [coin] : [];
|
|
97
|
+
}
|
|
98
|
+
const data = await this.fetchJson(`/v1/wallet/utxos/${encodeURIComponent(query.address)}`);
|
|
99
|
+
const raw = data.utxos ?? [];
|
|
100
|
+
return raw
|
|
101
|
+
.filter((u) => {
|
|
102
|
+
if (query.tokenId && u.tokenid !== query.tokenId)
|
|
103
|
+
return false;
|
|
104
|
+
if (query.sendable !== undefined && !!u.spent !== !query.sendable)
|
|
105
|
+
return false;
|
|
106
|
+
return true;
|
|
107
|
+
})
|
|
108
|
+
.map((u) => ({
|
|
109
|
+
coinid: u.coinid ?? u.id ?? '',
|
|
110
|
+
amount: String(u.amount ?? '0'),
|
|
111
|
+
address: u.address ?? query.address ?? '',
|
|
112
|
+
miniaddress: u.miniaddress,
|
|
113
|
+
tokenid: u.tokenid ?? '0x00',
|
|
114
|
+
token: u.token,
|
|
115
|
+
storestate: u.storestate,
|
|
116
|
+
state: u.state,
|
|
117
|
+
spent: !!u.spent,
|
|
118
|
+
mmrentry: u.mmrentry,
|
|
119
|
+
created: u.created,
|
|
120
|
+
}));
|
|
121
|
+
}
|
|
122
|
+
async getCoin(coinId) {
|
|
123
|
+
try {
|
|
124
|
+
const clean = sanitizeRpcParam(coinId, 'coinId');
|
|
125
|
+
const coins = await this.rpc(`coins coinid:${clean}`);
|
|
126
|
+
const u = Array.isArray(coins) ? coins[0] : null;
|
|
127
|
+
if (!u)
|
|
128
|
+
return null;
|
|
129
|
+
return {
|
|
130
|
+
coinid: u.coinid ?? coinId,
|
|
131
|
+
amount: String(u.amount ?? '0'),
|
|
132
|
+
address: u.address ?? '',
|
|
133
|
+
miniaddress: u.miniaddress,
|
|
134
|
+
tokenid: u.tokenid ?? '0x00',
|
|
135
|
+
token: u.token,
|
|
136
|
+
storestate: u.storestate,
|
|
137
|
+
state: u.state,
|
|
138
|
+
spent: !!u.spent,
|
|
139
|
+
mmrentry: u.mmrentry,
|
|
140
|
+
created: u.created,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Get MMR proof for a coin via Minima `coinproof` RPC command.
|
|
149
|
+
*/
|
|
150
|
+
async getProof(coinId) {
|
|
151
|
+
const clean = sanitizeRpcParam(coinId, 'coinId');
|
|
152
|
+
const data = await this.rpc(`coinproof coinid:${clean}`);
|
|
153
|
+
return { coinid: coinId, data };
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Get chain tip via Minima `status` RPC — parses response.chain.
|
|
157
|
+
*/
|
|
158
|
+
async getTip() {
|
|
159
|
+
const data = await this.rpc('status');
|
|
160
|
+
const chain = data?.chain;
|
|
161
|
+
return {
|
|
162
|
+
block: chain?.block ?? 0,
|
|
163
|
+
hash: chain?.hash ?? '',
|
|
164
|
+
time: chain?.time,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Get token info via Minima `tokens tokenid:X` RPC command.
|
|
169
|
+
*/
|
|
170
|
+
async getToken(tokenId) {
|
|
171
|
+
const clean = sanitizeRpcParam(tokenId, 'tokenId');
|
|
172
|
+
const data = await this.rpc(`tokens tokenid:${clean}`);
|
|
173
|
+
const t = Array.isArray(data) ? data[0] : data;
|
|
174
|
+
if (!t)
|
|
175
|
+
throw new Error(`Token not found: ${tokenId}`);
|
|
176
|
+
return this._mapToken(t);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Search tokens via Minima `tokens` RPC command, then filter client-side.
|
|
180
|
+
*/
|
|
181
|
+
async searchTokens(query) {
|
|
182
|
+
const data = await this.rpc('tokens');
|
|
183
|
+
const all = Array.isArray(data) ? data : [];
|
|
184
|
+
return all
|
|
185
|
+
.filter((t) => {
|
|
186
|
+
if (t.tokenid === '0x00')
|
|
187
|
+
return false; // skip native Minima
|
|
188
|
+
if (query.name) {
|
|
189
|
+
const n = typeof t.name === 'object' ? (t.name?.name ?? '') : String(t.name ?? '');
|
|
190
|
+
if (!n.toLowerCase().includes(query.name.toLowerCase()))
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
return true;
|
|
194
|
+
})
|
|
195
|
+
.slice(query.offset ?? 0, query.limit ? (query.offset ?? 0) + query.limit : undefined)
|
|
196
|
+
.map(this._mapToken);
|
|
197
|
+
}
|
|
198
|
+
async getTokensByCreator(address) {
|
|
199
|
+
return this.searchTokens({ creatorAddress: address });
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Broadcast a mined TxPoW hex.
|
|
203
|
+
* Uses POST /api/meg/postminedtxn (the correct Axia MEG broadcast bridge).
|
|
204
|
+
*/
|
|
205
|
+
async broadcastTxPoW(txpowHex) {
|
|
206
|
+
try {
|
|
207
|
+
const controller = new AbortController();
|
|
208
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
209
|
+
let result;
|
|
210
|
+
try {
|
|
211
|
+
const res = await fetch(`${this.baseUrl}/api/meg/postminedtxn`, {
|
|
212
|
+
method: 'POST',
|
|
213
|
+
signal: controller.signal,
|
|
214
|
+
headers: { 'Content-Type': 'application/json' },
|
|
215
|
+
body: JSON.stringify({ data: txpowHex }),
|
|
216
|
+
});
|
|
217
|
+
result = await res.json();
|
|
218
|
+
if (!res.ok)
|
|
219
|
+
return { success: false, message: `HTTP ${res.status}: ${result?.error ?? ''}` };
|
|
220
|
+
}
|
|
221
|
+
finally {
|
|
222
|
+
clearTimeout(timer);
|
|
223
|
+
}
|
|
224
|
+
return { success: true, txpowid: result?.txpowid, message: result?.message };
|
|
225
|
+
}
|
|
226
|
+
catch (e) {
|
|
227
|
+
return { success: false, message: String(e) };
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
_mapToken(t) {
|
|
231
|
+
return {
|
|
232
|
+
tokenid: t.tokenid ?? '',
|
|
233
|
+
name: typeof t.name === 'object' ? t.name : { name: String(t.name ?? '') },
|
|
234
|
+
total: t.total,
|
|
235
|
+
confirmed: t.confirmed,
|
|
236
|
+
sendable: t.sendable,
|
|
237
|
+
coins: t.coins,
|
|
238
|
+
script: t.script,
|
|
239
|
+
description: t.description,
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
exports.HostedProvider = HostedProvider;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LookupClientProvider — ChainStateProvider backed by a @totemsdk/lookup-client instance.
|
|
3
|
+
*
|
|
4
|
+
* Accepts any object that satisfies LookupClientLike (structural duck-typing),
|
|
5
|
+
* so @totemsdk/lookup-client remains a peer dependency: chain-provider does not
|
|
6
|
+
* bundle or import it at runtime. The caller is responsible for constructing the
|
|
7
|
+
* LookupClient and passing it in.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { connectLookupNode, LookupClientProvider } from '@totemsdk/lookup-client';
|
|
12
|
+
*
|
|
13
|
+
* const client = await connectLookupNode({ hyperswarmTopic: 'deadbeef...' });
|
|
14
|
+
* const provider = new LookupClientProvider(client);
|
|
15
|
+
*
|
|
16
|
+
* const tip = await provider.getTip();
|
|
17
|
+
* const coins = await provider.getCoins({ address: 'Mx...' });
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* Use inside a CompositeProvider for sovereign-first / hosted-fallback:
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { CompositeProvider, HostedProvider, LookupClientProvider } from '@totemsdk/chain-provider';
|
|
24
|
+
* import { connectLookupNode } from '@totemsdk/lookup-client';
|
|
25
|
+
*
|
|
26
|
+
* const client = await connectLookupNode({ hyperswarmTopic: 'abc...' });
|
|
27
|
+
* const fallback = new HostedProvider({ baseUrl: 'https://api.axia.to', projectId: '...' });
|
|
28
|
+
*
|
|
29
|
+
* const provider = new CompositeProvider(
|
|
30
|
+
* new LookupClientProvider(client),
|
|
31
|
+
* fallback,
|
|
32
|
+
* );
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
import type { BroadcastResult, ChainStateProvider, ChainTip, Coin, CoinsQuery, MMRProof, TokenInfo, TokenSearchQuery } from '../types.js';
|
|
36
|
+
/**
|
|
37
|
+
* Structural interface describing the subset of LookupClient methods that
|
|
38
|
+
* LookupClientProvider requires. Any object satisfying this interface can be
|
|
39
|
+
* passed in — most commonly a `LookupClient` from @totemsdk/lookup-client.
|
|
40
|
+
*/
|
|
41
|
+
export interface LookupClientLike {
|
|
42
|
+
getCoins(query: CoinsQuery): Promise<Coin[]>;
|
|
43
|
+
getCoin(coinId: string): Promise<Coin | null>;
|
|
44
|
+
getProof(coinId: string): Promise<MMRProof>;
|
|
45
|
+
getTip(): Promise<ChainTip>;
|
|
46
|
+
getToken(tokenId: string): Promise<TokenInfo>;
|
|
47
|
+
searchTokens(query: TokenSearchQuery): Promise<TokenInfo[]>;
|
|
48
|
+
getTokensByCreator(address: string): Promise<TokenInfo[]>;
|
|
49
|
+
broadcastTxPoW(txpowHex: string): Promise<BroadcastResult>;
|
|
50
|
+
}
|
|
51
|
+
export declare class LookupClientProvider implements ChainStateProvider {
|
|
52
|
+
private readonly _client;
|
|
53
|
+
constructor(_client: LookupClientLike);
|
|
54
|
+
getCoins(query: CoinsQuery): Promise<Coin[]>;
|
|
55
|
+
getCoin(coinId: string): Promise<Coin | null>;
|
|
56
|
+
getProof(coinId: string): Promise<MMRProof>;
|
|
57
|
+
getTip(): Promise<ChainTip>;
|
|
58
|
+
getToken(tokenId: string): Promise<TokenInfo>;
|
|
59
|
+
searchTokens(query: TokenSearchQuery): Promise<TokenInfo[]>;
|
|
60
|
+
getTokensByCreator(address: string): Promise<TokenInfo[]>;
|
|
61
|
+
broadcastTxPoW(txpowHex: string): Promise<BroadcastResult>;
|
|
62
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* LookupClientProvider — ChainStateProvider backed by a @totemsdk/lookup-client instance.
|
|
4
|
+
*
|
|
5
|
+
* Accepts any object that satisfies LookupClientLike (structural duck-typing),
|
|
6
|
+
* so @totemsdk/lookup-client remains a peer dependency: chain-provider does not
|
|
7
|
+
* bundle or import it at runtime. The caller is responsible for constructing the
|
|
8
|
+
* LookupClient and passing it in.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { connectLookupNode, LookupClientProvider } from '@totemsdk/lookup-client';
|
|
13
|
+
*
|
|
14
|
+
* const client = await connectLookupNode({ hyperswarmTopic: 'deadbeef...' });
|
|
15
|
+
* const provider = new LookupClientProvider(client);
|
|
16
|
+
*
|
|
17
|
+
* const tip = await provider.getTip();
|
|
18
|
+
* const coins = await provider.getCoins({ address: 'Mx...' });
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* Use inside a CompositeProvider for sovereign-first / hosted-fallback:
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { CompositeProvider, HostedProvider, LookupClientProvider } from '@totemsdk/chain-provider';
|
|
25
|
+
* import { connectLookupNode } from '@totemsdk/lookup-client';
|
|
26
|
+
*
|
|
27
|
+
* const client = await connectLookupNode({ hyperswarmTopic: 'abc...' });
|
|
28
|
+
* const fallback = new HostedProvider({ baseUrl: 'https://api.axia.to', projectId: '...' });
|
|
29
|
+
*
|
|
30
|
+
* const provider = new CompositeProvider(
|
|
31
|
+
* new LookupClientProvider(client),
|
|
32
|
+
* fallback,
|
|
33
|
+
* );
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
exports.LookupClientProvider = void 0;
|
|
38
|
+
class LookupClientProvider {
|
|
39
|
+
constructor(_client) {
|
|
40
|
+
this._client = _client;
|
|
41
|
+
}
|
|
42
|
+
getCoins(query) {
|
|
43
|
+
return this._client.getCoins(query);
|
|
44
|
+
}
|
|
45
|
+
getCoin(coinId) {
|
|
46
|
+
return this._client.getCoin(coinId);
|
|
47
|
+
}
|
|
48
|
+
getProof(coinId) {
|
|
49
|
+
return this._client.getProof(coinId);
|
|
50
|
+
}
|
|
51
|
+
getTip() {
|
|
52
|
+
return this._client.getTip();
|
|
53
|
+
}
|
|
54
|
+
getToken(tokenId) {
|
|
55
|
+
return this._client.getToken(tokenId);
|
|
56
|
+
}
|
|
57
|
+
searchTokens(query) {
|
|
58
|
+
return this._client.searchTokens(query);
|
|
59
|
+
}
|
|
60
|
+
getTokensByCreator(address) {
|
|
61
|
+
return this._client.getTokensByCreator(address);
|
|
62
|
+
}
|
|
63
|
+
broadcastTxPoW(txpowHex) {
|
|
64
|
+
return this._client.broadcastTxPoW(txpowHex);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.LookupClientProvider = LookupClientProvider;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MinimaRpcProvider — thin wrapper over @totemsdk/minima-rpc.
|
|
3
|
+
*/
|
|
4
|
+
import type { MinimaRpcClient } from '@totemsdk/minima-rpc';
|
|
5
|
+
import type { ChainStateProvider, CoinsQuery, Coin, MMRProof, ChainTip, TokenInfo, TokenSearchQuery, AddressInfo, ExportedCoin, BroadcastResult, DepositVerifier, VerifyDepositParams, DepositVerification } from '../types.js';
|
|
6
|
+
export declare class MinimaRpcProvider implements ChainStateProvider, DepositVerifier {
|
|
7
|
+
private readonly client;
|
|
8
|
+
constructor(client: MinimaRpcClient);
|
|
9
|
+
getCoins(query: CoinsQuery): Promise<Coin[]>;
|
|
10
|
+
getAddressInfo(address: string): Promise<AddressInfo | null>;
|
|
11
|
+
exportCoin(coinId: string): Promise<ExportedCoin | null>;
|
|
12
|
+
getCoin(coinId: string): Promise<Coin | null>;
|
|
13
|
+
/**
|
|
14
|
+
* Authoritative live check via `coinexport` (the coinproof endpoint): returns
|
|
15
|
+
* found/unspent/owned/token/amount + the full coin proof. `coincheck` on
|
|
16
|
+
* totem-node wants a full proof payload rather than a coinid, so coinexport
|
|
17
|
+
* is the canonical primitive (#3).
|
|
18
|
+
*/
|
|
19
|
+
verifyDeposit(params: VerifyDepositParams): Promise<DepositVerification>;
|
|
20
|
+
depositAddressFor(lp: string, opts?: {
|
|
21
|
+
poolId?: string;
|
|
22
|
+
tokenId?: string;
|
|
23
|
+
}): string;
|
|
24
|
+
/** MMR root at tip — the anchor peers verify offline proofs against. */
|
|
25
|
+
getMmrRoot(): Promise<string | null>;
|
|
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>;
|
|
34
|
+
getTip(): Promise<ChainTip>;
|
|
35
|
+
getToken(tokenId: string): Promise<TokenInfo>;
|
|
36
|
+
searchTokens(query: TokenSearchQuery): Promise<TokenInfo[]>;
|
|
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>;
|
|
50
|
+
broadcastTxPoW(txpowHex: string): Promise<BroadcastResult>;
|
|
51
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* MinimaRpcProvider — thin wrapper over @totemsdk/minima-rpc.
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MinimaRpcProvider = void 0;
|
|
7
|
+
const verify_deposit_js_1 = require("../verify-deposit.js");
|
|
8
|
+
class MinimaRpcProvider {
|
|
9
|
+
constructor(client) {
|
|
10
|
+
this.client = client;
|
|
11
|
+
}
|
|
12
|
+
async getCoins(query) {
|
|
13
|
+
const result = await this.client.coins({
|
|
14
|
+
address: query.address,
|
|
15
|
+
tokenid: query.tokenId,
|
|
16
|
+
sendable: query.sendable,
|
|
17
|
+
relevant: query.relevant,
|
|
18
|
+
coinid: query.coinId,
|
|
19
|
+
megammr: query.megammr,
|
|
20
|
+
});
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
async getAddressInfo(address) {
|
|
24
|
+
const res = await this.client.addressInfo(address);
|
|
25
|
+
if (!res?.publickey || !res?.script)
|
|
26
|
+
return null;
|
|
27
|
+
return { address, publickey: res.publickey, script: res.script };
|
|
28
|
+
}
|
|
29
|
+
async exportCoin(coinId) {
|
|
30
|
+
const res = await this.client.coinExport(coinId);
|
|
31
|
+
if (!res?.data)
|
|
32
|
+
return null;
|
|
33
|
+
return { coinid: coinId, data: res.data };
|
|
34
|
+
}
|
|
35
|
+
async getCoin(coinId) {
|
|
36
|
+
// `coins` only returns wallet-relevant coins, so it never sees coins locked
|
|
37
|
+
// to an app/channel script. `coinexport` is the chain-level primitive and is
|
|
38
|
+
// the only reliable way to resolve an arbitrary coinid (see verifyDeposit).
|
|
39
|
+
try {
|
|
40
|
+
const exported = await this.client.coinExport(coinId);
|
|
41
|
+
const coin = exported?.coinproof?.coin;
|
|
42
|
+
if (coin) {
|
|
43
|
+
return { ...coin, spent: coin.spent === true };
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// fall through to the wallet query
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
const coins = await this.getCoins({ coinId });
|
|
51
|
+
return coins[0] ?? null;
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Authoritative live check via `coinexport` (the coinproof endpoint): returns
|
|
59
|
+
* found/unspent/owned/token/amount + the full coin proof. `coincheck` on
|
|
60
|
+
* totem-node wants a full proof payload rather than a coinid, so coinexport
|
|
61
|
+
* is the canonical primitive (#3).
|
|
62
|
+
*/
|
|
63
|
+
async verifyDeposit(params) {
|
|
64
|
+
try {
|
|
65
|
+
const exported = await this.client.coinExport(params.coinId);
|
|
66
|
+
const coin = exported?.coinproof?.coin;
|
|
67
|
+
const spent = coin?.spent === true;
|
|
68
|
+
if (!coin) {
|
|
69
|
+
return (0, verify_deposit_js_1.notFoundResult)(`coin ${params.coinId} not found on chain`);
|
|
70
|
+
}
|
|
71
|
+
return (0, verify_deposit_js_1.evaluateDeposit)(coin, spent, params);
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
return {
|
|
75
|
+
valid: false,
|
|
76
|
+
exists: false,
|
|
77
|
+
unspent: false,
|
|
78
|
+
confirmed: false,
|
|
79
|
+
ownedByOwner: false,
|
|
80
|
+
tokenMatches: false,
|
|
81
|
+
amountSufficient: false,
|
|
82
|
+
reason: 'unable to reach chain provider',
|
|
83
|
+
error,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
depositAddressFor(lp, opts) {
|
|
88
|
+
return (0, verify_deposit_js_1.depositAddressFor)(lp, opts);
|
|
89
|
+
}
|
|
90
|
+
/** MMR root at tip — the anchor peers verify offline proofs against. */
|
|
91
|
+
async getMmrRoot() {
|
|
92
|
+
const info = await this.client.megammr();
|
|
93
|
+
return info?.hash ?? null;
|
|
94
|
+
}
|
|
95
|
+
async getProof(coinId) {
|
|
96
|
+
return this.client.mmrProof(coinId);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Current minimum TxPoW work from the tip block's Magic numbers (`magic`
|
|
100
|
+
* command). This is the live target locally mined transactions must meet;
|
|
101
|
+
* using it avoids hardcoding a difficulty that may be harder than the
|
|
102
|
+
* network currently requires (or easier than it has drifted to).
|
|
103
|
+
*/
|
|
104
|
+
async getMinTxPowWork() {
|
|
105
|
+
try {
|
|
106
|
+
const info = await this.client.magic();
|
|
107
|
+
return info?.lastblock?.currentmintxpowwork ?? null;
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async getTip() {
|
|
114
|
+
return this.client.getTip();
|
|
115
|
+
}
|
|
116
|
+
async getToken(tokenId) {
|
|
117
|
+
const tokens = await this.client.tokens(tokenId);
|
|
118
|
+
if (!tokens || tokens.length === 0) {
|
|
119
|
+
throw new Error(`Token not found: ${tokenId}`);
|
|
120
|
+
}
|
|
121
|
+
return tokens[0];
|
|
122
|
+
}
|
|
123
|
+
async searchTokens(query) {
|
|
124
|
+
const all = await this.client.tokens();
|
|
125
|
+
let results = all;
|
|
126
|
+
if (query.name) {
|
|
127
|
+
const needle = query.name.toLowerCase();
|
|
128
|
+
results = results.filter((t) => {
|
|
129
|
+
const nameStr = JSON.stringify(t.name ?? '').toLowerCase();
|
|
130
|
+
return nameStr.includes(needle);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
if (query.creatorAddress) {
|
|
134
|
+
const addr = query.creatorAddress;
|
|
135
|
+
results = results.filter((t) => {
|
|
136
|
+
const desc = JSON.stringify(t.description ?? '');
|
|
137
|
+
return desc.includes(addr);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
if (query.offset)
|
|
141
|
+
results = results.slice(query.offset);
|
|
142
|
+
if (query.limit)
|
|
143
|
+
results = results.slice(0, query.limit);
|
|
144
|
+
return results;
|
|
145
|
+
}
|
|
146
|
+
async getTokensByCreator(address) {
|
|
147
|
+
return this.searchTokens({ creatorAddress: address });
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Submit a raw transaction + witness via the node's explicit-mine path.
|
|
151
|
+
*
|
|
152
|
+
* `sendtransaction` only adds a pre-built TxPoW to the mempool; a TxPoW built
|
|
153
|
+
* outside the node has a placeholder header (block number 0, block difficulty
|
|
154
|
+
* MAX_HASH), which the node's block builder does not reliably promote. The
|
|
155
|
+
* node's own `txnmine` builds the header with the real tip fields and mines
|
|
156
|
+
* it. We then post that mined TxPoW with `sendtransaction`.
|
|
157
|
+
*
|
|
158
|
+
* TxnRow wire format (Java/C++): MiniString(id) || Transaction || Witness.
|
|
159
|
+
*/
|
|
160
|
+
async submitTransaction(txBytes, witnessBytes, id = `omnia-${Date.now()}`) {
|
|
161
|
+
try {
|
|
162
|
+
const idBytes = new TextEncoder().encode(id);
|
|
163
|
+
const len = idBytes.length;
|
|
164
|
+
const prefix = new Uint8Array([
|
|
165
|
+
(len >>> 24) & 0xff,
|
|
166
|
+
(len >>> 16) & 0xff,
|
|
167
|
+
(len >>> 8) & 0xff,
|
|
168
|
+
len & 0xff,
|
|
169
|
+
]);
|
|
170
|
+
const row = Buffer.concat([
|
|
171
|
+
Buffer.from(prefix),
|
|
172
|
+
Buffer.from(idBytes),
|
|
173
|
+
Buffer.from(txBytes),
|
|
174
|
+
Buffer.from(witnessBytes),
|
|
175
|
+
]);
|
|
176
|
+
const mined = await this.client.txnMine({ data: Buffer.from(row).toString('hex') });
|
|
177
|
+
if (!mined?.data) {
|
|
178
|
+
return { success: false, message: 'txnmine returned no data' };
|
|
179
|
+
}
|
|
180
|
+
const posted = await this.client.sendTransaction(mined.data);
|
|
181
|
+
return {
|
|
182
|
+
success: true,
|
|
183
|
+
txpowid: mined.txpowid ?? posted?.txpowid,
|
|
184
|
+
message: 'submitted via txnmine + sendtransaction',
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
catch (e) {
|
|
188
|
+
return { success: false, message: String(e) };
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
async broadcastTxPoW(txpowHex) {
|
|
192
|
+
try {
|
|
193
|
+
// `sendtransaction` submits a pre-mined TxPoW to the mempool where the
|
|
194
|
+
// node mines it into a block. `txnminepost` only posts the TxPoW (via the
|
|
195
|
+
// peer NIO path) and does not get it mined.
|
|
196
|
+
const result = await this.client.sendTransaction(txpowHex);
|
|
197
|
+
return {
|
|
198
|
+
success: true,
|
|
199
|
+
txpowid: result?.txpowid,
|
|
200
|
+
message: 'broadcast via MinimaRpc sendtransaction',
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
catch (e) {
|
|
204
|
+
return { success: false, message: String(e) };
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
exports.MinimaRpcProvider = MinimaRpcProvider;
|