tempo.ts 0.1.5 → 0.2.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 (276) hide show
  1. package/README.md +33 -2
  2. package/dist/chains.d.ts +509 -115
  3. package/dist/chains.d.ts.map +1 -1
  4. package/dist/chains.js +18 -9
  5. package/dist/chains.js.map +1 -1
  6. package/dist/ox/Order.d.ts +92 -0
  7. package/dist/ox/Order.d.ts.map +1 -0
  8. package/dist/ox/Order.js +88 -0
  9. package/dist/ox/Order.js.map +1 -0
  10. package/dist/ox/OrdersFilters.d.ts +72 -0
  11. package/dist/ox/OrdersFilters.d.ts.map +1 -0
  12. package/dist/ox/OrdersFilters.js +100 -0
  13. package/dist/ox/OrdersFilters.js.map +1 -0
  14. package/dist/ox/Pagination.d.ts +128 -0
  15. package/dist/ox/Pagination.d.ts.map +1 -0
  16. package/dist/ox/Pagination.js +78 -0
  17. package/dist/ox/Pagination.js.map +1 -0
  18. package/dist/ox/PoolId.d.ts +18 -0
  19. package/dist/ox/PoolId.d.ts.map +1 -0
  20. package/dist/ox/PoolId.js +13 -0
  21. package/dist/ox/PoolId.js.map +1 -0
  22. package/dist/ox/RpcSchema.d.ts +32 -0
  23. package/dist/ox/RpcSchema.d.ts.map +1 -0
  24. package/dist/ox/RpcSchema.js +2 -0
  25. package/dist/ox/RpcSchema.js.map +1 -0
  26. package/dist/ox/SignatureEnvelope.d.ts +1 -1
  27. package/dist/ox/SignatureEnvelope.d.ts.map +1 -1
  28. package/dist/ox/SignatureEnvelope.js.map +1 -1
  29. package/dist/{viem → ox}/Tick.d.ts +4 -0
  30. package/dist/ox/Tick.d.ts.map +1 -0
  31. package/dist/ox/Tick.js.map +1 -0
  32. package/dist/ox/Transaction.d.ts.map +1 -1
  33. package/dist/ox/Transaction.js +2 -1
  34. package/dist/ox/Transaction.js.map +1 -1
  35. package/dist/ox/TransactionEnvelopeAA.d.ts +6 -6
  36. package/dist/ox/TransactionEnvelopeAA.d.ts.map +1 -1
  37. package/dist/ox/TransactionEnvelopeAA.js +4 -2
  38. package/dist/ox/TransactionEnvelopeAA.js.map +1 -1
  39. package/dist/ox/TransactionRequest.d.ts +4 -0
  40. package/dist/ox/TransactionRequest.d.ts.map +1 -1
  41. package/dist/ox/TransactionRequest.js.map +1 -1
  42. package/dist/ox/index.d.ts +6 -0
  43. package/dist/ox/index.d.ts.map +1 -1
  44. package/dist/ox/index.js +6 -0
  45. package/dist/ox/index.js.map +1 -1
  46. package/dist/prool/Instance.d.ts.map +1 -1
  47. package/dist/prool/Instance.js +20 -4
  48. package/dist/prool/Instance.js.map +1 -1
  49. package/dist/viem/Abis.d.ts +1469 -1082
  50. package/dist/viem/Abis.d.ts.map +1 -1
  51. package/dist/viem/Abis.js +932 -671
  52. package/dist/viem/Abis.js.map +1 -1
  53. package/dist/viem/Account.d.ts +150 -0
  54. package/dist/viem/Account.d.ts.map +1 -0
  55. package/dist/viem/Account.js +221 -0
  56. package/dist/viem/Account.js.map +1 -0
  57. package/dist/viem/Actions/amm.d.ts +80 -118
  58. package/dist/viem/Actions/amm.d.ts.map +1 -1
  59. package/dist/viem/Actions/amm.js +47 -116
  60. package/dist/viem/Actions/amm.js.map +1 -1
  61. package/dist/viem/Actions/dex.d.ts +889 -633
  62. package/dist/viem/Actions/dex.d.ts.map +1 -1
  63. package/dist/viem/Actions/dex.js +99 -0
  64. package/dist/viem/Actions/dex.js.map +1 -1
  65. package/dist/viem/Actions/fee.d.ts +3 -17
  66. package/dist/viem/Actions/fee.d.ts.map +1 -1
  67. package/dist/viem/Actions/fee.js.map +1 -1
  68. package/dist/viem/Actions/index.d.ts +1 -0
  69. package/dist/viem/Actions/index.d.ts.map +1 -1
  70. package/dist/viem/Actions/index.js +1 -0
  71. package/dist/viem/Actions/index.js.map +1 -1
  72. package/dist/viem/Actions/reward.d.ts +3236 -0
  73. package/dist/viem/Actions/reward.d.ts.map +1 -0
  74. package/dist/viem/Actions/reward.js +725 -0
  75. package/dist/viem/Actions/reward.js.map +1 -0
  76. package/dist/viem/Actions/token.d.ts +4295 -2646
  77. package/dist/viem/Actions/token.d.ts.map +1 -1
  78. package/dist/viem/Actions/token.js +214 -335
  79. package/dist/viem/Actions/token.js.map +1 -1
  80. package/dist/viem/Addresses.d.ts +1 -2
  81. package/dist/viem/Addresses.d.ts.map +1 -1
  82. package/dist/viem/Addresses.js +1 -2
  83. package/dist/viem/Addresses.js.map +1 -1
  84. package/dist/viem/Chain.d.ts +38 -12
  85. package/dist/viem/Chain.d.ts.map +1 -1
  86. package/dist/viem/Chain.js +27 -18
  87. package/dist/viem/Chain.js.map +1 -1
  88. package/dist/viem/Decorator.d.ts +959 -405
  89. package/dist/viem/Decorator.d.ts.map +1 -1
  90. package/dist/viem/Decorator.js +13 -5
  91. package/dist/viem/Decorator.js.map +1 -1
  92. package/dist/viem/Formatters.d.ts +8 -1
  93. package/dist/viem/Formatters.d.ts.map +1 -1
  94. package/dist/viem/Formatters.js +17 -0
  95. package/dist/viem/Formatters.js.map +1 -1
  96. package/dist/viem/P256.d.ts +2 -0
  97. package/dist/viem/P256.d.ts.map +1 -0
  98. package/dist/viem/P256.js +2 -0
  99. package/dist/viem/P256.js.map +1 -0
  100. package/dist/viem/Secp256k1.d.ts +2 -0
  101. package/dist/viem/Secp256k1.d.ts.map +1 -0
  102. package/dist/viem/Secp256k1.js +2 -0
  103. package/dist/viem/Secp256k1.js.map +1 -0
  104. package/dist/viem/TokenIds.d.ts +1 -2
  105. package/dist/viem/TokenIds.d.ts.map +1 -1
  106. package/dist/viem/TokenIds.js +1 -2
  107. package/dist/viem/TokenIds.js.map +1 -1
  108. package/dist/viem/Transaction.d.ts +1 -1
  109. package/dist/viem/Transaction.d.ts.map +1 -1
  110. package/dist/viem/Transaction.js +46 -5
  111. package/dist/viem/Transaction.js.map +1 -1
  112. package/dist/viem/WebAuthnP256.d.ts +79 -0
  113. package/dist/viem/WebAuthnP256.d.ts.map +1 -0
  114. package/dist/viem/WebAuthnP256.js +95 -0
  115. package/dist/viem/WebAuthnP256.js.map +1 -0
  116. package/dist/viem/WebCryptoP256.d.ts +2 -0
  117. package/dist/viem/WebCryptoP256.d.ts.map +1 -0
  118. package/dist/viem/WebCryptoP256.js +2 -0
  119. package/dist/viem/WebCryptoP256.js.map +1 -0
  120. package/dist/viem/index.d.ts +6 -3
  121. package/dist/viem/index.d.ts.map +1 -1
  122. package/dist/viem/index.js +6 -3
  123. package/dist/viem/index.js.map +1 -1
  124. package/dist/viem/internal/account.d.ts +24 -0
  125. package/dist/viem/internal/account.d.ts.map +1 -0
  126. package/dist/viem/internal/account.js +68 -0
  127. package/dist/viem/internal/account.js.map +1 -0
  128. package/dist/viem/internal/types.d.ts +10 -0
  129. package/dist/viem/internal/types.d.ts.map +1 -1
  130. package/dist/wagmi/Actions/amm.d.ts +428 -0
  131. package/dist/wagmi/Actions/amm.d.ts.map +1 -0
  132. package/dist/wagmi/Actions/amm.js +472 -0
  133. package/dist/wagmi/Actions/amm.js.map +1 -0
  134. package/dist/wagmi/Actions/dex.d.ts +908 -0
  135. package/dist/wagmi/Actions/dex.d.ts.map +1 -0
  136. package/dist/wagmi/Actions/dex.js +1023 -0
  137. package/dist/wagmi/Actions/dex.js.map +1 -0
  138. package/dist/wagmi/Actions/fee.d.ts +111 -0
  139. package/dist/wagmi/Actions/fee.d.ts.map +1 -0
  140. package/dist/wagmi/Actions/fee.js +126 -0
  141. package/dist/wagmi/Actions/fee.js.map +1 -0
  142. package/dist/wagmi/Actions/index.d.ts +6 -0
  143. package/dist/wagmi/Actions/index.d.ts.map +1 -0
  144. package/dist/wagmi/Actions/index.js +6 -0
  145. package/dist/wagmi/Actions/index.js.map +1 -0
  146. package/dist/wagmi/Actions/reward.d.ts +348 -0
  147. package/dist/wagmi/Actions/reward.d.ts.map +1 -0
  148. package/dist/wagmi/Actions/reward.js +388 -0
  149. package/dist/wagmi/Actions/reward.js.map +1 -0
  150. package/dist/wagmi/Actions/token.d.ts +1546 -0
  151. package/dist/wagmi/Actions/token.d.ts.map +1 -0
  152. package/dist/wagmi/Actions/token.js +1712 -0
  153. package/dist/wagmi/Actions/token.js.map +1 -0
  154. package/dist/wagmi/Connector.d.ts +73 -0
  155. package/dist/wagmi/Connector.d.ts.map +1 -0
  156. package/dist/wagmi/Connector.js +249 -0
  157. package/dist/wagmi/Connector.js.map +1 -0
  158. package/dist/wagmi/Hooks/amm.d.ts +421 -0
  159. package/dist/wagmi/Hooks/amm.d.ts.map +1 -0
  160. package/dist/wagmi/Hooks/amm.js +504 -0
  161. package/dist/wagmi/Hooks/amm.js.map +1 -0
  162. package/dist/wagmi/Hooks/dex.d.ts +816 -0
  163. package/dist/wagmi/Hooks/dex.d.ts.map +1 -0
  164. package/dist/wagmi/Hooks/dex.js +973 -0
  165. package/dist/wagmi/Hooks/dex.js.map +1 -0
  166. package/dist/wagmi/Hooks/fee.d.ts +97 -0
  167. package/dist/wagmi/Hooks/fee.d.ts.map +1 -0
  168. package/dist/wagmi/Hooks/fee.js +109 -0
  169. package/dist/wagmi/Hooks/fee.js.map +1 -0
  170. package/dist/wagmi/Hooks/index.d.ts +6 -0
  171. package/dist/wagmi/Hooks/index.d.ts.map +1 -0
  172. package/dist/wagmi/Hooks/index.js +6 -0
  173. package/dist/wagmi/Hooks/index.js.map +1 -0
  174. package/dist/wagmi/Hooks/reward.d.ts +307 -0
  175. package/dist/wagmi/Hooks/reward.d.ts.map +1 -0
  176. package/dist/wagmi/Hooks/reward.js +349 -0
  177. package/dist/wagmi/Hooks/reward.js.map +1 -0
  178. package/dist/wagmi/Hooks/token.d.ts +1388 -0
  179. package/dist/wagmi/Hooks/token.d.ts.map +1 -0
  180. package/dist/wagmi/Hooks/token.js +1657 -0
  181. package/dist/wagmi/Hooks/token.js.map +1 -0
  182. package/dist/wagmi/index.d.ts +4 -0
  183. package/dist/wagmi/index.d.ts.map +1 -0
  184. package/dist/wagmi/index.js +4 -0
  185. package/dist/wagmi/index.js.map +1 -0
  186. package/package.json +54 -10
  187. package/src/chains.ts +19 -9
  188. package/src/ox/Order.test.ts +78 -0
  189. package/src/ox/Order.ts +125 -0
  190. package/src/ox/OrdersFilters.test.ts +182 -0
  191. package/src/ox/OrdersFilters.ts +125 -0
  192. package/src/ox/Pagination.test.ts +162 -0
  193. package/src/ox/Pagination.ts +164 -0
  194. package/src/ox/PoolId.test.ts +33 -0
  195. package/src/ox/PoolId.ts +27 -0
  196. package/src/ox/RpcSchema.ts +35 -0
  197. package/src/ox/SignatureEnvelope.ts +3 -1
  198. package/src/{viem → ox}/Tick.test.ts +1 -1
  199. package/src/{viem → ox}/Tick.ts +5 -0
  200. package/src/ox/Transaction.test.ts +1 -1
  201. package/src/ox/Transaction.ts +2 -1
  202. package/src/ox/TransactionEnvelopeAA.test.ts +239 -96
  203. package/src/ox/TransactionEnvelopeAA.ts +9 -7
  204. package/src/ox/TransactionRequest.ts +4 -0
  205. package/src/ox/index.ts +6 -0
  206. package/src/prool/Instance.ts +51 -37
  207. package/src/prool/internal/chain.json +104 -52
  208. package/src/tsconfig.json +9 -0
  209. package/src/viem/Abis.ts +972 -710
  210. package/src/viem/Account.ts +279 -0
  211. package/src/viem/Actions/__snapshots__/dex.test.ts.snap +850 -0
  212. package/src/viem/Actions/amm.test.ts +98 -169
  213. package/src/viem/Actions/amm.ts +68 -155
  214. package/src/viem/Actions/dex.test.ts +563 -484
  215. package/src/viem/Actions/dex.ts +173 -0
  216. package/src/viem/Actions/fee.test.ts +23 -34
  217. package/src/viem/Actions/fee.ts +7 -0
  218. package/src/viem/Actions/index.ts +1 -0
  219. package/src/viem/Actions/policy.test.ts +19 -33
  220. package/src/viem/Actions/reward.test.ts +457 -0
  221. package/src/viem/Actions/reward.ts +999 -0
  222. package/src/viem/Actions/token.test.ts +453 -287
  223. package/src/viem/Actions/token.ts +452 -540
  224. package/src/viem/Addresses.ts +1 -2
  225. package/src/viem/Chain.ts +70 -20
  226. package/src/viem/Decorator.test.ts +2 -1
  227. package/src/viem/Decorator.ts +996 -421
  228. package/src/viem/Formatters.ts +31 -5
  229. package/src/viem/P256.ts +1 -0
  230. package/src/viem/Secp256k1.ts +1 -0
  231. package/src/viem/TokenIds.ts +1 -2
  232. package/src/viem/Transaction.ts +53 -7
  233. package/src/viem/WebAuthnP256.ts +140 -0
  234. package/src/viem/WebCryptoP256.ts +1 -0
  235. package/src/viem/e2e.test.ts +1126 -297
  236. package/src/viem/index.ts +6 -3
  237. package/src/viem/internal/account.ts +107 -0
  238. package/src/viem/internal/types.ts +9 -0
  239. package/src/wagmi/Actions/__snapshots__/dex.test.ts.snap +310 -0
  240. package/src/wagmi/Actions/amm.test.ts +198 -0
  241. package/src/wagmi/Actions/amm.ts +691 -0
  242. package/src/wagmi/Actions/dex.test.ts +1507 -0
  243. package/src/wagmi/Actions/dex.ts +1640 -0
  244. package/src/wagmi/Actions/fee.test.ts +63 -0
  245. package/src/wagmi/Actions/fee.ts +208 -0
  246. package/src/wagmi/Actions/index.ts +5 -0
  247. package/src/wagmi/Actions/reward.test.ts +210 -0
  248. package/src/wagmi/Actions/reward.ts +632 -0
  249. package/src/wagmi/Actions/token.test.ts +1308 -0
  250. package/src/wagmi/Actions/token.ts +2613 -0
  251. package/src/wagmi/Connector.test.ts +53 -0
  252. package/src/wagmi/Connector.ts +367 -0
  253. package/src/wagmi/Hooks/__snapshots__/dex.test.ts.snap +457 -0
  254. package/src/wagmi/Hooks/amm.test.ts +424 -0
  255. package/src/wagmi/Hooks/amm.ts +806 -0
  256. package/src/wagmi/Hooks/dex.test.ts +1017 -0
  257. package/src/wagmi/Hooks/dex.ts +1685 -0
  258. package/src/wagmi/Hooks/fee.test.ts +166 -0
  259. package/src/wagmi/Hooks/fee.ts +206 -0
  260. package/src/wagmi/Hooks/index.ts +5 -0
  261. package/src/wagmi/Hooks/reward.test.ts +219 -0
  262. package/src/wagmi/Hooks/reward.ts +672 -0
  263. package/src/wagmi/Hooks/token.test.ts +1670 -0
  264. package/src/wagmi/Hooks/token.ts +2906 -0
  265. package/src/wagmi/index.ts +3 -0
  266. package/src/wagmi/internal/types.ts +16 -0
  267. package/dist/viem/Client.d.ts +0 -27
  268. package/dist/viem/Client.d.ts.map +0 -1
  269. package/dist/viem/Client.js +0 -28
  270. package/dist/viem/Client.js.map +0 -1
  271. package/dist/viem/Tick.d.ts.map +0 -1
  272. package/dist/viem/Tick.js.map +0 -1
  273. package/src/viem/Client.bench-d.ts +0 -8
  274. package/src/viem/Client.test.ts +0 -178
  275. package/src/viem/Client.ts +0 -91
  276. /package/dist/{viem → ox}/Tick.js +0 -0
@@ -1,16 +1,21 @@
1
+ // TODO: Find opportunities to make this file less duplicated + more simplified with Viem v3.
2
+
1
3
  import * as Hex from 'ox/Hex'
2
4
  import {
5
+ type Chain,
6
+ type Account as viem_Account,
3
7
  formatTransaction as viem_formatTransaction,
4
8
  formatTransactionRequest as viem_formatTransactionRequest,
5
9
  } from 'viem'
6
10
  import { parseAccount } from 'viem/accounts'
11
+ import type { UnionOmit } from '../internal/types.js'
7
12
  import * as ox_Transaction from '../ox/Transaction.js'
8
13
  import * as ox_TransactionRequest from '../ox/TransactionRequest.js'
14
+ import type { GetFeeTokenParameter } from './internal/types.js'
9
15
  import {
10
16
  isTempo,
11
17
  type Transaction,
12
18
  type TransactionRequest,
13
- type TransactionRequestAA,
14
19
  type TransactionRequestRpc,
15
20
  type TransactionRpc,
16
21
  } from './Transaction.js'
@@ -53,12 +58,21 @@ export const formatTransaction = (
53
58
  }
54
59
  }
55
60
 
56
- export const formatTransactionRequest = (
57
- r: TransactionRequest,
61
+ type Request<chain extends Chain | undefined> = UnionOmit<
62
+ TransactionRequest,
63
+ 'feeToken'
64
+ > &
65
+ GetFeeTokenParameter<chain> & { account?: viem_Account | undefined }
66
+ export const formatTransactionRequest = <chain extends Chain | undefined>(
67
+ r: Request<chain>,
58
68
  action?: string | undefined,
59
69
  ): TransactionRequestRpc => {
60
- const request = r as TransactionRequestAA
70
+ const request = r as Request<chain>
71
+
72
+ // Convert EIP-1559 transactions to AA transactions.
73
+ if (request.type === 'eip1559') (request as any).type = 'aa'
61
74
 
75
+ // If the request is not a Tempo transaction, route to Viem formatter.
62
76
  if (!isTempo(request))
63
77
  return viem_formatTransactionRequest(
64
78
  r as never,
@@ -85,7 +99,7 @@ export const formatTransactionRequest = (
85
99
  })),
86
100
  nonce: request.nonce ? BigInt(request.nonce) : undefined,
87
101
  type: 'aa',
88
- })
102
+ } as never)
89
103
 
90
104
  if (action === 'estimateGas') {
91
105
  rpc.maxFeePerGas = undefined
@@ -97,8 +111,20 @@ export const formatTransactionRequest = (
97
111
  rpc.data = undefined
98
112
  rpc.value = undefined
99
113
 
114
+ const [keyType, keyData] = (() => {
115
+ if (!r.account?.source) return [undefined, undefined]
116
+ if (r.account.source === 'webAuthn')
117
+ // TODO: derive correct bytes size of key data based on webauthn create metadata.
118
+ return ['webAuthn', `0x${'ff'.repeat(1400)}`]
119
+ if (['p256', 'secp256k1'].includes(r.account.source))
120
+ return [r.account.source, undefined]
121
+ return [undefined, undefined]
122
+ })()
123
+
100
124
  return {
101
125
  ...rpc,
126
+ ...(keyType ? { keyType } : {}),
127
+ ...(keyData ? { keyData } : {}),
102
128
  ...(request.feePayer
103
129
  ? {
104
130
  feePayer:
@@ -0,0 +1 @@
1
+ export { randomPrivateKey } from 'ox/P256'
@@ -0,0 +1 @@
1
+ export { randomPrivateKey } from 'ox/Secp256k1'
@@ -1,2 +1 @@
1
- export const defaultFeeToken = 1n
2
- export const defaultQuoteToken = 0n
1
+ export const linkingUsd = 0n
@@ -1,3 +1,5 @@
1
+ // TODO: Find opportunities to make this file less duplicated + more simplified with Viem v3.
2
+
1
3
  import * as Hex from 'ox/Hex'
2
4
  import * as Secp256k1 from 'ox/Secp256k1'
3
5
  import * as Signature from 'ox/Signature'
@@ -150,8 +152,16 @@ export function deserialize<
150
152
  const serialized extends TransactionSerializedGeneric,
151
153
  >(serializedTransaction: serialized): deserialize.ReturnValue<serialized> {
152
154
  const type = Hex.slice(serializedTransaction, 0, 1)
153
- if (type === '0x76')
154
- return deserializeAA(serializedTransaction as `0x76${string}`) as never
155
+ if (type === '0x76') {
156
+ const from =
157
+ Hex.slice(serializedTransaction, -6) === '0xfeefeefeefee'
158
+ ? Hex.slice(serializedTransaction, -26, -6)
159
+ : undefined
160
+ return {
161
+ ...deserializeAA(serializedTransaction as `0x76${string}`),
162
+ from,
163
+ } as never
164
+ }
155
165
  return viem_parseTransaction(serializedTransaction) as never
156
166
  }
157
167
 
@@ -169,10 +179,31 @@ export async function serialize(
169
179
  feePayer?: Account | true | undefined
170
180
  from?: Address | undefined
171
181
  },
172
- signature?: viem_Signature | undefined,
182
+ signature?:
183
+ | OneOf<SignatureEnvelope.SignatureEnvelope | viem_Signature>
184
+ | undefined,
173
185
  ) {
174
- if (!isTempo(transaction))
186
+ // Convert EIP-1559 transactions to AA transactions.
187
+ if (transaction.type === 'eip1559') (transaction as any).type = 'aa'
188
+
189
+ // If the transaction is not a Tempo transaction, route to Viem serializer.
190
+ if (!isTempo(transaction)) {
191
+ if (signature && 'type' in signature && signature.type !== 'secp256k1')
192
+ throw new Error(
193
+ 'Unsupported signature type. Expected `secp256k1` but got `' +
194
+ signature.type +
195
+ '`.',
196
+ )
197
+ if (signature && 'type' in signature) {
198
+ const { r, s, yParity } = signature?.signature!
199
+ return viem_serializeTransaction(transaction as never, {
200
+ r: Hex.fromNumber(r, { size: 32 }),
201
+ s: Hex.fromNumber(s, { size: 32 }),
202
+ yParity,
203
+ })
204
+ }
175
205
  return viem_serializeTransaction(transaction as never, signature)
206
+ }
176
207
 
177
208
  const type = getType(transaction)
178
209
  if (type === 'aa')
@@ -215,10 +246,11 @@ async function serializeAA(
215
246
  feePayer?: Account | true | undefined
216
247
  from?: Address | undefined
217
248
  },
218
- sig?: viem_Signature | undefined,
249
+ sig?: OneOf<SignatureEnvelope.SignatureEnvelope | viem_Signature> | undefined,
219
250
  ) {
220
251
  const signature = (() => {
221
252
  if (transaction.signature) return transaction.signature
253
+ if (sig && 'type' in sig) return sig as SignatureEnvelope.SignatureEnvelope
222
254
  if (sig)
223
255
  return SignatureEnvelope.from({
224
256
  r: BigInt(sig.r!),
@@ -275,12 +307,13 @@ async function serializeAA(
275
307
  })
276
308
 
277
309
  const sender = (() => {
310
+ if (transaction.from) return transaction.from
278
311
  if (signature.type === 'secp256k1')
279
312
  return Secp256k1.recoverAddress({
280
313
  payload: TxAA.getSignPayload(tx),
281
314
  signature: signature.signature,
282
315
  })
283
- throw new Error('Unsupported signature type')
316
+ throw new Error('Unable to extract sender from transaction or signature.')
284
317
  })()
285
318
 
286
319
  const hash = TxAA.getFeePayerSignPayload(tx, {
@@ -296,8 +329,21 @@ async function serializeAA(
296
329
  })
297
330
  }
298
331
 
332
+ if (feePayer === true) {
333
+ const serialized = TxAA.serialize(transaction_ox, {
334
+ feePayerSignature: null,
335
+ signature,
336
+ })
337
+ // if the transaction is ready to be sent off (signed), add the sender
338
+ // and a fee marker to the serialized transaction, so the fee payer proxy
339
+ // can infer the sender address.
340
+ if (transaction.from && signature)
341
+ return Hex.concat(serialized, transaction.from, '0xfeefeefeefee')
342
+ return serialized
343
+ }
344
+
299
345
  return TxAA.serialize(transaction_ox, {
300
- feePayerSignature: feePayer === true ? null : undefined,
346
+ feePayerSignature: undefined,
301
347
  signature,
302
348
  })
303
349
  }
@@ -0,0 +1,140 @@
1
+ import * as Bytes from 'ox/Bytes'
2
+ import type * as Hex from 'ox/Hex'
3
+ import * as PublicKey from 'ox/PublicKey'
4
+ import * as WebAuthnP256 from 'ox/WebAuthnP256'
5
+
6
+ export type P256Credential = {
7
+ id: WebAuthnP256.P256Credential['id']
8
+ publicKey: Hex.Hex
9
+ raw: WebAuthnP256.P256Credential['raw']
10
+ }
11
+
12
+ /**
13
+ * Creates a WebAuthn credential (ie. a passkey).
14
+ *
15
+ * This function returns the credential object, which includes the public key.
16
+ * It is recommended to store the public key against the credential in an external store
17
+ * as it is not possible to extract a public key from a credential after it has been created.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * import { WebAuthnP256 } from 'tempo.ts/viem'
22
+ *
23
+ * const credential = await WebAuthnP256.createCredential({ name: 'Example' })
24
+ * // {
25
+ * // id: 'oZ48...',
26
+ * // publicKey: '0x...',
27
+ * // }
28
+ * ```
29
+ *
30
+ * @param parameters WebAuthnP256 createCredential options.
31
+ * @returns WebAuthn credential.
32
+ */
33
+ export async function createCredential(
34
+ parameters: createCredential.Parameters,
35
+ ): Promise<createCredential.ReturnValue> {
36
+ const { createFn, label, rpId, userId } = parameters
37
+ const credential = await WebAuthnP256.createCredential({
38
+ ...parameters,
39
+ authenticatorSelection: {
40
+ ...parameters.authenticatorSelection,
41
+ requireResidentKey: true,
42
+ residentKey: 'required',
43
+ userVerification: 'required',
44
+ },
45
+ createFn,
46
+ extensions: {
47
+ ...parameters.extensions,
48
+ credProps: true,
49
+ },
50
+ rp: rpId
51
+ ? {
52
+ id: rpId,
53
+ name: rpId,
54
+ }
55
+ : undefined,
56
+ name: undefined as never,
57
+ user: {
58
+ displayName: label,
59
+ id: new Uint8Array(userId ?? Bytes.fromString(label)),
60
+ name: label,
61
+ },
62
+ })
63
+ return {
64
+ id: credential.id,
65
+ publicKey: PublicKey.toHex(credential.publicKey, {
66
+ includePrefix: false,
67
+ }),
68
+ raw: credential.raw,
69
+ }
70
+ }
71
+
72
+ export declare namespace createCredential {
73
+ export type Parameters = Omit<
74
+ WebAuthnP256.createCredential.Options,
75
+ 'rp' | 'user'
76
+ > & {
77
+ /**
78
+ * Credential creation function. Useful for environments that do not support
79
+ * the WebAuthn API natively (i.e. React Native or testing environments).
80
+ *
81
+ * @default window.navigator.credentials.create
82
+ */
83
+ createFn?: WebAuthnP256.createCredential.Options['createFn'] | undefined
84
+ /** Label. */
85
+ label: string
86
+ /** Relying Party ID. */
87
+ rpId?: string | undefined
88
+ /** User ID. */
89
+ userId?: Bytes.Bytes | undefined
90
+ }
91
+
92
+ export type ReturnValue = P256Credential
93
+ }
94
+
95
+ /**
96
+ * Gets a WebAuthn credential (ie. a passkey), and optionally signs over a digest/hash.
97
+ *
98
+ * A `getPublicKey` function is required to fetch the public key paired with the credential
99
+ * from an external store. It is not possible to extract a public key from a credential after
100
+ * the credential has been created with `WebAuthnP256.createCredential`.
101
+ *
102
+ * @example
103
+ * ```ts
104
+ * import { WebAuthnP256 } from 'tempo.ts/viem'
105
+ *
106
+ * const credential = await WebAuthnP256.getCredential({
107
+ * async getPublicKey(credential) {
108
+ * // Get public key from store
109
+ * return store.getPublicKey(credential.id)
110
+ * }
111
+ * })
112
+ * ```
113
+ *
114
+ * @param parameters WebAuthnP256 getCredential options.
115
+ * @returns WebAuthn credential.
116
+ */
117
+ export async function getCredential(
118
+ parameters: getCredential.Parameters,
119
+ ): Promise<getCredential.ReturnValue> {
120
+ const { raw } = await WebAuthnP256.sign({
121
+ ...parameters,
122
+ challenge: parameters.hash ?? '0x',
123
+ })
124
+ const publicKey = await parameters.getPublicKey(raw)
125
+ return {
126
+ id: raw.id,
127
+ publicKey,
128
+ raw,
129
+ }
130
+ }
131
+
132
+ export declare namespace getCredential {
133
+ export type Parameters = Omit<WebAuthnP256.sign.Options, 'challenge'> & {
134
+ hash?: Hex.Hex | undefined
135
+ getPublicKey: (
136
+ credential: WebAuthnP256.P256Credential['raw'],
137
+ ) => Promise<Hex.Hex>
138
+ }
139
+ export type ReturnValue = P256Credential
140
+ }
@@ -0,0 +1 @@
1
+ export { createKeyPair } from 'ox/WebCryptoP256'