@ottochain/sdk 1.2.0 → 1.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/LICENSE +190 -0
- package/dist/cjs/index.js +34 -15
- package/dist/cjs/ottochain/index.js +20 -1
- package/dist/cjs/ottochain/metagraph-client.js +7 -8
- package/dist/cjs/ottochain/snapshot.js +3 -3
- package/dist/cjs/{metakit → ottochain}/transaction.js +4 -5
- package/dist/cjs/verify.js +17 -0
- package/dist/esm/apps/contracts/index.js +28 -10
- package/dist/esm/apps/corporate/index.js +79 -24
- package/dist/esm/apps/governance/index.js +85 -36
- package/dist/esm/apps/identity/constants.js +27 -22
- package/dist/esm/apps/identity/index.js +35 -7
- package/dist/esm/apps/index.js +32 -6
- package/dist/esm/apps/markets/index.js +27 -6
- package/dist/esm/apps/oracles/index.js +27 -7
- package/dist/esm/errors.js +19 -9
- package/dist/esm/generated/google/protobuf/struct.js +39 -33
- package/dist/esm/generated/google/protobuf/timestamp.js +9 -6
- package/dist/esm/generated/index.js +134 -10
- package/dist/esm/generated/ottochain/apps/contracts/v1/contract.js +54 -48
- package/dist/esm/generated/ottochain/apps/corporate/v1/corporate.js +357 -335
- package/dist/esm/generated/ottochain/apps/governance/v1/governance.js +299 -284
- package/dist/esm/generated/ottochain/apps/identity/v1/agent.js +47 -38
- package/dist/esm/generated/ottochain/apps/identity/v1/attestation.js +50 -44
- package/dist/esm/generated/ottochain/apps/markets/v1/market.js +86 -77
- package/dist/esm/generated/ottochain/apps/oracles/v1/oracle.js +72 -66
- package/dist/esm/generated/ottochain/v1/common.js +4 -1
- package/dist/esm/generated/ottochain/v1/fiber.js +96 -90
- package/dist/esm/generated/ottochain/v1/messages.js +82 -79
- package/dist/esm/generated/ottochain/v1/records.js +140 -137
- package/dist/esm/index.js +86 -20
- package/dist/esm/{metakit → ottochain}/drop-nulls.js +5 -1
- package/dist/esm/ottochain/index.js +56 -3
- package/dist/esm/ottochain/metagraph-client.js +16 -13
- package/dist/esm/{metakit → ottochain}/normalize.js +11 -4
- package/dist/esm/ottochain/snapshot.js +20 -10
- package/dist/esm/{metakit → ottochain}/transaction.js +25 -14
- package/dist/esm/ottochain/types.js +2 -1
- package/dist/esm/types.js +7 -2
- package/dist/esm/validation.js +76 -65
- package/dist/esm/verify.js +17 -0
- package/dist/types/index.d.ts +14 -7
- package/dist/types/ottochain/index.d.ts +4 -0
- package/dist/types/ottochain/metagraph-client.d.ts +1 -1
- package/dist/types/{metakit → ottochain}/transaction.d.ts +1 -1
- package/dist/types/validation.d.ts +8 -8
- package/dist/types/verify.d.ts +9 -0
- package/package.json +5 -3
- package/dist/cjs/metakit/binary.js +0 -58
- package/dist/cjs/metakit/canonicalize.js +0 -40
- package/dist/cjs/metakit/codec.js +0 -45
- package/dist/cjs/metakit/currency-transaction.js +0 -319
- package/dist/cjs/metakit/currency-types.js +0 -13
- package/dist/cjs/metakit/hash.js +0 -84
- package/dist/cjs/metakit/index.js +0 -86
- package/dist/cjs/metakit/network/client.js +0 -78
- package/dist/cjs/metakit/network/currency-l1-client.js +0 -101
- package/dist/cjs/metakit/network/data-l1-client.js +0 -76
- package/dist/cjs/metakit/network/index.js +0 -16
- package/dist/cjs/metakit/network/types.js +0 -20
- package/dist/cjs/metakit/sign.js +0 -120
- package/dist/cjs/metakit/signed-object.js +0 -100
- package/dist/cjs/metakit/types.js +0 -14
- package/dist/cjs/metakit/verify.js +0 -217
- package/dist/cjs/metakit/wallet.js +0 -127
- package/dist/esm/metakit/binary.js +0 -53
- package/dist/esm/metakit/canonicalize.js +0 -33
- package/dist/esm/metakit/codec.js +0 -38
- package/dist/esm/metakit/currency-transaction.js +0 -306
- package/dist/esm/metakit/currency-types.js +0 -10
- package/dist/esm/metakit/hash.js +0 -77
- package/dist/esm/metakit/index.js +0 -33
- package/dist/esm/metakit/network/client.js +0 -74
- package/dist/esm/metakit/network/currency-l1-client.js +0 -97
- package/dist/esm/metakit/network/data-l1-client.js +0 -72
- package/dist/esm/metakit/network/index.js +0 -9
- package/dist/esm/metakit/network/types.js +0 -16
- package/dist/esm/metakit/sign.js +0 -114
- package/dist/esm/metakit/signed-object.js +0 -94
- package/dist/esm/metakit/types.js +0 -11
- package/dist/esm/metakit/verify.js +0 -210
- package/dist/esm/metakit/wallet.js +0 -117
- package/dist/types/metakit/binary.d.ts +0 -38
- package/dist/types/metakit/canonicalize.d.ts +0 -26
- package/dist/types/metakit/codec.d.ts +0 -16
- package/dist/types/metakit/currency-transaction.d.ts +0 -157
- package/dist/types/metakit/currency-types.d.ts +0 -55
- package/dist/types/metakit/hash.d.ts +0 -50
- package/dist/types/metakit/index.d.ts +0 -26
- package/dist/types/metakit/network/client.d.ts +0 -23
- package/dist/types/metakit/network/currency-l1-client.d.ts +0 -71
- package/dist/types/metakit/network/data-l1-client.d.ts +0 -57
- package/dist/types/metakit/network/index.d.ts +0 -10
- package/dist/types/metakit/network/types.d.ts +0 -74
- package/dist/types/metakit/sign.d.ts +0 -65
- package/dist/types/metakit/signed-object.d.ts +0 -66
- package/dist/types/metakit/types.d.ts +0 -67
- package/dist/types/metakit/verify.d.ts +0 -55
- package/dist/types/metakit/wallet.d.ts +0 -70
- /package/dist/cjs/{metakit → ottochain}/drop-nulls.js +0 -0
- /package/dist/cjs/{metakit → ottochain}/normalize.js +0 -0
- /package/dist/types/{metakit → ottochain}/drop-nulls.d.ts +0 -0
- /package/dist/types/{metakit → ottochain}/normalize.d.ts +0 -0
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Codec Utilities
|
|
4
|
-
*
|
|
5
|
-
* Encoding/decoding utilities for the Constellation signature protocol.
|
|
6
|
-
* Re-exports from binary.ts for backwards compatibility and provides additional utilities.
|
|
7
|
-
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.decodeDataUpdate = exports.CONSTELLATION_PREFIX = exports.encodeDataUpdate = exports.toBytes = void 0;
|
|
10
|
-
var binary_js_1 = require("./binary.js");
|
|
11
|
-
Object.defineProperty(exports, "toBytes", { enumerable: true, get: function () { return binary_js_1.toBytes; } });
|
|
12
|
-
Object.defineProperty(exports, "encodeDataUpdate", { enumerable: true, get: function () { return binary_js_1.encodeDataUpdate; } });
|
|
13
|
-
var types_js_1 = require("./types.js");
|
|
14
|
-
Object.defineProperty(exports, "CONSTELLATION_PREFIX", { enumerable: true, get: function () { return types_js_1.CONSTELLATION_PREFIX; } });
|
|
15
|
-
/**
|
|
16
|
-
* Decode a DataUpdate encoded message back to its original JSON
|
|
17
|
-
*
|
|
18
|
-
* @param bytes - DataUpdate encoded bytes
|
|
19
|
-
* @returns Decoded JSON object
|
|
20
|
-
* @throws Error if bytes are not valid DataUpdate encoding
|
|
21
|
-
*/
|
|
22
|
-
function decodeDataUpdate(bytes) {
|
|
23
|
-
const text = new TextDecoder().decode(bytes);
|
|
24
|
-
// Validate prefix
|
|
25
|
-
if (!text.startsWith('\x19Constellation Signed Data:\n')) {
|
|
26
|
-
throw new Error('Invalid DataUpdate encoding: missing Constellation prefix');
|
|
27
|
-
}
|
|
28
|
-
// Parse the format: \x19Constellation Signed Data:\n{length}\n{base64}
|
|
29
|
-
const withoutPrefix = text.slice('\x19Constellation Signed Data:\n'.length);
|
|
30
|
-
const newlineIndex = withoutPrefix.indexOf('\n');
|
|
31
|
-
if (newlineIndex === -1) {
|
|
32
|
-
throw new Error('Invalid DataUpdate encoding: missing length delimiter');
|
|
33
|
-
}
|
|
34
|
-
const lengthStr = withoutPrefix.slice(0, newlineIndex);
|
|
35
|
-
const base64Data = withoutPrefix.slice(newlineIndex + 1);
|
|
36
|
-
const expectedLength = parseInt(lengthStr, 10);
|
|
37
|
-
if (isNaN(expectedLength) || base64Data.length !== expectedLength) {
|
|
38
|
-
throw new Error('Invalid DataUpdate encoding: length mismatch');
|
|
39
|
-
}
|
|
40
|
-
// Decode base64 to UTF-8 JSON
|
|
41
|
-
const jsonBytes = Buffer.from(base64Data, 'base64');
|
|
42
|
-
const jsonString = new TextDecoder().decode(jsonBytes);
|
|
43
|
-
return JSON.parse(jsonString);
|
|
44
|
-
}
|
|
45
|
-
exports.decodeDataUpdate = decodeDataUpdate;
|
|
@@ -1,319 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Currency transaction operations for metagraph token transfers
|
|
4
|
-
*
|
|
5
|
-
* @packageDocumentation
|
|
6
|
-
*/
|
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.getTransactionReference = exports.hashCurrencyTransaction = exports.encodeCurrencyTransaction = exports.verifyCurrencyTransaction = exports.signCurrencyTransaction = exports.createCurrencyTransactionBatch = exports.createCurrencyTransaction = exports.isValidDagAddress = exports.unitsToToken = exports.tokenToUnits = void 0;
|
|
9
|
-
const dag4_keystore_1 = require("@stardust-collective/dag4-keystore");
|
|
10
|
-
const currency_types_js_1 = require("./currency-types.js");
|
|
11
|
-
const wallet_js_1 = require("./wallet.js");
|
|
12
|
-
const verify_js_1 = require("./verify.js");
|
|
13
|
-
/**
|
|
14
|
-
* Convert token amount to smallest units
|
|
15
|
-
*
|
|
16
|
-
* @param amount - Amount in token units (e.g., 100.5)
|
|
17
|
-
* @returns Amount in smallest units (1e-8)
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* ```typescript
|
|
21
|
-
* const units = tokenToUnits(100.5); // 10050000000
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
function tokenToUnits(amount) {
|
|
25
|
-
return Math.floor(amount * 1e8);
|
|
26
|
-
}
|
|
27
|
-
exports.tokenToUnits = tokenToUnits;
|
|
28
|
-
/**
|
|
29
|
-
* Convert smallest units to token amount
|
|
30
|
-
*
|
|
31
|
-
* @param units - Amount in smallest units
|
|
32
|
-
* @returns Amount in token units
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```typescript
|
|
36
|
-
* const tokens = unitsToToken(10050000000); // 100.5
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
function unitsToToken(units) {
|
|
40
|
-
return units * currency_types_js_1.TOKEN_DECIMALS;
|
|
41
|
-
}
|
|
42
|
-
exports.unitsToToken = unitsToToken;
|
|
43
|
-
/**
|
|
44
|
-
* Validate DAG address format
|
|
45
|
-
*
|
|
46
|
-
* @param address - DAG address to validate
|
|
47
|
-
* @returns True if address is valid
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* ```typescript
|
|
51
|
-
* const valid = isValidDagAddress('DAG...');
|
|
52
|
-
* ```
|
|
53
|
-
*/
|
|
54
|
-
function isValidDagAddress(address) {
|
|
55
|
-
return dag4_keystore_1.keyStore.validateDagAddress(address);
|
|
56
|
-
}
|
|
57
|
-
exports.isValidDagAddress = isValidDagAddress;
|
|
58
|
-
/**
|
|
59
|
-
* Create a metagraph token transaction
|
|
60
|
-
*
|
|
61
|
-
* @param params - Transfer parameters
|
|
62
|
-
* @param privateKey - Private key to sign with (hex string)
|
|
63
|
-
* @param lastRef - Reference to last accepted transaction
|
|
64
|
-
* @returns Signed currency transaction
|
|
65
|
-
*
|
|
66
|
-
* @throws If addresses are invalid or amount is too small
|
|
67
|
-
*
|
|
68
|
-
* @example
|
|
69
|
-
* ```typescript
|
|
70
|
-
* const tx = await createCurrencyTransaction(
|
|
71
|
-
* { destination: 'DAG...', amount: 100.5, fee: 0 },
|
|
72
|
-
* privateKey,
|
|
73
|
-
* { hash: 'abc123...', ordinal: 5 }
|
|
74
|
-
* );
|
|
75
|
-
* ```
|
|
76
|
-
*/
|
|
77
|
-
async function createCurrencyTransaction(params, privateKey, lastRef) {
|
|
78
|
-
// Get source address from private key
|
|
79
|
-
const publicKey = dag4_keystore_1.keyStore.getPublicKeyFromPrivate(privateKey);
|
|
80
|
-
const source = (0, wallet_js_1.getAddress)(publicKey);
|
|
81
|
-
// Validate addresses
|
|
82
|
-
if (!isValidDagAddress(source)) {
|
|
83
|
-
throw new Error('Invalid source address');
|
|
84
|
-
}
|
|
85
|
-
if (!isValidDagAddress(params.destination)) {
|
|
86
|
-
throw new Error('Invalid destination address');
|
|
87
|
-
}
|
|
88
|
-
if (source === params.destination) {
|
|
89
|
-
throw new Error('Source and destination addresses cannot be the same');
|
|
90
|
-
}
|
|
91
|
-
// Convert amounts to smallest units
|
|
92
|
-
const amount = tokenToUnits(params.amount);
|
|
93
|
-
const fee = tokenToUnits(params.fee ?? 0);
|
|
94
|
-
// Validate amounts
|
|
95
|
-
if (amount < 1) {
|
|
96
|
-
throw new Error('Transfer amount must be greater than 1e-8');
|
|
97
|
-
}
|
|
98
|
-
if (fee < 0) {
|
|
99
|
-
throw new Error('Fee must be greater than or equal to zero');
|
|
100
|
-
}
|
|
101
|
-
// Use dag4.js TransactionV2 to create and encode the transaction
|
|
102
|
-
const txProps = {
|
|
103
|
-
fromAddress: source,
|
|
104
|
-
toAddress: params.destination,
|
|
105
|
-
amount,
|
|
106
|
-
fee,
|
|
107
|
-
lastTxRef: lastRef,
|
|
108
|
-
};
|
|
109
|
-
const tx = new dag4_keystore_1.TransactionV2(txProps);
|
|
110
|
-
// Get encoded transaction for hashing
|
|
111
|
-
const encodedTx = tx.getEncoded();
|
|
112
|
-
// Kryo serialize - v2 uses setReferences = false (matching dag4.js behavior)
|
|
113
|
-
const serializedTx = dag4_keystore_1.txEncode.kryoSerialize(encodedTx, false);
|
|
114
|
-
// Hash the serialized transaction
|
|
115
|
-
const hash = dag4_keystore_1.keyStore.sha256(Buffer.from(serializedTx, 'hex'));
|
|
116
|
-
// Sign the hash
|
|
117
|
-
const signature = await dag4_keystore_1.keyStore.sign(privateKey, hash);
|
|
118
|
-
// Get uncompressed public key
|
|
119
|
-
const uncompressedPublicKey = publicKey.length === 128 ? '04' + publicKey : publicKey;
|
|
120
|
-
// Verify signature
|
|
121
|
-
const success = dag4_keystore_1.keyStore.verify(uncompressedPublicKey, hash, signature);
|
|
122
|
-
if (!success) {
|
|
123
|
-
throw new Error('Sign-Verify failed');
|
|
124
|
-
}
|
|
125
|
-
// Add signature proof (remove '04' prefix from public key)
|
|
126
|
-
const proof = {
|
|
127
|
-
id: uncompressedPublicKey.substring(2),
|
|
128
|
-
signature,
|
|
129
|
-
};
|
|
130
|
-
tx.addSignature(proof);
|
|
131
|
-
return tx.getPostTransaction();
|
|
132
|
-
}
|
|
133
|
-
exports.createCurrencyTransaction = createCurrencyTransaction;
|
|
134
|
-
/**
|
|
135
|
-
* Create multiple metagraph token transactions (batch)
|
|
136
|
-
*
|
|
137
|
-
* @param transfers - Array of transfer parameters
|
|
138
|
-
* @param privateKey - Private key to sign with
|
|
139
|
-
* @param lastRef - Reference to last accepted transaction
|
|
140
|
-
* @returns Array of signed currency transactions
|
|
141
|
-
*
|
|
142
|
-
* @throws If any address is invalid or amount is too small
|
|
143
|
-
*
|
|
144
|
-
* @example
|
|
145
|
-
* ```typescript
|
|
146
|
-
* const txns = await createCurrencyTransactionBatch(
|
|
147
|
-
* [
|
|
148
|
-
* { destination: 'DAG...1', amount: 10 },
|
|
149
|
-
* { destination: 'DAG...2', amount: 20 },
|
|
150
|
-
* ],
|
|
151
|
-
* privateKey,
|
|
152
|
-
* { hash: 'abc123...', ordinal: 5 }
|
|
153
|
-
* );
|
|
154
|
-
* ```
|
|
155
|
-
*/
|
|
156
|
-
async function createCurrencyTransactionBatch(transfers, privateKey, lastRef) {
|
|
157
|
-
const transactions = [];
|
|
158
|
-
let currentRef = { ...lastRef };
|
|
159
|
-
for (const transfer of transfers) {
|
|
160
|
-
const tx = await createCurrencyTransaction(transfer, privateKey, currentRef);
|
|
161
|
-
// Calculate hash for next transaction's parent reference
|
|
162
|
-
const hash = await hashCurrencyTransaction(tx);
|
|
163
|
-
// Update reference for next transaction
|
|
164
|
-
currentRef = {
|
|
165
|
-
hash: hash.value,
|
|
166
|
-
ordinal: currentRef.ordinal + 1,
|
|
167
|
-
};
|
|
168
|
-
transactions.push(tx);
|
|
169
|
-
}
|
|
170
|
-
return transactions;
|
|
171
|
-
}
|
|
172
|
-
exports.createCurrencyTransactionBatch = createCurrencyTransactionBatch;
|
|
173
|
-
/**
|
|
174
|
-
* Add a signature to an existing currency transaction (for multi-sig)
|
|
175
|
-
*
|
|
176
|
-
* @param transaction - Transaction to sign
|
|
177
|
-
* @param privateKey - Private key to sign with
|
|
178
|
-
* @returns Transaction with additional signature
|
|
179
|
-
*
|
|
180
|
-
* @throws If sign-verify fails
|
|
181
|
-
*
|
|
182
|
-
* @example
|
|
183
|
-
* ```typescript
|
|
184
|
-
* const signedTx = await signCurrencyTransaction(tx, privateKey2);
|
|
185
|
-
* ```
|
|
186
|
-
*/
|
|
187
|
-
async function signCurrencyTransaction(transaction, privateKey) {
|
|
188
|
-
// Reconstruct TransactionV2 from PostTransaction
|
|
189
|
-
const tx = dag4_keystore_1.TransactionV2.fromPostTransaction(transaction);
|
|
190
|
-
// Restore existing proofs (fromPostTransaction doesn't copy them)
|
|
191
|
-
for (const existingProof of transaction.proofs) {
|
|
192
|
-
tx.addSignature(existingProof);
|
|
193
|
-
}
|
|
194
|
-
// Get encoded transaction
|
|
195
|
-
const encodedTx = tx.getEncoded();
|
|
196
|
-
// Kryo serialize - v2 uses setReferences = false (matching dag4.js behavior)
|
|
197
|
-
const serializedTx = dag4_keystore_1.txEncode.kryoSerialize(encodedTx, false);
|
|
198
|
-
const hash = dag4_keystore_1.keyStore.sha256(Buffer.from(serializedTx, 'hex'));
|
|
199
|
-
// Sign the hash
|
|
200
|
-
const publicKey = dag4_keystore_1.keyStore.getPublicKeyFromPrivate(privateKey);
|
|
201
|
-
const signature = await dag4_keystore_1.keyStore.sign(privateKey, hash);
|
|
202
|
-
// Verify signature
|
|
203
|
-
const uncompressedPublicKey = publicKey.length === 128 ? '04' + publicKey : publicKey;
|
|
204
|
-
const success = dag4_keystore_1.keyStore.verify(uncompressedPublicKey, hash, signature);
|
|
205
|
-
if (!success) {
|
|
206
|
-
throw new Error('Sign-Verify failed');
|
|
207
|
-
}
|
|
208
|
-
// Add new proof
|
|
209
|
-
const proof = {
|
|
210
|
-
id: uncompressedPublicKey.substring(2),
|
|
211
|
-
signature,
|
|
212
|
-
};
|
|
213
|
-
tx.addSignature(proof);
|
|
214
|
-
return tx.getPostTransaction();
|
|
215
|
-
}
|
|
216
|
-
exports.signCurrencyTransaction = signCurrencyTransaction;
|
|
217
|
-
/**
|
|
218
|
-
* Verify all signatures on a currency transaction
|
|
219
|
-
*
|
|
220
|
-
* @param transaction - Transaction to verify
|
|
221
|
-
* @returns Verification result with valid/invalid proofs
|
|
222
|
-
*
|
|
223
|
-
* @example
|
|
224
|
-
* ```typescript
|
|
225
|
-
* const result = await verifyCurrencyTransaction(tx);
|
|
226
|
-
* console.log('Valid:', result.isValid);
|
|
227
|
-
* ```
|
|
228
|
-
*/
|
|
229
|
-
async function verifyCurrencyTransaction(transaction) {
|
|
230
|
-
// Reconstruct TransactionV2 to get encoded form
|
|
231
|
-
const tx = dag4_keystore_1.TransactionV2.fromPostTransaction(transaction);
|
|
232
|
-
// Get hash
|
|
233
|
-
const encodedTx = tx.getEncoded();
|
|
234
|
-
// Kryo serialize - v2 uses setReferences = false (matching dag4.js behavior)
|
|
235
|
-
const serializedTx = dag4_keystore_1.txEncode.kryoSerialize(encodedTx, false);
|
|
236
|
-
const hash = dag4_keystore_1.keyStore.sha256(Buffer.from(serializedTx, 'hex'));
|
|
237
|
-
const validProofs = [];
|
|
238
|
-
const invalidProofs = [];
|
|
239
|
-
// Verify each proof
|
|
240
|
-
for (const proof of transaction.proofs) {
|
|
241
|
-
const publicKey = '04' + proof.id; // Add back the '04' prefix
|
|
242
|
-
// Normalize signature to low-S form for BIP 62/146 compatibility
|
|
243
|
-
const normalizedSignature = (0, verify_js_1.normalizeSignatureToLowS)(proof.signature);
|
|
244
|
-
const isValid = dag4_keystore_1.keyStore.verify(publicKey, hash, normalizedSignature);
|
|
245
|
-
if (isValid) {
|
|
246
|
-
validProofs.push(proof);
|
|
247
|
-
}
|
|
248
|
-
else {
|
|
249
|
-
invalidProofs.push(proof);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
return {
|
|
253
|
-
isValid: invalidProofs.length === 0 && validProofs.length > 0,
|
|
254
|
-
validProofs,
|
|
255
|
-
invalidProofs,
|
|
256
|
-
};
|
|
257
|
-
}
|
|
258
|
-
exports.verifyCurrencyTransaction = verifyCurrencyTransaction;
|
|
259
|
-
/**
|
|
260
|
-
* Encode a currency transaction for hashing
|
|
261
|
-
*
|
|
262
|
-
* @param transaction - Transaction to encode
|
|
263
|
-
* @returns Hex-encoded string
|
|
264
|
-
*
|
|
265
|
-
* @example
|
|
266
|
-
* ```typescript
|
|
267
|
-
* const encoded = encodeCurrencyTransaction(tx);
|
|
268
|
-
* ```
|
|
269
|
-
*/
|
|
270
|
-
function encodeCurrencyTransaction(transaction) {
|
|
271
|
-
const tx = dag4_keystore_1.TransactionV2.fromPostTransaction(transaction);
|
|
272
|
-
return tx.getEncoded();
|
|
273
|
-
}
|
|
274
|
-
exports.encodeCurrencyTransaction = encodeCurrencyTransaction;
|
|
275
|
-
/**
|
|
276
|
-
* Hash a currency transaction
|
|
277
|
-
*
|
|
278
|
-
* @param transaction - Transaction to hash
|
|
279
|
-
* @returns Hash object with value and bytes
|
|
280
|
-
*
|
|
281
|
-
* @example
|
|
282
|
-
* ```typescript
|
|
283
|
-
* const hash = await hashCurrencyTransaction(tx);
|
|
284
|
-
* console.log('Hash:', hash.value);
|
|
285
|
-
* ```
|
|
286
|
-
*/
|
|
287
|
-
async function hashCurrencyTransaction(transaction) {
|
|
288
|
-
const encoded = encodeCurrencyTransaction(transaction);
|
|
289
|
-
// Kryo serialize - v2 uses setReferences = false (matching dag4.js behavior)
|
|
290
|
-
const serialized = dag4_keystore_1.txEncode.kryoSerialize(encoded, false);
|
|
291
|
-
const hash = dag4_keystore_1.keyStore.sha256(Buffer.from(serialized, 'hex'));
|
|
292
|
-
return {
|
|
293
|
-
value: hash,
|
|
294
|
-
bytes: Buffer.from(hash, 'hex'),
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
exports.hashCurrencyTransaction = hashCurrencyTransaction;
|
|
298
|
-
/**
|
|
299
|
-
* Get transaction reference from a currency transaction
|
|
300
|
-
* Useful for chaining transactions
|
|
301
|
-
*
|
|
302
|
-
* @param transaction - Transaction to extract reference from
|
|
303
|
-
* @param ordinal - Ordinal number for this transaction
|
|
304
|
-
* @returns Transaction reference
|
|
305
|
-
*
|
|
306
|
-
* @example
|
|
307
|
-
* ```typescript
|
|
308
|
-
* const ref = await getTransactionReference(tx, 6);
|
|
309
|
-
* // Use ref as lastRef for next transaction
|
|
310
|
-
* ```
|
|
311
|
-
*/
|
|
312
|
-
async function getTransactionReference(transaction, ordinal) {
|
|
313
|
-
const hash = await hashCurrencyTransaction(transaction);
|
|
314
|
-
return {
|
|
315
|
-
hash: hash.value,
|
|
316
|
-
ordinal,
|
|
317
|
-
};
|
|
318
|
-
}
|
|
319
|
-
exports.getTransactionReference = getTransactionReference;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Currency transaction types for metagraph token transfers
|
|
4
|
-
*
|
|
5
|
-
* @packageDocumentation
|
|
6
|
-
*/
|
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.TOKEN_DECIMALS = void 0;
|
|
9
|
-
/**
|
|
10
|
-
* Token decimals constant (1e-8)
|
|
11
|
-
* Same as DAG_DECIMALS from dag4.js
|
|
12
|
-
*/
|
|
13
|
-
exports.TOKEN_DECIMALS = 1e-8;
|
package/dist/cjs/metakit/hash.js
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Hashing Utilities
|
|
4
|
-
*
|
|
5
|
-
* SHA-256 and SHA-512 hashing for the Constellation signature protocol.
|
|
6
|
-
*/
|
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.hashData = exports.computeDigest = exports.hashBytes = exports.hash = void 0;
|
|
9
|
-
const js_sha256_1 = require("js-sha256");
|
|
10
|
-
const js_sha512_1 = require("js-sha512");
|
|
11
|
-
const binary_js_1 = require("./binary.js");
|
|
12
|
-
/**
|
|
13
|
-
* Compute SHA-256 hash of canonical JSON data
|
|
14
|
-
*
|
|
15
|
-
* @param data - Any JSON-serializable object
|
|
16
|
-
* @returns Hash object with hex string and raw bytes
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```typescript
|
|
20
|
-
* const hashResult = hash({ action: 'test' });
|
|
21
|
-
* console.log(hashResult.value); // 64-char hex string
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
function hash(data) {
|
|
25
|
-
const bytes = (0, binary_js_1.toBytes)(data, false);
|
|
26
|
-
return hashBytes(bytes);
|
|
27
|
-
}
|
|
28
|
-
exports.hash = hash;
|
|
29
|
-
/**
|
|
30
|
-
* Compute SHA-256 hash of raw bytes
|
|
31
|
-
*
|
|
32
|
-
* @param bytes - Input bytes
|
|
33
|
-
* @returns Hash object with hex string and raw bytes
|
|
34
|
-
*/
|
|
35
|
-
function hashBytes(bytes) {
|
|
36
|
-
const hashArray = js_sha256_1.sha256.array(bytes);
|
|
37
|
-
const hashUint8 = new Uint8Array(hashArray);
|
|
38
|
-
const hashHex = js_sha256_1.sha256.hex(bytes);
|
|
39
|
-
return {
|
|
40
|
-
value: hashHex,
|
|
41
|
-
bytes: hashUint8,
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
exports.hashBytes = hashBytes;
|
|
45
|
-
/**
|
|
46
|
-
* Compute the full signing digest according to Constellation protocol
|
|
47
|
-
*
|
|
48
|
-
* Protocol:
|
|
49
|
-
* 1. Serialize data to binary (with optional DataUpdate prefix)
|
|
50
|
-
* 2. Compute SHA-256 hash
|
|
51
|
-
* 3. Convert hash to hex string
|
|
52
|
-
* 4. Treat hex string as UTF-8 bytes (NOT hex decode)
|
|
53
|
-
* 5. Compute SHA-512 of those bytes
|
|
54
|
-
* 6. Truncate to 32 bytes for secp256k1 signing
|
|
55
|
-
*
|
|
56
|
-
* @param data - Any JSON-serializable object
|
|
57
|
-
* @param isDataUpdate - Whether to apply DataUpdate encoding
|
|
58
|
-
* @returns 32-byte digest ready for ECDSA signing
|
|
59
|
-
*/
|
|
60
|
-
function computeDigest(data, isDataUpdate = false) {
|
|
61
|
-
// Step 1: Serialize to binary
|
|
62
|
-
const dataBytes = (0, binary_js_1.toBytes)(data, isDataUpdate);
|
|
63
|
-
// Step 2: SHA-256 hash
|
|
64
|
-
const sha256Hash = hashBytes(dataBytes);
|
|
65
|
-
// Step 3-4: Hex string as UTF-8 bytes (critical: NOT hex decode)
|
|
66
|
-
const hexAsUtf8 = new TextEncoder().encode(sha256Hash.value);
|
|
67
|
-
// Step 5: SHA-512
|
|
68
|
-
const sha512Hash = js_sha512_1.sha512.array(hexAsUtf8);
|
|
69
|
-
// Step 6: Truncate to 32 bytes
|
|
70
|
-
return new Uint8Array(sha512Hash.slice(0, 32));
|
|
71
|
-
}
|
|
72
|
-
exports.computeDigest = computeDigest;
|
|
73
|
-
/**
|
|
74
|
-
* Compute SHA-256 hash of data with optional DataUpdate encoding
|
|
75
|
-
*
|
|
76
|
-
* @param data - Any JSON-serializable object
|
|
77
|
-
* @param isDataUpdate - Whether to apply DataUpdate encoding
|
|
78
|
-
* @returns Hash object
|
|
79
|
-
*/
|
|
80
|
-
function hashData(data, isDataUpdate = false) {
|
|
81
|
-
const bytes = (0, binary_js_1.toBytes)(data, isDataUpdate);
|
|
82
|
-
return hashBytes(bytes);
|
|
83
|
-
}
|
|
84
|
-
exports.hashData = hashData;
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Metakit SDK
|
|
4
|
-
*
|
|
5
|
-
* Reusable signing, encoding, and network operations for Constellation metagraphs.
|
|
6
|
-
* This module is framework-level functionality, independent of any specific metagraph domain.
|
|
7
|
-
*
|
|
8
|
-
* @packageDocumentation
|
|
9
|
-
*/
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.createDataTransactionRequest = exports.createScriptPayload = exports.createStateMachinePayload = exports.getPublicKeyForRegistration = exports.addTransactionSignature = exports.signTransaction = exports.createInvokeScriptPayload = exports.createArchivePayload = exports.createTransitionPayload = exports.NetworkError = exports.HttpClient = exports.DataL1Client = exports.CurrencyL1Client = exports.unitsToToken = exports.tokenToUnits = exports.isValidDagAddress = exports.getTransactionReference = exports.hashCurrencyTransaction = exports.encodeCurrencyTransaction = exports.verifyCurrencyTransaction = exports.signCurrencyTransaction = exports.createCurrencyTransactionBatch = exports.createCurrencyTransaction = exports.TOKEN_DECIMALS = exports.isValidPublicKey = exports.isValidPrivateKey = exports.getAddress = exports.getPublicKeyId = exports.getPublicKeyHex = exports.keyPairFromPrivateKey = exports.generateKeyPair = exports.batchSign = exports.addSignature = exports.createSignedObject = exports.verifySignature = exports.verifyHash = exports.verify = exports.signHash = exports.signDataUpdate = exports.sign = exports.decodeDataUpdate = exports.computeDigest = exports.hashData = exports.hashBytes = exports.hash = exports.encodeDataUpdate = exports.toBytes = exports.canonicalize = exports.CONSTELLATION_PREFIX = exports.ALGORITHM = void 0;
|
|
12
|
-
var types_js_1 = require("./types.js");
|
|
13
|
-
Object.defineProperty(exports, "ALGORITHM", { enumerable: true, get: function () { return types_js_1.ALGORITHM; } });
|
|
14
|
-
Object.defineProperty(exports, "CONSTELLATION_PREFIX", { enumerable: true, get: function () { return types_js_1.CONSTELLATION_PREFIX; } });
|
|
15
|
-
// Canonicalization
|
|
16
|
-
var canonicalize_js_1 = require("./canonicalize.js");
|
|
17
|
-
Object.defineProperty(exports, "canonicalize", { enumerable: true, get: function () { return canonicalize_js_1.canonicalize; } });
|
|
18
|
-
// Binary encoding
|
|
19
|
-
var binary_js_1 = require("./binary.js");
|
|
20
|
-
Object.defineProperty(exports, "toBytes", { enumerable: true, get: function () { return binary_js_1.toBytes; } });
|
|
21
|
-
Object.defineProperty(exports, "encodeDataUpdate", { enumerable: true, get: function () { return binary_js_1.encodeDataUpdate; } });
|
|
22
|
-
// Hashing
|
|
23
|
-
var hash_js_1 = require("./hash.js");
|
|
24
|
-
Object.defineProperty(exports, "hash", { enumerable: true, get: function () { return hash_js_1.hash; } });
|
|
25
|
-
Object.defineProperty(exports, "hashBytes", { enumerable: true, get: function () { return hash_js_1.hashBytes; } });
|
|
26
|
-
Object.defineProperty(exports, "hashData", { enumerable: true, get: function () { return hash_js_1.hashData; } });
|
|
27
|
-
Object.defineProperty(exports, "computeDigest", { enumerable: true, get: function () { return hash_js_1.computeDigest; } });
|
|
28
|
-
// Codec utilities
|
|
29
|
-
var codec_js_1 = require("./codec.js");
|
|
30
|
-
Object.defineProperty(exports, "decodeDataUpdate", { enumerable: true, get: function () { return codec_js_1.decodeDataUpdate; } });
|
|
31
|
-
// Signing
|
|
32
|
-
var sign_js_1 = require("./sign.js");
|
|
33
|
-
Object.defineProperty(exports, "sign", { enumerable: true, get: function () { return sign_js_1.sign; } });
|
|
34
|
-
Object.defineProperty(exports, "signDataUpdate", { enumerable: true, get: function () { return sign_js_1.signDataUpdate; } });
|
|
35
|
-
Object.defineProperty(exports, "signHash", { enumerable: true, get: function () { return sign_js_1.signHash; } });
|
|
36
|
-
// Verification
|
|
37
|
-
var verify_js_1 = require("./verify.js");
|
|
38
|
-
Object.defineProperty(exports, "verify", { enumerable: true, get: function () { return verify_js_1.verify; } });
|
|
39
|
-
Object.defineProperty(exports, "verifyHash", { enumerable: true, get: function () { return verify_js_1.verifyHash; } });
|
|
40
|
-
Object.defineProperty(exports, "verifySignature", { enumerable: true, get: function () { return verify_js_1.verifySignature; } });
|
|
41
|
-
// High-level API
|
|
42
|
-
var signed_object_js_1 = require("./signed-object.js");
|
|
43
|
-
Object.defineProperty(exports, "createSignedObject", { enumerable: true, get: function () { return signed_object_js_1.createSignedObject; } });
|
|
44
|
-
Object.defineProperty(exports, "addSignature", { enumerable: true, get: function () { return signed_object_js_1.addSignature; } });
|
|
45
|
-
Object.defineProperty(exports, "batchSign", { enumerable: true, get: function () { return signed_object_js_1.batchSign; } });
|
|
46
|
-
// Wallet utilities
|
|
47
|
-
var wallet_js_1 = require("./wallet.js");
|
|
48
|
-
Object.defineProperty(exports, "generateKeyPair", { enumerable: true, get: function () { return wallet_js_1.generateKeyPair; } });
|
|
49
|
-
Object.defineProperty(exports, "keyPairFromPrivateKey", { enumerable: true, get: function () { return wallet_js_1.keyPairFromPrivateKey; } });
|
|
50
|
-
Object.defineProperty(exports, "getPublicKeyHex", { enumerable: true, get: function () { return wallet_js_1.getPublicKeyHex; } });
|
|
51
|
-
Object.defineProperty(exports, "getPublicKeyId", { enumerable: true, get: function () { return wallet_js_1.getPublicKeyId; } });
|
|
52
|
-
Object.defineProperty(exports, "getAddress", { enumerable: true, get: function () { return wallet_js_1.getAddress; } });
|
|
53
|
-
Object.defineProperty(exports, "isValidPrivateKey", { enumerable: true, get: function () { return wallet_js_1.isValidPrivateKey; } });
|
|
54
|
-
Object.defineProperty(exports, "isValidPublicKey", { enumerable: true, get: function () { return wallet_js_1.isValidPublicKey; } });
|
|
55
|
-
var currency_types_js_1 = require("./currency-types.js");
|
|
56
|
-
Object.defineProperty(exports, "TOKEN_DECIMALS", { enumerable: true, get: function () { return currency_types_js_1.TOKEN_DECIMALS; } });
|
|
57
|
-
// Currency transaction operations
|
|
58
|
-
var currency_transaction_js_1 = require("./currency-transaction.js");
|
|
59
|
-
Object.defineProperty(exports, "createCurrencyTransaction", { enumerable: true, get: function () { return currency_transaction_js_1.createCurrencyTransaction; } });
|
|
60
|
-
Object.defineProperty(exports, "createCurrencyTransactionBatch", { enumerable: true, get: function () { return currency_transaction_js_1.createCurrencyTransactionBatch; } });
|
|
61
|
-
Object.defineProperty(exports, "signCurrencyTransaction", { enumerable: true, get: function () { return currency_transaction_js_1.signCurrencyTransaction; } });
|
|
62
|
-
Object.defineProperty(exports, "verifyCurrencyTransaction", { enumerable: true, get: function () { return currency_transaction_js_1.verifyCurrencyTransaction; } });
|
|
63
|
-
Object.defineProperty(exports, "encodeCurrencyTransaction", { enumerable: true, get: function () { return currency_transaction_js_1.encodeCurrencyTransaction; } });
|
|
64
|
-
Object.defineProperty(exports, "hashCurrencyTransaction", { enumerable: true, get: function () { return currency_transaction_js_1.hashCurrencyTransaction; } });
|
|
65
|
-
Object.defineProperty(exports, "getTransactionReference", { enumerable: true, get: function () { return currency_transaction_js_1.getTransactionReference; } });
|
|
66
|
-
Object.defineProperty(exports, "isValidDagAddress", { enumerable: true, get: function () { return currency_transaction_js_1.isValidDagAddress; } });
|
|
67
|
-
Object.defineProperty(exports, "tokenToUnits", { enumerable: true, get: function () { return currency_transaction_js_1.tokenToUnits; } });
|
|
68
|
-
Object.defineProperty(exports, "unitsToToken", { enumerable: true, get: function () { return currency_transaction_js_1.unitsToToken; } });
|
|
69
|
-
// Network operations
|
|
70
|
-
var index_js_1 = require("./network/index.js");
|
|
71
|
-
Object.defineProperty(exports, "CurrencyL1Client", { enumerable: true, get: function () { return index_js_1.CurrencyL1Client; } });
|
|
72
|
-
Object.defineProperty(exports, "DataL1Client", { enumerable: true, get: function () { return index_js_1.DataL1Client; } });
|
|
73
|
-
Object.defineProperty(exports, "HttpClient", { enumerable: true, get: function () { return index_js_1.HttpClient; } });
|
|
74
|
-
Object.defineProperty(exports, "NetworkError", { enumerable: true, get: function () { return index_js_1.NetworkError; } });
|
|
75
|
-
// Transaction helpers for self-signed mode
|
|
76
|
-
var transaction_js_1 = require("./transaction.js");
|
|
77
|
-
Object.defineProperty(exports, "createTransitionPayload", { enumerable: true, get: function () { return transaction_js_1.createTransitionPayload; } });
|
|
78
|
-
Object.defineProperty(exports, "createArchivePayload", { enumerable: true, get: function () { return transaction_js_1.createArchivePayload; } });
|
|
79
|
-
Object.defineProperty(exports, "createInvokeScriptPayload", { enumerable: true, get: function () { return transaction_js_1.createInvokeScriptPayload; } });
|
|
80
|
-
Object.defineProperty(exports, "signTransaction", { enumerable: true, get: function () { return transaction_js_1.signTransaction; } });
|
|
81
|
-
Object.defineProperty(exports, "addTransactionSignature", { enumerable: true, get: function () { return transaction_js_1.addTransactionSignature; } });
|
|
82
|
-
Object.defineProperty(exports, "getPublicKeyForRegistration", { enumerable: true, get: function () { return transaction_js_1.getPublicKeyForRegistration; } });
|
|
83
|
-
var transaction_js_2 = require("./transaction.js");
|
|
84
|
-
Object.defineProperty(exports, "createStateMachinePayload", { enumerable: true, get: function () { return transaction_js_2.createStateMachinePayload; } });
|
|
85
|
-
Object.defineProperty(exports, "createScriptPayload", { enumerable: true, get: function () { return transaction_js_2.createScriptPayload; } });
|
|
86
|
-
Object.defineProperty(exports, "createDataTransactionRequest", { enumerable: true, get: function () { return transaction_js_2.createDataTransactionRequest; } });
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Base HTTP client for network operations
|
|
4
|
-
*
|
|
5
|
-
* @packageDocumentation
|
|
6
|
-
*/
|
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.HttpClient = void 0;
|
|
9
|
-
const types_js_1 = require("./types.js");
|
|
10
|
-
const DEFAULT_TIMEOUT = 30000;
|
|
11
|
-
/**
|
|
12
|
-
* Simple HTTP client using native fetch
|
|
13
|
-
*/
|
|
14
|
-
class HttpClient {
|
|
15
|
-
constructor(baseUrl, timeout = DEFAULT_TIMEOUT) {
|
|
16
|
-
this.baseUrl = baseUrl.replace(/\/$/, '');
|
|
17
|
-
this.defaultTimeout = timeout;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Make a GET request
|
|
21
|
-
*/
|
|
22
|
-
async get(path, options = {}) {
|
|
23
|
-
return this.request('GET', path, undefined, options);
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Make a POST request
|
|
27
|
-
*/
|
|
28
|
-
async post(path, body, options = {}) {
|
|
29
|
-
return this.request('POST', path, body, options);
|
|
30
|
-
}
|
|
31
|
-
async request(method, path, body, options = {}) {
|
|
32
|
-
const url = `${this.baseUrl}${path}`;
|
|
33
|
-
const timeout = options.timeout ?? this.defaultTimeout;
|
|
34
|
-
const controller = new AbortController();
|
|
35
|
-
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
36
|
-
try {
|
|
37
|
-
const headers = {
|
|
38
|
-
'Content-Type': 'application/json',
|
|
39
|
-
Accept: 'application/json',
|
|
40
|
-
...options.headers,
|
|
41
|
-
};
|
|
42
|
-
const response = await fetch(url, {
|
|
43
|
-
method,
|
|
44
|
-
headers,
|
|
45
|
-
body: body ? JSON.stringify(body) : undefined,
|
|
46
|
-
signal: controller.signal,
|
|
47
|
-
});
|
|
48
|
-
clearTimeout(timeoutId);
|
|
49
|
-
const text = await response.text();
|
|
50
|
-
if (!response.ok) {
|
|
51
|
-
throw new types_js_1.NetworkError(`HTTP ${response.status}: ${response.statusText}`, response.status, text);
|
|
52
|
-
}
|
|
53
|
-
if (!text) {
|
|
54
|
-
return undefined;
|
|
55
|
-
}
|
|
56
|
-
try {
|
|
57
|
-
return JSON.parse(text);
|
|
58
|
-
}
|
|
59
|
-
catch {
|
|
60
|
-
return text;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
catch (error) {
|
|
64
|
-
clearTimeout(timeoutId);
|
|
65
|
-
if (error instanceof types_js_1.NetworkError) {
|
|
66
|
-
throw error;
|
|
67
|
-
}
|
|
68
|
-
if (error instanceof Error) {
|
|
69
|
-
if (error.name === 'AbortError') {
|
|
70
|
-
throw new types_js_1.NetworkError(`Request timeout after ${timeout}ms`);
|
|
71
|
-
}
|
|
72
|
-
throw new types_js_1.NetworkError(error.message);
|
|
73
|
-
}
|
|
74
|
-
throw new types_js_1.NetworkError('Unknown network error');
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
exports.HttpClient = HttpClient;
|