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