ox 1.0.0-beta.9 → 1.0.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 (244) hide show
  1. package/CHANGELOG.md +1143 -0
  2. package/dist/core/AbiParameters.d.ts.map +1 -1
  3. package/dist/core/AbiParameters.js +4 -1
  4. package/dist/core/AbiParameters.js.map +1 -1
  5. package/dist/core/Rlp.d.ts +27 -5
  6. package/dist/core/Rlp.d.ts.map +1 -1
  7. package/dist/core/Rlp.js +40 -4
  8. package/dist/core/Rlp.js.map +1 -1
  9. package/dist/core/RpcSchema.d.ts +1 -1
  10. package/dist/core/internal/abiParameters.d.ts.map +1 -1
  11. package/dist/core/internal/abiParameters.js +21 -3
  12. package/dist/core/internal/abiParameters.js.map +1 -1
  13. package/dist/core/internal/rpcSchemas/eth.d.ts +36 -0
  14. package/dist/core/internal/rpcSchemas/eth.d.ts.map +1 -1
  15. package/dist/erc4337/EntryPoint.d.ts +1072 -1
  16. package/dist/erc4337/EntryPoint.d.ts.map +1 -1
  17. package/dist/erc4337/EntryPoint.js +872 -0
  18. package/dist/erc4337/EntryPoint.js.map +1 -1
  19. package/dist/erc4337/RpcSchema.d.ts +2 -2
  20. package/dist/erc4337/RpcSchema.d.ts.map +1 -1
  21. package/dist/erc4337/UserOperation.d.ts +39 -10
  22. package/dist/erc4337/UserOperation.d.ts.map +1 -1
  23. package/dist/erc4337/UserOperation.js +62 -48
  24. package/dist/erc4337/UserOperation.js.map +1 -1
  25. package/dist/erc4337/UserOperationGas.d.ts +9 -5
  26. package/dist/erc4337/UserOperationGas.d.ts.map +1 -1
  27. package/dist/erc4337/UserOperationGas.js +17 -15
  28. package/dist/erc4337/UserOperationGas.js.map +1 -1
  29. package/dist/erc4337/UserOperationReceipt.d.ts +4 -4
  30. package/dist/erc4337/UserOperationReceipt.d.ts.map +1 -1
  31. package/dist/erc4337/UserOperationReceipt.js.map +1 -1
  32. package/dist/tempo/Channel.d.ts +8 -14
  33. package/dist/tempo/Channel.d.ts.map +1 -1
  34. package/dist/tempo/Channel.js +4 -8
  35. package/dist/tempo/Channel.js.map +1 -1
  36. package/dist/tempo/KeyAuthorization.d.ts +5 -5
  37. package/dist/tempo/KeyAuthorization.d.ts.map +1 -1
  38. package/dist/tempo/KeyAuthorization.js +4 -4
  39. package/dist/tempo/KeyAuthorization.js.map +1 -1
  40. package/dist/tempo/MultisigConfig.d.ts +37 -85
  41. package/dist/tempo/MultisigConfig.d.ts.map +1 -1
  42. package/dist/tempo/MultisigConfig.js +39 -83
  43. package/dist/tempo/MultisigConfig.js.map +1 -1
  44. package/dist/tempo/PoolId.d.ts +8 -7
  45. package/dist/tempo/PoolId.d.ts.map +1 -1
  46. package/dist/tempo/PoolId.js +10 -6
  47. package/dist/tempo/PoolId.js.map +1 -1
  48. package/dist/tempo/SignatureEnvelope.d.ts +32 -30
  49. package/dist/tempo/SignatureEnvelope.d.ts.map +1 -1
  50. package/dist/tempo/SignatureEnvelope.js +56 -51
  51. package/dist/tempo/SignatureEnvelope.js.map +1 -1
  52. package/dist/tempo/Transaction.d.ts +16 -5
  53. package/dist/tempo/Transaction.d.ts.map +1 -1
  54. package/dist/tempo/Transaction.js +34 -7
  55. package/dist/tempo/Transaction.js.map +1 -1
  56. package/dist/tempo/TransactionReceipt.d.ts +3 -3
  57. package/dist/tempo/TransactionReceipt.d.ts.map +1 -1
  58. package/dist/tempo/TransactionReceipt.js +8 -1
  59. package/dist/tempo/TransactionReceipt.js.map +1 -1
  60. package/dist/tempo/TransactionRequest.d.ts +15 -2
  61. package/dist/tempo/TransactionRequest.d.ts.map +1 -1
  62. package/dist/tempo/TransactionRequest.js +65 -29
  63. package/dist/tempo/TransactionRequest.js.map +1 -1
  64. package/dist/tempo/TxEnvelopeTempo.d.ts +2 -3
  65. package/dist/tempo/TxEnvelopeTempo.d.ts.map +1 -1
  66. package/dist/tempo/TxEnvelopeTempo.js +5 -6
  67. package/dist/tempo/TxEnvelopeTempo.js.map +1 -1
  68. package/dist/tempo/index.d.ts +6 -26
  69. package/dist/tempo/index.d.ts.map +1 -1
  70. package/dist/tempo/index.js +6 -26
  71. package/dist/tempo/index.js.map +1 -1
  72. package/dist/zod/Abi.d.ts +214 -0
  73. package/dist/zod/Abi.d.ts.map +1 -0
  74. package/dist/zod/Abi.js +5 -0
  75. package/dist/zod/Abi.js.map +1 -0
  76. package/dist/zod/AbiConstructor.d.ts +58 -0
  77. package/dist/zod/AbiConstructor.d.ts.map +1 -0
  78. package/dist/zod/AbiConstructor.js +18 -0
  79. package/dist/zod/AbiConstructor.js.map +1 -0
  80. package/dist/zod/AbiError.d.ts +20 -0
  81. package/dist/zod/AbiError.d.ts.map +1 -0
  82. package/dist/zod/AbiError.js +9 -0
  83. package/dist/zod/AbiError.js.map +1 -0
  84. package/dist/zod/AbiEvent.d.ts +23 -0
  85. package/dist/zod/AbiEvent.d.ts.map +1 -0
  86. package/dist/zod/AbiEvent.js +11 -0
  87. package/dist/zod/AbiEvent.js.map +1 -0
  88. package/dist/zod/AbiFallback.d.ts +16 -0
  89. package/dist/zod/AbiFallback.d.ts.map +1 -0
  90. package/dist/zod/AbiFallback.js +16 -0
  91. package/dist/zod/AbiFallback.js.map +1 -0
  92. package/dist/zod/AbiFunction.d.ts +109 -0
  93. package/dist/zod/AbiFunction.d.ts.map +1 -0
  94. package/dist/zod/AbiFunction.js +26 -0
  95. package/dist/zod/AbiFunction.js.map +1 -0
  96. package/dist/zod/AbiItem.d.ts +216 -0
  97. package/dist/zod/AbiItem.d.ts.map +1 -0
  98. package/dist/zod/AbiItem.js +26 -0
  99. package/dist/zod/AbiItem.js.map +1 -0
  100. package/dist/zod/AbiParameter.d.ts +32 -0
  101. package/dist/zod/AbiParameter.d.ts.map +1 -0
  102. package/dist/zod/AbiParameter.js +28 -0
  103. package/dist/zod/AbiParameter.js.map +1 -0
  104. package/dist/zod/AbiParameters.d.ts +30 -0
  105. package/dist/zod/AbiParameters.d.ts.map +1 -0
  106. package/dist/zod/AbiParameters.js +6 -0
  107. package/dist/zod/AbiParameters.js.map +1 -0
  108. package/dist/zod/AbiReceive.d.ts +6 -0
  109. package/dist/zod/AbiReceive.d.ts.map +1 -0
  110. package/dist/zod/AbiReceive.js +7 -0
  111. package/dist/zod/AbiReceive.js.map +1 -0
  112. package/dist/zod/RpcSchema.d.ts +1 -0
  113. package/dist/zod/RpcSchema.d.ts.map +1 -1
  114. package/dist/zod/Solidity.d.ts +15 -0
  115. package/dist/zod/Solidity.d.ts.map +1 -0
  116. package/dist/zod/Solidity.js +41 -0
  117. package/dist/zod/Solidity.js.map +1 -0
  118. package/dist/zod/TypedData.d.ts +226 -0
  119. package/dist/zod/TypedData.d.ts.map +1 -0
  120. package/dist/zod/TypedData.js +84 -0
  121. package/dist/zod/TypedData.js.map +1 -0
  122. package/dist/zod/internal/rpcSchemas/Eth.d.ts +1 -0
  123. package/dist/zod/internal/rpcSchemas/Eth.d.ts.map +1 -1
  124. package/dist/zod/internal/rpcSchemas/Eth.js +5 -0
  125. package/dist/zod/internal/rpcSchemas/Eth.js.map +1 -1
  126. package/dist/zod/tempo/AuthorizationTempo.d.ts +0 -20
  127. package/dist/zod/tempo/AuthorizationTempo.d.ts.map +1 -1
  128. package/dist/zod/tempo/KeyAuthorization.d.ts +503 -67
  129. package/dist/zod/tempo/KeyAuthorization.d.ts.map +1 -1
  130. package/dist/zod/tempo/KeyAuthorization.js +32 -15
  131. package/dist/zod/tempo/KeyAuthorization.js.map +1 -1
  132. package/dist/zod/tempo/RpcSchemaTempo.d.ts +216 -34
  133. package/dist/zod/tempo/RpcSchemaTempo.d.ts.map +1 -1
  134. package/dist/zod/tempo/SignatureEnvelope.d.ts +0 -6
  135. package/dist/zod/tempo/SignatureEnvelope.d.ts.map +1 -1
  136. package/dist/zod/tempo/SignatureEnvelope.js +0 -2
  137. package/dist/zod/tempo/SignatureEnvelope.js.map +1 -1
  138. package/dist/zod/tempo/Transaction.d.ts +1479 -333
  139. package/dist/zod/tempo/Transaction.d.ts.map +1 -1
  140. package/dist/zod/tempo/Transaction.js +65 -16
  141. package/dist/zod/tempo/Transaction.js.map +1 -1
  142. package/dist/zod/tempo/TransactionRequest.d.ts +410 -57
  143. package/dist/zod/tempo/TransactionRequest.d.ts.map +1 -1
  144. package/dist/zod/tempo/TransactionRequest.js +81 -18
  145. package/dist/zod/tempo/TransactionRequest.js.map +1 -1
  146. package/dist/zod/tempo/TxEnvelopeTempo.d.ts +225 -18
  147. package/dist/zod/tempo/TxEnvelopeTempo.d.ts.map +1 -1
  148. package/dist/zod/tempo/TxEnvelopeTempo.js +1 -1
  149. package/dist/zod/tempo/TxEnvelopeTempo.js.map +1 -1
  150. package/dist/zod/tempo/ZoneRpcAuthentication.d.ts +0 -3
  151. package/dist/zod/tempo/ZoneRpcAuthentication.d.ts.map +1 -1
  152. package/dist/zod/tempo/z.d.ts +0 -1
  153. package/dist/zod/tempo/z.d.ts.map +1 -1
  154. package/dist/zod/tempo/z.js +0 -1
  155. package/dist/zod/tempo/z.js.map +1 -1
  156. package/dist/zod/z.d.ts +12 -0
  157. package/dist/zod/z.d.ts.map +1 -1
  158. package/dist/zod/z.js +12 -0
  159. package/dist/zod/z.js.map +1 -1
  160. package/package.json +62 -11
  161. package/src/core/AbiParameters.ts +3 -1
  162. package/src/core/Rlp.ts +66 -3
  163. package/src/core/RpcSchema.ts +1 -1
  164. package/src/core/_test/AbiParameters.decode.test.ts +88 -12
  165. package/src/core/_test/AbiParameters.encode.test.ts +68 -0
  166. package/src/core/_test/Rlp.test.ts +101 -0
  167. package/src/core/internal/abiParameters.ts +21 -3
  168. package/src/core/internal/rpcSchemas/eth.ts +36 -0
  169. package/src/erc4337/EntryPoint.ts +875 -1
  170. package/src/erc4337/RpcSchema.ts +4 -2
  171. package/src/erc4337/UserOperation.ts +130 -60
  172. package/src/erc4337/UserOperationGas.ts +36 -28
  173. package/src/erc4337/UserOperationReceipt.ts +15 -7
  174. package/src/erc4337/_test/EntryPoint.test.ts +48 -0
  175. package/src/erc4337/_test/RpcSchema.test-d.ts +56 -0
  176. package/src/erc4337/_test/UserOperation.snap-d.ts +31 -0
  177. package/src/erc4337/_test/UserOperation.test.ts +221 -5
  178. package/src/erc4337/_test/UserOperationGas.test.ts +30 -1
  179. package/src/tempo/Channel.test.ts +2 -23
  180. package/src/tempo/Channel.ts +10 -22
  181. package/src/tempo/KeyAuthorization.test.ts +22 -0
  182. package/src/tempo/KeyAuthorization.ts +9 -9
  183. package/src/tempo/MultisigConfig.test.ts +54 -37
  184. package/src/tempo/MultisigConfig.ts +59 -134
  185. package/src/tempo/PoolId.test.ts +7 -41
  186. package/src/tempo/PoolId.ts +15 -10
  187. package/src/tempo/SignatureEnvelope.test.ts +118 -17
  188. package/src/tempo/SignatureEnvelope.ts +77 -72
  189. package/src/tempo/Transaction.test.ts +0 -15
  190. package/src/tempo/Transaction.ts +63 -12
  191. package/src/tempo/TransactionReceipt.test.ts +76 -0
  192. package/src/tempo/TransactionReceipt.ts +11 -5
  193. package/src/tempo/TransactionRequest.test.ts +174 -1
  194. package/src/tempo/TransactionRequest.ts +84 -32
  195. package/src/tempo/TxEnvelopeTempo.test.ts +2 -2
  196. package/src/tempo/TxEnvelopeTempo.ts +7 -7
  197. package/src/tempo/e2e.test.ts +228 -50
  198. package/src/tempo/index.ts +6 -26
  199. package/src/version.ts +1 -1
  200. package/src/zod/Abi.ts +5 -0
  201. package/src/zod/AbiConstructor.ts +24 -0
  202. package/src/zod/AbiError.ts +9 -0
  203. package/src/zod/AbiEvent.ts +11 -0
  204. package/src/zod/AbiFallback.ts +22 -0
  205. package/src/zod/AbiFunction.ts +32 -0
  206. package/src/zod/AbiItem.ts +28 -0
  207. package/src/zod/AbiParameter.ts +36 -0
  208. package/src/zod/AbiParameters.ts +7 -0
  209. package/src/zod/AbiReceive.ts +7 -0
  210. package/src/zod/Solidity.ts +83 -0
  211. package/src/zod/TypedData.ts +111 -0
  212. package/src/zod/_test/Abi.test-d.ts +46 -0
  213. package/src/zod/_test/Abi.test.ts +163 -0
  214. package/src/zod/_test/TypedData.test-d.ts +20 -0
  215. package/src/zod/_test/TypedData.test.ts +87 -0
  216. package/src/zod/_test/z.test-d.ts +6 -0
  217. package/src/zod/_test/z.test.ts +17 -0
  218. package/src/zod/internal/rpcSchemas/Eth.ts +6 -0
  219. package/src/zod/tempo/KeyAuthorization.ts +34 -15
  220. package/src/zod/tempo/SignatureEnvelope.ts +0 -2
  221. package/src/zod/tempo/Transaction.ts +84 -22
  222. package/src/zod/tempo/TransactionRequest.ts +86 -20
  223. package/src/zod/tempo/TxEnvelopeTempo.ts +1 -1
  224. package/src/zod/tempo/_test/KeyAuthorization.test.ts +53 -0
  225. package/src/zod/tempo/_test/SignatureEnvelope.test.ts +0 -2
  226. package/src/zod/tempo/_test/Transaction.test.ts +155 -0
  227. package/src/zod/tempo/_test/TransactionRequest.test.ts +44 -0
  228. package/src/zod/tempo/z.ts +0 -1
  229. package/src/zod/z.ts +12 -0
  230. package/dist/tempo/TokenId.d.ts +0 -87
  231. package/dist/tempo/TokenId.d.ts.map +0 -1
  232. package/dist/tempo/TokenId.js +0 -123
  233. package/dist/tempo/TokenId.js.map +0 -1
  234. package/dist/zod/tempo/TokenId.d.ts +0 -6
  235. package/dist/zod/tempo/TokenId.d.ts.map +0 -1
  236. package/dist/zod/tempo/TokenId.js +0 -14
  237. package/dist/zod/tempo/TokenId.js.map +0 -1
  238. package/src/CHANGELOG.md +0 -820
  239. package/src/tempo/TokenId.bench.ts +0 -36
  240. package/src/tempo/TokenId.test.ts +0 -79
  241. package/src/tempo/TokenId.ts +0 -141
  242. package/src/zod/tempo/TokenId.ts +0 -15
  243. package/src/zod/tempo/_test/TokenId.test-d.ts +0 -17
  244. package/src/zod/tempo/_test/TokenId.test.ts +0 -21
@@ -1,36 +0,0 @@
1
- import { bench, describe } from 'vp/test'
2
- import * as TokenId from './TokenId.js'
3
-
4
- const sender = '0x1234567890123456789012345678901234567890' as const
5
- const salt =
6
- '0x0000000000000000000000000000000000000000000000000000000000000001' as const
7
-
8
- const tokenIdAddress = '0x20c0000000000000000000000000000000000001' as const
9
- const tokenIdBigInt = 1n
10
- const tokenIdLargeBigInt = 0xdeadbeefcafebabe1234n
11
-
12
- describe('TokenId.compute', () => {
13
- bench('hex sender', () => {
14
- TokenId.compute({ sender, salt })
15
- })
16
- })
17
-
18
- describe('TokenId.fromAddress', () => {
19
- bench('hex address', () => {
20
- TokenId.fromAddress(tokenIdAddress)
21
- })
22
- })
23
-
24
- describe('TokenId.toAddress', () => {
25
- bench('small bigint', () => {
26
- TokenId.toAddress(tokenIdBigInt)
27
- })
28
-
29
- bench('large bigint', () => {
30
- TokenId.toAddress(tokenIdLargeBigInt)
31
- })
32
-
33
- bench('hex address passthrough', () => {
34
- TokenId.toAddress(tokenIdAddress)
35
- })
36
- })
@@ -1,79 +0,0 @@
1
- import { Hex } from 'ox'
2
- import { TokenId } from 'ox/tempo'
3
- import { expect, test } from 'vp/test'
4
-
5
- test('from', () => {
6
- expect(TokenId.from(0n)).toBe(0n)
7
- expect(TokenId.from(0x1)).toBe(1n)
8
- expect(TokenId.from(0xdef)).toBe(0xdefn)
9
- expect(TokenId.from('0x20c0000000000000000000000000000000000000')).toBe(0n)
10
- expect(TokenId.from('0x20c0000000000000000000000000000000000001')).toBe(1n)
11
- expect(TokenId.from('0x20c0000000000000000000000000000000000def')).toBe(
12
- 0xdefn,
13
- )
14
- })
15
-
16
- test('fromAddress', () => {
17
- expect(
18
- TokenId.fromAddress('0x20c0000000000000000000000000000000000000'),
19
- ).toBe(0n)
20
- expect(
21
- TokenId.fromAddress('0x20c0000000000000000000000000000000000001'),
22
- ).toBe(1n)
23
- expect(
24
- TokenId.fromAddress('0x20c0000000000000000000000000000000000def'),
25
- ).toBe(0xdefn)
26
- })
27
-
28
- test('toAddress', () => {
29
- expect(TokenId.toAddress(0n)).toBe(
30
- '0x20c0000000000000000000000000000000000000',
31
- )
32
- expect(TokenId.toAddress('0x20c0000000000000000000000000000000000000')).toBe(
33
- '0x20c0000000000000000000000000000000000000',
34
- )
35
- expect(TokenId.toAddress(1n)).toBe(
36
- '0x20c0000000000000000000000000000000000001',
37
- )
38
- expect(TokenId.toAddress(0xdefn)).toBe(
39
- '0x20c0000000000000000000000000000000000def',
40
- )
41
- })
42
-
43
- test('compute', () => {
44
- const sender = '0x1234567890123456789012345678901234567890'
45
- const salt1 =
46
- '0x0000000000000000000000000000000000000000000000000000000000000001'
47
- const salt2 =
48
- '0x0000000000000000000000000000000000000000000000000000000000000002'
49
-
50
- const id1 = TokenId.compute({ sender, salt: salt1 })
51
- const id2 = TokenId.compute({ sender, salt: salt2 })
52
- const address1 = TokenId.toAddress(id1)
53
- const address2 = TokenId.toAddress(id2)
54
-
55
- // deterministic: same inputs produce same output
56
- expect(TokenId.compute({ sender, salt: salt1 })).toBe(id1)
57
-
58
- // different salts produce different addresses
59
- expect(address1).not.toBe(address2)
60
-
61
- // address suffix matches id
62
- expect(Hex.slice(address1, 12)).toBe(Hex.fromNumber(id1, { size: 8 }))
63
-
64
- // addresses have TIP-20 prefix (0x20c0 followed by zeroes for 12 bytes total)
65
- expect(address1.toLowerCase().startsWith('0x20c000000000000000000000')).toBe(
66
- true,
67
- )
68
- expect(address2.toLowerCase().startsWith('0x20c000000000000000000000')).toBe(
69
- true,
70
- )
71
-
72
- // addresses are 20 bytes (40 hex chars + 0x prefix)
73
- expect(address1.length).toBe(42)
74
-
75
- // different senders produce different addresses
76
- const otherSender = '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd'
77
- const address3 = TokenId.compute({ sender: otherSender, salt: salt1 })
78
- expect(address3).not.toBe(address1)
79
- })
@@ -1,141 +0,0 @@
1
- import { keccak_256 } from '@noble/hashes/sha3.js'
2
- import * as Address from '../core/Address.js'
3
- import * as Bytes from '../core/Bytes.js'
4
- import * as Hex from '../core/Hex.js'
5
-
6
- const tip20Prefix = '0x20c0'
7
-
8
- export type TokenId = bigint
9
- export type TokenIdOrAddress = TokenId | Address.Address
10
-
11
- /**
12
- * Converts a token ID or address to a token ID.
13
- *
14
- * TIP-20 is Tempo's native token standard for stablecoins with deterministic addresses
15
- * derived from sequential token IDs (prefix `0x20c0`).
16
- *
17
- * [TIP-20 Token Standard](https://docs.tempo.xyz/protocol/tip20/overview)
18
- *
19
- * @example
20
- * ```ts twoslash
21
- * import { TokenId } from 'ox/tempo'
22
- *
23
- * const tokenId = TokenId.from(1n)
24
- * ```
25
- *
26
- * @param tokenIdOrAddress - The token ID or address.
27
- * @returns The token ID.
28
- */
29
- export function from(tokenIdOrAddress: TokenIdOrAddress | number): TokenId {
30
- if (
31
- typeof tokenIdOrAddress === 'bigint' ||
32
- typeof tokenIdOrAddress === 'number'
33
- )
34
- return BigInt(tokenIdOrAddress)
35
- return fromAddress(tokenIdOrAddress)
36
- }
37
-
38
- /**
39
- * Converts a TIP-20 token address to a token ID.
40
- *
41
- * [TIP-20 Token Standard](https://docs.tempo.xyz/protocol/tip20/overview)
42
- *
43
- * @example
44
- * ```ts twoslash
45
- * import { TokenId } from 'ox/tempo'
46
- *
47
- * const tokenId = TokenId.fromAddress(
48
- * '0x20c0000000000000000000000000000000000001'
49
- * )
50
- * ```
51
- *
52
- * @param address - The token address.
53
- * @returns The token ID.
54
- */
55
- export function fromAddress(address: Address.Address): TokenId {
56
- const resolved = address
57
- // The TIP-20 prefix check is case-insensitive, but most inputs are already
58
- // lowercase, so peek the prefix bytes before falling back to a full
59
- // `.toLowerCase()`.
60
- const c2 = resolved.charCodeAt(2)
61
- const c3 = resolved.charCodeAt(3)
62
- const c4 = resolved.charCodeAt(4)
63
- const c5 = resolved.charCodeAt(5)
64
- const isPrefix =
65
- c2 === 50 /* '2' */ &&
66
- c3 === 48 /* '0' */ &&
67
- (c4 === 99 || c4 === 67) /* 'c' or 'C' */ &&
68
- c5 === 48 /* '0' */
69
- if (!isPrefix) throw new Error('invalid tip20 address.')
70
- // The trailing 36 hex characters encode the token id big-endian.
71
- return BigInt(`0x${resolved.slice(tip20Prefix.length)}`)
72
- }
73
-
74
- /**
75
- * Converts a TIP-20 token ID to an address.
76
- *
77
- * [TIP-20 Token Standard](https://docs.tempo.xyz/protocol/tip20/overview)
78
- *
79
- * @example
80
- * ```ts twoslash
81
- * import { TokenId } from 'ox/tempo'
82
- *
83
- * const address = TokenId.toAddress(1n)
84
- * ```
85
- *
86
- * @param tokenId - The token ID.
87
- * @returns The address.
88
- */
89
- export function toAddress(tokenId: TokenIdOrAddress): Address.Address {
90
- if (typeof tokenId === 'string') {
91
- Address.assert(tokenId)
92
- return tokenId
93
- }
94
-
95
- const tokenIdHex = Hex.fromNumber(tokenId, { size: 18 })
96
- return Hex.concat(tip20Prefix, tokenIdHex)
97
- }
98
-
99
- /**
100
- * Computes a deterministic TIP-20 token address from a sender address and salt.
101
- *
102
- * The address is computed as: `TIP20_PREFIX (12 bytes) || keccak256(abi.encode(sender, salt))[:8]`
103
- *
104
- * [TIP-20 Token Standard](https://docs.tempo.xyz/protocol/tip20/overview)
105
- *
106
- * @example
107
- * ```ts twoslash
108
- * import { TokenId } from 'ox/tempo'
109
- *
110
- * const id = TokenId.compute({
111
- * sender: '0x1234567890123456789012345678901234567890',
112
- * salt: '0x0000000000000000000000000000000000000000000000000000000000000001'
113
- * })
114
- * ```
115
- *
116
- * @param value - The sender address and salt.
117
- * @returns The computed TIP-20 token id.
118
- */
119
- export function compute(value: compute.Value): bigint {
120
- // ABI encoding of (address, bytes32) is the 32-byte left-padded address
121
- // followed by the 32-byte salt. Build that 64-byte buffer directly and hash
122
- // it, instead of round-tripping through `AbiParameters.encode`.
123
- const buffer = new Uint8Array(64)
124
- const addressBytes = Bytes.fromHex(value.sender)
125
- buffer.set(addressBytes, 12)
126
- buffer.set(Bytes.fromHex(value.salt, { size: 32 }), 32)
127
- const hash = keccak_256(buffer)
128
- // First 8 bytes of the digest, big-endian.
129
- let id = 0n
130
- for (let i = 0; i < 8; i++) id = (id << 8n) | BigInt(hash[i]!)
131
- return id
132
- }
133
-
134
- export declare namespace compute {
135
- export type Value = {
136
- /** The salt (32 bytes). */
137
- salt: Hex.Hex
138
- /** The sender address. */
139
- sender: Address.Address
140
- }
141
- }
@@ -1,15 +0,0 @@
1
- /* eslint-disable jsdoc-js/require-jsdoc, jsdoc-js/require-description, jsdoc-js/require-example */
2
- import * as core_TokenId from '../../tempo/TokenId.js'
3
- import * as z_Address from '../Address.js'
4
- import * as z from 'zod/mini'
5
-
6
- /** TIP-20 token id schema (non-negative bigint). */
7
- export const TokenId = z
8
- .bigint()
9
- .check(z.refine((value) => value >= 0n, 'expected token id'))
10
-
11
- /** Codec decoding a TIP-20 token address into a token id. */
12
- export const address = z.codec(z_Address.Address, TokenId, {
13
- decode: (value) => core_TokenId.fromAddress(value),
14
- encode: (value) => core_TokenId.toAddress(value),
15
- })
@@ -1,17 +0,0 @@
1
- import type * as core_Address from '../../../core/Address.js'
2
- import type * as core_TokenId from '../../../tempo/TokenId.js'
3
- import type * as z from 'zod/mini'
4
- import { expectTypeOf, test } from 'vp/test'
5
- import * as z_TokenId from '../TokenId.js'
6
-
7
- test('TokenId schemas preserve decoded and encoded types', () => {
8
- expectTypeOf<
9
- z.output<typeof z_TokenId.TokenId>
10
- >().toEqualTypeOf<core_TokenId.TokenId>()
11
- expectTypeOf<
12
- z.output<typeof z_TokenId.address>
13
- >().toEqualTypeOf<core_TokenId.TokenId>()
14
- expectTypeOf<
15
- z.input<typeof z_TokenId.address>
16
- >().toEqualTypeOf<core_Address.Address>()
17
- })
@@ -1,21 +0,0 @@
1
- import { describe, expect, test } from 'vp/test'
2
- import * as z_TokenId from '../TokenId.js'
3
- import * as z from 'zod/mini'
4
-
5
- describe('TokenId', () => {
6
- test('accepts non-negative bigints', () => {
7
- expect(z.safeDecode(z_TokenId.TokenId, 1n).success).toBe(true)
8
- expect(z.safeDecode(z_TokenId.TokenId, -1n).success).toMatchInlineSnapshot(
9
- `false`,
10
- )
11
- })
12
-
13
- test('decodes and encodes token addresses', () => {
14
- expect(
15
- z.decode(z_TokenId.address, '0x20c0000000000000000000000000000000000001'),
16
- ).toMatchInlineSnapshot(`1n`)
17
- expect(z.encode(z_TokenId.address, 1n)).toMatchInlineSnapshot(
18
- `"0x20c0000000000000000000000000000000000001"`,
19
- )
20
- })
21
- })