@ocap/client 1.30.24 → 1.30.25
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/base.js +98 -8
- package/dist/base.js.map +1 -1
- package/dist/browser.d.ts +1 -0
- package/dist/bundle.js +1 -1
- package/dist/client.js +3 -0
- package/dist/client.js.map +1 -1
- package/dist/encode.js +9 -4
- package/dist/encode.js.map +1 -1
- package/dist/extension.js +222 -11
- package/dist/extension.js.map +1 -1
- package/dist/report.html +2 -2
- package/dist/verify.js +278 -0
- package/dist/verify.js.map +1 -0
- package/lib/base.js +98 -8
- package/lib/base.js.map +1 -1
- package/lib/client.js +3 -0
- package/lib/client.js.map +1 -1
- package/lib/encode.js +9 -4
- package/lib/encode.js.map +1 -1
- package/lib/extension.js +222 -11
- package/lib/extension.js.map +1 -1
- package/lib/node.d.ts +1 -0
- package/lib/verify.js +280 -0
- package/lib/verify.js.map +1 -0
- package/package.json +11 -11
package/lib/extension.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const { get, set, omit, camelCase } = require('./helpers/lite');
|
|
4
|
-
const { isValid } = require('@arcblock/did');
|
|
4
|
+
const { isValid, isFromPublicKey, toTypeInfo } = require('@arcblock/did');
|
|
5
5
|
const ocapProto = require('@ocap/proto/schema');
|
|
6
6
|
const { toTxHash } = require('@ocap/mcrypto');
|
|
7
|
+
const { fromPublicKey } = require('@ocap/wallet');
|
|
7
8
|
const { encodeTx: encodeTxCbor, decodeTx: decodeTxCbor } = require('@ocap/message/cbor');
|
|
8
9
|
const { bytesToHex, toBase58, toBase64, toHex, toBuffer, fromTokenToUnit, fromUnitToToken } = require('@ocap/util');
|
|
9
10
|
const { DEFAULT_TOKEN_DECIMAL } = require('@ocap/util/lib/constant');
|
|
10
11
|
const { formatFactoryState, preMintFromFactory: preMintFromFactoryCbor } = require('@ocap/asset/mint/client');
|
|
12
|
+
const { verifyTxInclusion } = require('./verify');
|
|
11
13
|
const {
|
|
12
14
|
toDelegateAddress,
|
|
13
15
|
toAssetAddress: toAssetAddressCbor,
|
|
@@ -51,11 +53,16 @@ const filteredTransactions = transactions.filter(x => !['DeclareTx'].includes(x)
|
|
|
51
53
|
* @param {GraphQLClient} client
|
|
52
54
|
* @param {object} [deps=defaultDeps]
|
|
53
55
|
*/
|
|
56
|
+
// Chain-side fee/gas config (txConfig, vaults) can change without a client restart;
|
|
57
|
+
// re-fetch it periodically instead of caching it for the lifetime of the process (#625).
|
|
58
|
+
const CONTEXT_TTL_MS = 30 * 1000;
|
|
59
|
+
|
|
54
60
|
const createExtensionMethods = (client, deps = defaultDeps) => {
|
|
55
61
|
Object.defineProperty(client, 'ctx', {
|
|
56
62
|
value: {
|
|
57
63
|
loading: false,
|
|
58
64
|
ready: false,
|
|
65
|
+
fetchedAt: 0,
|
|
59
66
|
callbacks: []
|
|
60
67
|
}
|
|
61
68
|
});
|
|
@@ -159,7 +166,7 @@ const createExtensionMethods = (client, deps = defaultDeps) => {
|
|
|
159
166
|
* @returns {object}
|
|
160
167
|
*/
|
|
161
168
|
client.getContext = async () => {
|
|
162
|
-
if (client.ctx.ready) {
|
|
169
|
+
if (client.ctx.ready && Date.now() - client.ctx.fetchedAt < CONTEXT_TTL_MS) {
|
|
163
170
|
return client.context;
|
|
164
171
|
}
|
|
165
172
|
|
|
@@ -227,8 +234,10 @@ const createExtensionMethods = (client, deps = defaultDeps) => {
|
|
|
227
234
|
getForgeState: forgeStateResult.getForgeState
|
|
228
235
|
};
|
|
229
236
|
} catch (err) {
|
|
230
|
-
client.ctx.loading =
|
|
231
|
-
client.ctx.callbacks
|
|
237
|
+
client.ctx.loading = false;
|
|
238
|
+
const callbacks = client.ctx.callbacks;
|
|
239
|
+
client.ctx.callbacks = [];
|
|
240
|
+
callbacks.forEach(cb => cb(err));
|
|
232
241
|
throw err;
|
|
233
242
|
}
|
|
234
243
|
|
|
@@ -242,9 +251,12 @@ const createExtensionMethods = (client, deps = defaultDeps) => {
|
|
|
242
251
|
};
|
|
243
252
|
|
|
244
253
|
client.ctx.ready = true;
|
|
254
|
+
client.ctx.fetchedAt = Date.now();
|
|
245
255
|
client.ctx.loading = false;
|
|
246
256
|
|
|
247
|
-
client.ctx.callbacks
|
|
257
|
+
const callbacks = client.ctx.callbacks;
|
|
258
|
+
client.ctx.callbacks = [];
|
|
259
|
+
callbacks.forEach(cb => cb());
|
|
248
260
|
|
|
249
261
|
debug('context ready');
|
|
250
262
|
|
|
@@ -267,6 +279,9 @@ const createExtensionMethods = (client, deps = defaultDeps) => {
|
|
|
267
279
|
}
|
|
268
280
|
|
|
269
281
|
const getTokenState = async address => {
|
|
282
|
+
if (typeof address !== 'string' || !/^(?:did:[a-z0-9]+:)?[a-zA-Z0-9_-]+$/.test(address)) {
|
|
283
|
+
throw new Error(`Invalid token address: ${address}`);
|
|
284
|
+
}
|
|
270
285
|
const {
|
|
271
286
|
getTokenState: { code, state }
|
|
272
287
|
} = await client.doRawQuery(`{
|
|
@@ -483,14 +498,44 @@ const createExtensionMethods = (client, deps = defaultDeps) => {
|
|
|
483
498
|
txSignFn.__tx__ = signMethod;
|
|
484
499
|
client[signMethod] = txSignFn;
|
|
485
500
|
|
|
486
|
-
|
|
501
|
+
const verifySignerPkBinding = entry => {
|
|
502
|
+
if (!entry || !entry.pk) return;
|
|
503
|
+
const matchesSigner = entry.signer && isFromPublicKey(entry.signer, entry.pk);
|
|
504
|
+
const matchesDelegator = entry.delegator && isFromPublicKey(entry.delegator, entry.pk);
|
|
505
|
+
if (!matchesSigner && !matchesDelegator) {
|
|
506
|
+
throw new Error('Invalid multisig signer public key');
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
|
|
487
510
|
// Generate transaction multi sign function
|
|
488
511
|
if (multiSignTxs.includes(x)) {
|
|
489
|
-
const txMultiSignFn = async ({ tx, wallet, delegator, data, encoding = '' }) => {
|
|
512
|
+
const txMultiSignFn = async ({ tx, wallet, delegator, data, extra: sigExtra = '', encoding = '' }) => {
|
|
490
513
|
if (typeof wallet.toAddress !== 'function') {
|
|
491
514
|
throw new Error('Multisig requires a valid wallet');
|
|
492
515
|
}
|
|
493
|
-
|
|
516
|
+
const existingSignatures = structuredClone(tx.signatures || tx.signaturesList || []);
|
|
517
|
+
delete tx.signaturesList;
|
|
518
|
+
for (let i = 0; i < existingSignatures.length; i++) {
|
|
519
|
+
const entry = existingSignatures[i] || {};
|
|
520
|
+
verifySignerPkBinding(entry);
|
|
521
|
+
if (entry.signature) {
|
|
522
|
+
if (!entry.pk) {
|
|
523
|
+
throw new Error('Invalid existing multisig signature: missing public key');
|
|
524
|
+
}
|
|
525
|
+
const priorTx = Object.assign({}, tx, {
|
|
526
|
+
signatures: [omit(entry, ['signature', 'extra'])].concat(existingSignatures.slice(i + 1))
|
|
527
|
+
});
|
|
528
|
+
const { buffer: priorBuffer } = await txEncodeFn({ tx: priorTx, wallet });
|
|
529
|
+
const entryDid = entry.delegator || entry.signer;
|
|
530
|
+
const entryType = entryDid ? toTypeInfo(entryDid) : undefined;
|
|
531
|
+
const verified = await fromPublicKey(entry.pk, entryType).verify(bytesToHex(priorBuffer), entry.signature, true, entry.extra);
|
|
532
|
+
if (!verified) {
|
|
533
|
+
throw new Error('Invalid existing multisig signature');
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
tx.signatures = existingSignatures;
|
|
494
539
|
if (delegator) {
|
|
495
540
|
tx.signatures.unshift({
|
|
496
541
|
pk: wallet.publicKey,
|
|
@@ -509,6 +554,9 @@ const createExtensionMethods = (client, deps = defaultDeps) => {
|
|
|
509
554
|
|
|
510
555
|
const { object, buffer } = await txEncodeFn({ tx, wallet });
|
|
511
556
|
object.signatures[0].signature = await wallet.sign(bytesToHex(buffer));
|
|
557
|
+
if (sigExtra) {
|
|
558
|
+
object.signatures[0].extra = typeof sigExtra === 'string' ? sigExtra : JSON.stringify(sigExtra);
|
|
559
|
+
}
|
|
512
560
|
return _formatEncodedTx(object, encoding);
|
|
513
561
|
};
|
|
514
562
|
const multiSignMethod = camelCase(`multi_sign_${x}`);
|
|
@@ -517,22 +565,58 @@ const createExtensionMethods = (client, deps = defaultDeps) => {
|
|
|
517
565
|
}
|
|
518
566
|
|
|
519
567
|
if (multiSignV2Txs.includes(x)) {
|
|
520
|
-
const txMultiSignFn = async ({ tx, wallet, encoding = '' }) => {
|
|
568
|
+
const txMultiSignFn = async ({ tx, wallet, delegator = '', extra: sigExtra = '', encoding = '' }) => {
|
|
521
569
|
if (typeof wallet.toAddress !== 'function') {
|
|
522
570
|
throw new Error('Multisig requires a valid wallet');
|
|
523
571
|
}
|
|
524
572
|
|
|
525
573
|
const signer = wallet.address;
|
|
526
574
|
const signatures = structuredClone(tx.signatures || tx.signaturesList || []);
|
|
527
|
-
|
|
575
|
+
delete tx.signaturesList;
|
|
576
|
+
const matchEntry = s => delegator ? s.signer === signer && (!s.delegator || s.delegator === delegator) || s.signer === delegator && (!s.delegator || s.delegator === signer) : s.signer === signer || s.delegator === signer;
|
|
577
|
+
const item = signatures.find(s => !s.signature && matchEntry(s)) || signatures.find(matchEntry);
|
|
528
578
|
if (!item) {
|
|
529
579
|
throw new Error('Signer not in the list');
|
|
530
580
|
}
|
|
581
|
+
const hasExistingSignature = signatures.some(s => s !== item && s && s.signature);
|
|
582
|
+
if (!hasExistingSignature) {
|
|
583
|
+
if (delegator && item.signer === delegator && !item.delegator) {
|
|
584
|
+
item.delegator = signer;
|
|
585
|
+
}
|
|
586
|
+
if (!item.pk && (item.delegator === signer || !item.delegator && item.signer === signer)) {
|
|
587
|
+
item.pk = wallet.publicKey;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
531
590
|
|
|
532
|
-
|
|
591
|
+
for (const entry of signatures) {
|
|
592
|
+
if (!entry || !entry.pk) {
|
|
593
|
+
throw new Error('Multisig requires all signature entries to specify pk before signing');
|
|
594
|
+
}
|
|
595
|
+
verifySignerPkBinding(entry);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
tx.signatures = signatures.map(s => omit(s, ['signature', 'extra']));
|
|
533
599
|
const { object, buffer } = await txEncodeFn({ tx, wallet });
|
|
534
600
|
|
|
601
|
+
for (const rawEntry of signatures) {
|
|
602
|
+
const entry = rawEntry || {};
|
|
603
|
+
if (entry.signature && rawEntry !== item) {
|
|
604
|
+
if (!entry.pk) {
|
|
605
|
+
throw new Error('Invalid existing multisig signature: missing public key');
|
|
606
|
+
}
|
|
607
|
+
const entryDid = entry.delegator || entry.signer;
|
|
608
|
+
const entryType = entryDid ? toTypeInfo(entryDid) : undefined;
|
|
609
|
+
const verified = await fromPublicKey(entry.pk, entryType).verify(bytesToHex(buffer), entry.signature, true, entry.extra);
|
|
610
|
+
if (!verified) {
|
|
611
|
+
throw new Error('Invalid existing multisig signature');
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
535
616
|
item.signature = await wallet.sign(bytesToHex(buffer));
|
|
617
|
+
if (sigExtra) {
|
|
618
|
+
item.extra = typeof sigExtra === 'string' ? sigExtra : JSON.stringify(sigExtra);
|
|
619
|
+
}
|
|
536
620
|
object.signatures = signatures;
|
|
537
621
|
return _formatEncodedTx(object, encoding);
|
|
538
622
|
};
|
|
@@ -1451,6 +1535,133 @@ const createExtensionMethods = (client, deps = defaultDeps) => {
|
|
|
1451
1535
|
wallet
|
|
1452
1536
|
}, extra);
|
|
1453
1537
|
};
|
|
1538
|
+
|
|
1539
|
+
/**
|
|
1540
|
+
* Query the latest (or height-specific) Ledger Checkpoint.
|
|
1541
|
+
*
|
|
1542
|
+
* @memberof GraphQLClient
|
|
1543
|
+
* @function
|
|
1544
|
+
* @name GraphQLClient#getLatestCheckpoint
|
|
1545
|
+
* @param {object} [params]
|
|
1546
|
+
* @param {number} [params.height]
|
|
1547
|
+
* @returns {Promise<object>}
|
|
1548
|
+
*/
|
|
1549
|
+
client.getLatestCheckpoint = async ({ height } = {}) => {
|
|
1550
|
+
const args = height !== undefined && height !== null ? `(height: ${Number(height)})` : '';
|
|
1551
|
+
const res = await client.doRawQuery(`{
|
|
1552
|
+
getLatestCheckpoint${args} {
|
|
1553
|
+
code
|
|
1554
|
+
checkpoint {
|
|
1555
|
+
height
|
|
1556
|
+
sequence
|
|
1557
|
+
prevSequence
|
|
1558
|
+
txRoot
|
|
1559
|
+
txCount
|
|
1560
|
+
stateRoot
|
|
1561
|
+
timestamp
|
|
1562
|
+
prevCheckpoint
|
|
1563
|
+
proposer
|
|
1564
|
+
hash
|
|
1565
|
+
signature
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
}`);
|
|
1569
|
+
return get(res, 'getLatestCheckpoint') || { code: 'ERROR', checkpoint: null };
|
|
1570
|
+
};
|
|
1571
|
+
|
|
1572
|
+
/**
|
|
1573
|
+
* Query a Ledger entry by transaction hash or sequence number.
|
|
1574
|
+
*
|
|
1575
|
+
* @memberof GraphQLClient
|
|
1576
|
+
* @function
|
|
1577
|
+
* @name GraphQLClient#getLedgerEntry
|
|
1578
|
+
* @param {object} [params]
|
|
1579
|
+
* @param {string} [params.hash]
|
|
1580
|
+
* @param {number} [params.sequence]
|
|
1581
|
+
* @returns {Promise<object>}
|
|
1582
|
+
*/
|
|
1583
|
+
client.getLedgerEntry = async ({ hash, sequence } = {}) => {
|
|
1584
|
+
const argParts = [];
|
|
1585
|
+
if (hash !== undefined && hash !== null) {
|
|
1586
|
+
argParts.push(`hash: ${JSON.stringify(String(hash))}`);
|
|
1587
|
+
}
|
|
1588
|
+
if (sequence !== undefined && sequence !== null) {
|
|
1589
|
+
argParts.push(`sequence: ${Number(sequence)}`);
|
|
1590
|
+
}
|
|
1591
|
+
const args = argParts.length > 0 ? `(${argParts.join(', ')})` : '';
|
|
1592
|
+
const res = await client.doRawQuery(`{
|
|
1593
|
+
getLedgerEntry${args} {
|
|
1594
|
+
code
|
|
1595
|
+
entry {
|
|
1596
|
+
sequence
|
|
1597
|
+
hash
|
|
1598
|
+
from
|
|
1599
|
+
nonce
|
|
1600
|
+
pk
|
|
1601
|
+
signature
|
|
1602
|
+
txRaw
|
|
1603
|
+
tx
|
|
1604
|
+
txType
|
|
1605
|
+
timestamp
|
|
1606
|
+
chainId
|
|
1607
|
+
entryHash
|
|
1608
|
+
txExtra
|
|
1609
|
+
stateCommitHash
|
|
1610
|
+
finalized
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
}`);
|
|
1614
|
+
return get(res, 'getLedgerEntry') || { code: 'ERROR', entry: null };
|
|
1615
|
+
};
|
|
1616
|
+
|
|
1617
|
+
/**
|
|
1618
|
+
* Query a Ledger Merkle inclusion proof and its covering Checkpoint by sequence number.
|
|
1619
|
+
*
|
|
1620
|
+
* @memberof GraphQLClient
|
|
1621
|
+
* @function
|
|
1622
|
+
* @name GraphQLClient#getMerkleProof
|
|
1623
|
+
* @param {object} params
|
|
1624
|
+
* @param {number} params.sequence
|
|
1625
|
+
* @returns {Promise<object>}
|
|
1626
|
+
*/
|
|
1627
|
+
client.getMerkleProof = async ({ sequence } = {}) => {
|
|
1628
|
+
const res = await client.doRawQuery(`{
|
|
1629
|
+
getMerkleProof(sequence: ${Number(sequence)}) {
|
|
1630
|
+
code
|
|
1631
|
+
proof {
|
|
1632
|
+
index
|
|
1633
|
+
leaf
|
|
1634
|
+
proof
|
|
1635
|
+
root
|
|
1636
|
+
}
|
|
1637
|
+
checkpoint {
|
|
1638
|
+
height
|
|
1639
|
+
sequence
|
|
1640
|
+
prevSequence
|
|
1641
|
+
txRoot
|
|
1642
|
+
txCount
|
|
1643
|
+
stateRoot
|
|
1644
|
+
timestamp
|
|
1645
|
+
prevCheckpoint
|
|
1646
|
+
proposer
|
|
1647
|
+
hash
|
|
1648
|
+
signature
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
}`);
|
|
1652
|
+
return get(res, 'getMerkleProof') || { code: 'ERROR', proof: null, checkpoint: null };
|
|
1653
|
+
};
|
|
1654
|
+
|
|
1655
|
+
/**
|
|
1656
|
+
* Cryptographically verify transaction inclusion in a signed Checkpoint (< 1ms, zero network I/O).
|
|
1657
|
+
*
|
|
1658
|
+
* @memberof GraphQLClient
|
|
1659
|
+
* @function
|
|
1660
|
+
* @name GraphQLClient#verifyTxInclusion
|
|
1661
|
+
* @param {object} params
|
|
1662
|
+
* @returns {Promise<{ verified: boolean, reason?: string }>}
|
|
1663
|
+
*/
|
|
1664
|
+
client.verifyTxInclusion = params => verifyTxInclusion(params);
|
|
1454
1665
|
};
|
|
1455
1666
|
|
|
1456
1667
|
module.exports = { createExtensionMethods, defaultDeps };
|