@altiuslabs/tx-sdk 0.1.5 → 0.1.7
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/README.md +1 -2
- package/package.json +1 -1
- package/src/index.js +3 -0
- package/src/utils.js +61 -0
package/README.md
CHANGED
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -25,5 +25,8 @@ export { NonceManager, create_nonce_manager } from './nonce.js';
|
|
|
25
25
|
// High-level client
|
|
26
26
|
export { TxClient, TxClientBuilder, create_tx_client } from './client.js';
|
|
27
27
|
|
|
28
|
+
// Utilities
|
|
29
|
+
export { keccak256, pad_hex, num_to_hex, rlp_encode, transaction_hash, computeFeeTokenAddress, isReservedAddress } from './utils.js';
|
|
30
|
+
|
|
28
31
|
// Constants
|
|
29
32
|
export * from './constants.js';
|
package/src/utils.js
CHANGED
|
@@ -56,3 +56,64 @@ export function rlp_encode(items) {
|
|
|
56
56
|
export function transaction_hash(signed_tx) {
|
|
57
57
|
return keccak256(signed_tx);
|
|
58
58
|
}
|
|
59
|
+
|
|
60
|
+
// ============================================================================
|
|
61
|
+
// Fee Token Address Computation
|
|
62
|
+
// ============================================================================
|
|
63
|
+
|
|
64
|
+
// Altius fee token prefix (10 bytes): 0xA1700000...
|
|
65
|
+
const FEE_TOKEN_PREFIX_BYTES = [0xA1, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Compute deterministic fee token address.
|
|
69
|
+
*
|
|
70
|
+
* Address = prefix(10B) || keccak256(creator, salt)[:10]
|
|
71
|
+
*
|
|
72
|
+
* @param {string} creator - Creator address (0x-prefixed hex)
|
|
73
|
+
* @param {string} salt - Salt as 0x-prefixed hex (32 bytes) or B256
|
|
74
|
+
* @returns {{address: string, index: bigint}} Computed address and lower 8 bytes as index
|
|
75
|
+
*/
|
|
76
|
+
export function computeFeeTokenAddress(creator, salt) {
|
|
77
|
+
// Clean addresses
|
|
78
|
+
const creatorClean = creator.startsWith('0x') ? creator.slice(2) : creator;
|
|
79
|
+
const saltClean = salt.startsWith('0x') ? salt.slice(2) : salt;
|
|
80
|
+
|
|
81
|
+
// RLP encode (creator, salt) - same as Rust implementation
|
|
82
|
+
const creatorBuf = Buffer.from(creatorClean, 'hex');
|
|
83
|
+
const saltBuf = Buffer.from(saltClean, 'hex');
|
|
84
|
+
const rlpEncoded = RLP.encode([creatorBuf, saltBuf]);
|
|
85
|
+
|
|
86
|
+
// Keccak256 hash
|
|
87
|
+
const hash = sha3.keccak_256(rlpEncoded);
|
|
88
|
+
const hashBytes = Buffer.from(hash, 'hex');
|
|
89
|
+
|
|
90
|
+
// Build address: prefix(10B) + hash[:10](10B)
|
|
91
|
+
const addressBytes = Buffer.alloc(20);
|
|
92
|
+
for (let i = 0; i < 10; i++) {
|
|
93
|
+
addressBytes[i] = FEE_TOKEN_PREFIX_BYTES[i];
|
|
94
|
+
}
|
|
95
|
+
for (let i = 0; i < 10; i++) {
|
|
96
|
+
addressBytes[10 + i] = hashBytes[i];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Compute index from lower 8 bytes
|
|
100
|
+
const lower8 = hashBytes.slice(0, 8);
|
|
101
|
+
const index = BigInt('0x' + lower8.toString('hex'));
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
address: '0x' + addressBytes.toString('hex'),
|
|
105
|
+
index,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Check if address is in reserved range (index < 256)
|
|
111
|
+
* @param {string} address - Address to check
|
|
112
|
+
* @param {string} creator - Creator address used for computation
|
|
113
|
+
* @param {string} salt - Salt used for computation
|
|
114
|
+
* @returns {boolean} True if reserved
|
|
115
|
+
*/
|
|
116
|
+
export function isReservedAddress(address, creator, salt) {
|
|
117
|
+
const { index } = computeFeeTokenAddress(creator, salt);
|
|
118
|
+
return index < 256n;
|
|
119
|
+
}
|