@altiuslabs/tx-sdk 0.1.10 → 0.1.12

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@altiuslabs/tx-sdk",
3
- "version": "0.1.10",
3
+ "version": "0.1.12",
4
4
  "description": "SDK for signing and sending Altius USD multi-token transactions",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/error.js CHANGED
@@ -40,5 +40,5 @@ export class MissingFieldError extends AltiusError {
40
40
  }
41
41
 
42
42
  // Re-export error classes for convenience
43
- export const Error = AltiusError;
44
- export const Result = AltiusError;
43
+ // export const Error = AltiusError; // Removed - causes initialization issues
44
+ // export const Result = AltiusError;
@@ -180,27 +180,30 @@ export class TxBuilder {
180
180
  signature_hash() {
181
181
  const tx = this.build();
182
182
 
183
- // For 0x7a transaction, the items are:
184
- // [chainId, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to, value, data, accessList, feeToken, feePayer, maxFeePerGasUsdAttodollars, feePayerSignature]
183
+ // For 0x7a transaction, encode fields WITHOUT fee_payer_signature (matches node's encode_for_signing)
184
+ // Format: [chainId, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to, value, input, accessList, feeToken, feePayer, maxFeePerGasUsdAttodollars]
185
185
  const list = [
186
- num_to_hex(tx.chain_id, 32),
187
- num_to_hex(tx.nonce, 32),
188
- num_to_hex(tx.max_priority_fee_per_gas, 32),
189
- num_to_hex(tx.max_fee_per_gas, 32),
190
- num_to_hex(tx.gas_limit, 32),
186
+ tx.chain_id,
187
+ tx.nonce,
188
+ tx.max_priority_fee_per_gas,
189
+ tx.max_fee_per_gas,
190
+ tx.gas_limit,
191
191
  tx.to,
192
- num_to_hex(tx.value, 32),
192
+ tx.value,
193
193
  tx.data,
194
194
  // Empty access list
195
- '0xc0',
195
+ [],
196
196
  tx.fee_token,
197
197
  tx.fee_payer,
198
- num_to_hex(tx.max_fee_per_gas_usd_attodollars, 32),
199
- tx.fee_payer_signature,
198
+ tx.max_fee_per_gas_usd_attodollars,
200
199
  ];
201
200
 
201
+ // Encode with type byte 0x7a prefix (matches node's encode_for_signing)
202
+ const type_byte = '0x7a';
202
203
  const encoded = rlp_encode(list);
203
- return keccak256(encoded);
204
+ const with_type = type_byte + encoded.slice(2);
205
+
206
+ return keccak256(with_type);
204
207
  }
205
208
 
206
209
  /**
@@ -218,22 +221,23 @@ export class TxBuilder {
218
221
  const tx = this.build();
219
222
 
220
223
  // Build signed transaction list (for EIP-2718 encoding)
224
+ // Format matches node's rlp_encode_fields: [chainId, nonce, ..., feePayerSignature, signature]
221
225
  const signed_fields = [
222
- num_to_hex(tx.chain_id, 32),
223
- num_to_hex(tx.nonce, 32),
224
- num_to_hex(tx.max_priority_fee_per_gas, 32),
225
- num_to_hex(tx.max_fee_per_gas, 32),
226
- num_to_hex(tx.gas_limit, 32),
226
+ tx.chain_id,
227
+ tx.nonce,
228
+ tx.max_priority_fee_per_gas,
229
+ tx.max_fee_per_gas,
230
+ tx.gas_limit,
227
231
  tx.to,
228
- num_to_hex(tx.value, 32),
232
+ tx.value,
229
233
  tx.data,
230
- '0xc0', // empty access list
234
+ [], // empty access list
231
235
  tx.fee_token,
232
236
  tx.fee_payer,
233
- num_to_hex(tx.max_fee_per_gas_usd_attodollars, 32),
237
+ tx.max_fee_per_gas_usd_attodollars,
234
238
  tx.fee_payer_signature,
235
239
  // Signature: y_parity (1 byte), r (32 bytes), s (32 bytes)
236
- num_to_hex(y_parity, 1),
240
+ y_parity,
237
241
  sig.r,
238
242
  sig.s,
239
243
  ];
@@ -281,12 +285,12 @@ export function fee_payer_signature_hash(tx, sender) {
281
285
  // RLP encode the fields as a list
282
286
  // [chain_id, nonce, gas_limit, fee_token, fee_payer, max_fee_per_gas_usd_attodollars, sender]
283
287
  const list = [
284
- num_to_hex(tx.chain_id, 32),
285
- num_to_hex(tx.nonce, 32),
286
- num_to_hex(tx.gas_limit, 32),
288
+ tx.chain_id,
289
+ tx.nonce,
290
+ tx.gas_limit,
287
291
  tx.fee_token,
288
292
  tx.fee_payer,
289
- num_to_hex(tx.max_fee_per_gas_usd_attodollars, 32),
293
+ tx.max_fee_per_gas_usd_attodollars,
290
294
  sender,
291
295
  ];
292
296
 
package/src/utils.js CHANGED
@@ -39,12 +39,53 @@ export function num_to_hex(num, bytes = 32) {
39
39
  }
40
40
 
41
41
  /**
42
- * Simple RLP encoding (for 0x7a transactions)
42
+ * Convert a value to RLP-compatible Buffer
43
+ * @param {any} value - Value to convert
44
+ * @returns {Buffer} RLP-compatible buffer
45
+ */
46
+ function toRlpBytes(value) {
47
+ if (Buffer.isBuffer(value)) {
48
+ return value;
49
+ }
50
+ if (typeof value === 'string') {
51
+ // Hex string
52
+ if (value.startsWith('0x')) {
53
+ return Buffer.from(value.slice(2), 'hex');
54
+ }
55
+ return Buffer.from(value);
56
+ }
57
+ if (typeof value === 'number' || typeof value === 'bigint') {
58
+ // Convert number/bigint to big-endian bytes
59
+ const bigIntValue = BigInt(value);
60
+ if (bigIntValue === 0n) {
61
+ return Buffer.from([]);
62
+ }
63
+ const hex = bigIntValue.toString(16);
64
+ // RLP encodes integers as big-endian without leading zeros (except for zero itself)
65
+ return Buffer.from(hex, 'hex');
66
+ }
67
+ if (Array.isArray(value)) {
68
+ // For empty arrays, return empty buffer
69
+ if (value.length === 0) {
70
+ return Buffer.from([]);
71
+ }
72
+ // Recursively encode array items
73
+ const encodedItems = value.map(item => toRlpBytes(item));
74
+ return Buffer.concat(encodedItems);
75
+ }
76
+ // Default: stringify
77
+ return Buffer.from(String(value));
78
+ }
79
+
80
+ /**
81
+ * RLP encoding (for 0x7a transactions)
43
82
  * @param {Array} items - Array of items to encode
44
83
  * @returns {string} RLP encoded data as 0x-prefixed hex
45
84
  */
46
85
  export function rlp_encode(items) {
47
- const encoded = RLP.encode(items);
86
+ // Convert all items to RLP-compatible buffers first
87
+ const buffers = items.map(item => toRlpBytes(item));
88
+ const encoded = RLP.encode(buffers);
48
89
  return '0x' + Buffer.from(encoded).toString('hex');
49
90
  }
50
91