|
| 1 | +import * as AbiParameters from '../core/AbiParameters.js' |
1 | 2 | import * as Address from '../core/Address.js' |
| 3 | +import * as Hash from '../core/Hash.js' |
2 | 4 | import * as Hex from '../core/Hex.js' |
3 | 5 |
|
4 | 6 | const tip20Prefix = '0x20c0' |
@@ -78,3 +80,45 @@ export function toAddress(tokenId: TokenIdOrAddress): Address.Address { |
78 | 80 | const tokenIdHex = Hex.fromNumber(tokenId, { size: 18 }) |
79 | 81 | return Hex.concat(tip20Prefix, tokenIdHex) |
80 | 82 | } |
| 83 | + |
| 84 | +/** |
| 85 | + * Computes a deterministic TIP-20 token address from a sender address and salt. |
| 86 | + * |
| 87 | + * The address is computed as: `TIP20_PREFIX (12 bytes) || keccak256(abi.encode(sender, salt))[:8]` |
| 88 | + * |
| 89 | + * [TIP-20 Token Standard](https://docs.tempo.xyz/protocol/tip20/overview) |
| 90 | + * |
| 91 | + * @example |
| 92 | + * ```ts twoslash |
| 93 | + * import { TokenId } from 'ox/tempo' |
| 94 | + * |
| 95 | + * const address = TokenId.compute({ |
| 96 | + * sender: '0x1234567890123456789012345678901234567890', |
| 97 | + * salt: '0x0000000000000000000000000000000000000000000000000000000000000001', |
| 98 | + * }) |
| 99 | + * ``` |
| 100 | + * |
| 101 | + * @param value - The sender address and salt. |
| 102 | + * @returns The computed TIP-20 token address. |
| 103 | + */ |
| 104 | +export function compute(value: compute.Value): Address.Address { |
| 105 | + const hash = Hash.keccak256( |
| 106 | + AbiParameters.encode(AbiParameters.from('address, bytes32'), [ |
| 107 | + value.sender, |
| 108 | + value.salt, |
| 109 | + ]), |
| 110 | + ) |
| 111 | + return Hex.concat( |
| 112 | + Hex.padRight(tip20Prefix, 12), |
| 113 | + Hex.slice(hash, 0, 8), |
| 114 | + ) as Address.Address |
| 115 | +} |
| 116 | + |
| 117 | +export declare namespace compute { |
| 118 | + export type Value = { |
| 119 | + /** The salt (32 bytes). */ |
| 120 | + salt: Hex.Hex |
| 121 | + /** The sender address. */ |
| 122 | + sender: Address.Address |
| 123 | + } |
| 124 | +} |
0 commit comments