accounts 0.12.2 → 0.14.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 (128) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/README.md +6 -5
  3. package/dist/cli/adapter.d.ts.map +1 -1
  4. package/dist/cli/adapter.js +76 -69
  5. package/dist/cli/adapter.js.map +1 -1
  6. package/dist/core/AccessKey.d.ts +89 -106
  7. package/dist/core/AccessKey.d.ts.map +1 -1
  8. package/dist/core/AccessKey.js +230 -210
  9. package/dist/core/AccessKey.js.map +1 -1
  10. package/dist/core/Account.d.ts +0 -9
  11. package/dist/core/Account.d.ts.map +1 -1
  12. package/dist/core/Account.js +1 -13
  13. package/dist/core/Account.js.map +1 -1
  14. package/dist/core/Adapter.d.ts +24 -4
  15. package/dist/core/Adapter.d.ts.map +1 -1
  16. package/dist/core/Client.d.ts +1 -1
  17. package/dist/core/Client.d.ts.map +1 -1
  18. package/dist/core/Client.js.map +1 -1
  19. package/dist/core/Provider.d.ts +6 -7
  20. package/dist/core/Provider.d.ts.map +1 -1
  21. package/dist/core/Provider.js +73 -77
  22. package/dist/core/Provider.js.map +1 -1
  23. package/dist/core/Remote.d.ts +2 -0
  24. package/dist/core/Remote.d.ts.map +1 -1
  25. package/dist/core/Remote.js +3 -1
  26. package/dist/core/Remote.js.map +1 -1
  27. package/dist/core/Schema.d.ts +17 -7
  28. package/dist/core/Schema.d.ts.map +1 -1
  29. package/dist/core/adapters/dialog.d.ts.map +1 -1
  30. package/dist/core/adapters/dialog.js +126 -131
  31. package/dist/core/adapters/dialog.js.map +1 -1
  32. package/dist/core/adapters/local.d.ts.map +1 -1
  33. package/dist/core/adapters/local.js +92 -105
  34. package/dist/core/adapters/local.js.map +1 -1
  35. package/dist/core/adapters/privy.d.ts +164 -0
  36. package/dist/core/adapters/privy.d.ts.map +1 -0
  37. package/dist/core/adapters/privy.js +578 -0
  38. package/dist/core/adapters/privy.js.map +1 -0
  39. package/dist/core/adapters/turnkey.d.ts.map +1 -1
  40. package/dist/core/adapters/turnkey.js +136 -133
  41. package/dist/core/adapters/turnkey.js.map +1 -1
  42. package/dist/core/internal/AccessKeyTransaction.d.ts +59 -0
  43. package/dist/core/internal/AccessKeyTransaction.d.ts.map +1 -0
  44. package/dist/core/internal/AccessKeyTransaction.js +155 -0
  45. package/dist/core/internal/AccessKeyTransaction.js.map +1 -0
  46. package/dist/core/zod/rpc.d.ts +71 -4
  47. package/dist/core/zod/rpc.d.ts.map +1 -1
  48. package/dist/core/zod/rpc.js +33 -4
  49. package/dist/core/zod/rpc.js.map +1 -1
  50. package/dist/index.d.ts +1 -1
  51. package/dist/index.d.ts.map +1 -1
  52. package/dist/index.js +1 -1
  53. package/dist/index.js.map +1 -1
  54. package/dist/react-native/adapter.d.ts.map +1 -1
  55. package/dist/react-native/adapter.js +86 -75
  56. package/dist/react-native/adapter.js.map +1 -1
  57. package/dist/server/CliAuth.d.ts +55 -0
  58. package/dist/server/CliAuth.d.ts.map +1 -1
  59. package/dist/server/CliAuth.js +18 -1
  60. package/dist/server/CliAuth.js.map +1 -1
  61. package/dist/server/Kv.d.ts +19 -9
  62. package/dist/server/Kv.d.ts.map +1 -1
  63. package/dist/server/Kv.js +32 -9
  64. package/dist/server/Kv.js.map +1 -1
  65. package/dist/server/internal/handlers/auth.d.ts.map +1 -1
  66. package/dist/server/internal/handlers/auth.js +3 -1
  67. package/dist/server/internal/handlers/auth.js.map +1 -1
  68. package/dist/server/internal/handlers/codeAuth.js +1 -1
  69. package/dist/server/internal/handlers/codeAuth.js.map +1 -1
  70. package/dist/server/internal/handlers/exchange.js +1 -1
  71. package/dist/server/internal/handlers/exchange.js.map +1 -1
  72. package/dist/server/internal/handlers/relay.d.ts +18 -12
  73. package/dist/server/internal/handlers/relay.d.ts.map +1 -1
  74. package/dist/server/internal/handlers/relay.js +116 -3
  75. package/dist/server/internal/handlers/relay.js.map +1 -1
  76. package/dist/server/internal/handlers/webAuthn.d.ts +6 -1
  77. package/dist/server/internal/handlers/webAuthn.d.ts.map +1 -1
  78. package/dist/server/internal/handlers/webAuthn.js +16 -9
  79. package/dist/server/internal/handlers/webAuthn.js.map +1 -1
  80. package/package.json +3 -3
  81. package/src/cli/{Provider.test.ts → Provider.localnet.test.ts} +49 -1
  82. package/src/cli/adapter.ts +95 -76
  83. package/src/core/AccessKey.test.ts +563 -560
  84. package/src/core/AccessKey.ts +327 -327
  85. package/src/core/Account.test.ts +6 -51
  86. package/src/core/Account.ts +1 -19
  87. package/src/core/Adapter.ts +22 -4
  88. package/src/core/Client.test.ts +1 -1
  89. package/src/core/Client.ts +1 -1
  90. package/src/core/Provider.connect.browser.test.ts +1 -1
  91. package/src/core/{Provider.test.ts → Provider.localnet.test.ts} +98 -32
  92. package/src/core/Provider.ts +118 -112
  93. package/src/core/Remote.test.ts +28 -0
  94. package/src/core/Remote.ts +4 -1
  95. package/src/core/Schema.test-d.ts +41 -0
  96. package/src/core/adapters/dialog.test.ts +335 -15
  97. package/src/core/adapters/dialog.ts +120 -127
  98. package/src/core/adapters/local.test.ts +1 -1
  99. package/src/core/adapters/local.ts +95 -119
  100. package/src/core/adapters/privy.test-d.ts +128 -0
  101. package/src/core/adapters/privy.test.ts +648 -0
  102. package/src/core/adapters/privy.ts +811 -0
  103. package/src/core/adapters/turnkey.test.ts +117 -13
  104. package/src/core/adapters/turnkey.ts +132 -149
  105. package/src/core/internal/AccessKeyTransaction.ts +243 -0
  106. package/src/core/{mppx.test.ts → mppx.localnet.test.ts} +32 -2
  107. package/src/core/zod/request.test.ts +36 -0
  108. package/src/core/zod/rpc.test.ts +139 -0
  109. package/src/core/zod/rpc.ts +44 -10
  110. package/src/index.ts +1 -1
  111. package/src/react-native/{Provider.test.ts → Provider.localnet.test.ts} +65 -0
  112. package/src/react-native/adapter.ts +107 -87
  113. package/src/server/CliAuth.test-d.ts +23 -0
  114. package/src/server/CliAuth.test.ts +47 -0
  115. package/src/server/CliAuth.ts +20 -1
  116. package/src/server/Handler.test-d.ts +1 -1
  117. package/src/server/Kv.test.ts +51 -0
  118. package/src/server/Kv.ts +43 -9
  119. package/src/server/internal/handlers/auth.test.ts +10 -5
  120. package/src/server/internal/handlers/auth.ts +4 -1
  121. package/src/server/internal/handlers/codeAuth.ts +1 -1
  122. package/src/server/internal/handlers/exchange.ts +1 -1
  123. package/src/server/internal/handlers/{relay.test.ts → relay.localnet.test.ts} +4 -1
  124. package/src/server/internal/handlers/relay.ts +139 -14
  125. package/src/server/internal/handlers/session.test.ts +1 -1
  126. package/src/server/internal/handlers/webAuthn.test.ts +20 -0
  127. package/src/server/internal/handlers/webAuthn.ts +24 -12
  128. /package/src/server/internal/handlers/{exchange.test.ts → exchange.localnet.test.ts} +0 -0
@@ -11,12 +11,12 @@ import {
11
11
  RpcResponse,
12
12
  } from 'ox'
13
13
  import { KeyAuthorization } from 'ox/tempo'
14
- import { prepareTransactionRequest } from 'viem/actions'
15
14
  import { Account as TempoAccount, Secp256k1 } from 'viem/tempo'
16
15
  import * as z from 'zod/mini'
17
16
 
18
17
  import * as AccessKey from '../core/AccessKey.js'
19
18
  import * as Adapter from '../core/Adapter.js'
19
+ import * as AccessKeyTransaction from '../core/internal/AccessKeyTransaction.js'
20
20
  import * as CliAuth from '../server/CliAuth.js'
21
21
  import * as Keyring from './keyring.js'
22
22
 
@@ -44,9 +44,9 @@ export function cli(options: cli.Options): Adapter.Adapter {
44
44
  const deserialized = KeyAuthorization.deserialize(entry.keyAuthorization)
45
45
  if (!deserialized.signature) throw new Error('Managed access key is missing a signature.')
46
46
  const keyAuthorization = deserialized as KeyAuthorization.Signed
47
- AccessKey.save({
48
- address,
49
- keyAuthorization,
47
+ AccessKey.add({
48
+ account: address,
49
+ authorization: keyAuthorization,
50
50
  privateKey: entry.key,
51
51
  store,
52
52
  })
@@ -104,9 +104,9 @@ export function cli(options: cli.Options): Adapter.Adapter {
104
104
  if (!managedKey) return
105
105
 
106
106
  const signed = KeyAuthorization.fromRpc(z.encode(CliAuth.keyAuthorization, keyAuthorization))
107
- AccessKey.save({
108
- address,
109
- keyAuthorization: signed,
107
+ AccessKey.add({
108
+ account: address,
109
+ authorization: signed,
110
110
  privateKey: managedKey.key,
111
111
  store,
112
112
  })
@@ -129,29 +129,42 @@ export function cli(options: cli.Options): Adapter.Adapter {
129
129
  )
130
130
  }
131
131
 
132
- async function withManagedAccessKey<result>(
133
- fn: (
134
- account: TempoAccount.Account,
135
- keyAuthorization?: KeyAuthorization.Signed | undefined,
136
- ) => Promise<result>,
132
+ async function prepareManagedTransaction(
133
+ client: ReturnType<typeof getClient>,
134
+ parameters: AccessKeyTransaction.create.PrepareParameters,
135
+ options: {
136
+ calls?: AccessKeyTransaction.create.Options['calls'] | undefined
137
+ chainId?: number | undefined
138
+ } = {},
137
139
  ) {
138
- const rootAddress = store.getState().accounts[store.getState().activeAccount]?.address
139
- if (rootAddress) await loadManagedKey(rootAddress)
140
+ const state = store.getState()
141
+ const address = parameters.from ?? state.accounts[state.activeAccount]?.address
142
+ if (!address) throw new core_Provider.DisconnectedError({ message: 'No active account.' })
143
+ await loadManagedKey(address)
144
+ const transaction = await AccessKeyTransaction.create({
145
+ address,
146
+ calls: options.calls,
147
+ chainId: options.chainId ?? state.chainId,
148
+ client,
149
+ store,
150
+ })
151
+ if (!transaction)
152
+ throw new core_Provider.UnauthorizedError({
153
+ message: `Account "${address}" cannot sign with an access key.`,
154
+ })
155
+ return await transaction.prepare(parameters)
156
+ }
140
157
 
141
- const account = getAccount({ signable: true })
142
- const keyAuthorization = AccessKey.getPending(account, { store })
143
- try {
144
- return await fn(account, keyAuthorization ?? undefined)
145
- } catch (error) {
146
- AccessKey.remove(account, { store })
147
- throw error
148
- }
158
+ async function loadManagedAccount(address: Adapter.signPersonalMessage.Parameters['address']) {
159
+ await loadManagedKey(address)
160
+ return getAccount({ address, signable: true })
149
161
  }
150
162
 
151
163
  async function authorize(request: {
152
164
  account?: Adapter.authorizeAccessKey.ReturnType['rootAddress'] | undefined
153
165
  authorizeAccessKey: Adapter.authorizeAccessKey.Parameters | undefined
154
166
  method: 'wallet_authorizeAccessKey' | 'wallet_connect'
167
+ showDeposit?: Adapter.createAccount.Parameters['showDeposit'] | undefined
155
168
  }) {
156
169
  const {
157
170
  host,
@@ -192,6 +205,7 @@ export function cli(options: cli.Options): Adapter.Adapter {
192
205
  ...(keyType ? { keyType } : {}),
193
206
  ...(authorizeAccessKey?.limits ? { limits: authorizeAccessKey.limits } : {}),
194
207
  pubKey: publicKey,
208
+ ...(request.showDeposit !== undefined ? { showDeposit: request.showDeposit } : {}),
195
209
  }
196
210
  const created = await post({
197
211
  body,
@@ -257,8 +271,27 @@ export function cli(options: cli.Options): Adapter.Adapter {
257
271
  rootAddress: result.accountAddress,
258
272
  }
259
273
  },
260
- async createAccount(params, request) {
261
- return this.loadAccounts(params, request)
274
+ async createAccount(parameters) {
275
+ if (parameters?.digest)
276
+ throw unsupported('`wallet_connect` digest signing not supported by CLI adapter.')
277
+
278
+ const result = await authorize({
279
+ authorizeAccessKey: parameters?.authorizeAccessKey,
280
+ method: 'wallet_connect',
281
+ ...(parameters?.showDeposit !== undefined
282
+ ? { showDeposit: parameters.showDeposit }
283
+ : {}),
284
+ })
285
+
286
+ return {
287
+ accounts: [
288
+ {
289
+ address: result.accountAddress,
290
+ capabilities: {},
291
+ },
292
+ ],
293
+ keyAuthorization: z.encode(CliAuth.keyAuthorization, result.keyAuthorization),
294
+ }
262
295
  },
263
296
  async loadAccounts(parameters) {
264
297
  if (parameters?.digest)
@@ -267,6 +300,9 @@ export function cli(options: cli.Options): Adapter.Adapter {
267
300
  const result = await authorize({
268
301
  authorizeAccessKey: parameters?.authorizeAccessKey,
269
302
  method: 'wallet_connect',
303
+ ...(parameters?.showDeposit !== undefined
304
+ ? { showDeposit: parameters.showDeposit }
305
+ : {}),
270
306
  })
271
307
 
272
308
  return {
@@ -285,74 +321,57 @@ export function cli(options: cli.Options): Adapter.Adapter {
285
321
  async sendTransaction(parameters) {
286
322
  const { feePayer, ...rest } = parameters
287
323
  const client = getClient(typeof feePayer === 'string' ? { feePayer } : {})
288
- const { account, prepared } = await withManagedAccessKey(
289
- async (account, keyAuthorization) => ({
290
- account,
291
- prepared: await prepareTransactionRequest(client, {
292
- account,
293
- ...rest,
294
- ...(feePayer ? { feePayer: true } : {}),
295
- ...(keyAuthorization ? { keyAuthorization } : {}),
296
- type: 'tempo',
297
- } as never),
298
- }),
324
+ const prepared = await prepareManagedTransaction(
325
+ client,
326
+ {
327
+ ...rest,
328
+ ...(feePayer ? { feePayer: true } : {}),
329
+ },
330
+ {
331
+ calls: parameters.calls as AccessKeyTransaction.create.Options['calls'],
332
+ chainId: parameters.chainId,
333
+ },
299
334
  )
300
- const signed = await account.signTransaction(prepared as never)
301
- const result = await client.request({
302
- method: 'eth_sendRawTransaction' as never,
303
- params: [signed],
304
- })
305
- AccessKey.removePending(account, { store })
306
- return result
335
+ return await prepared.send()
307
336
  },
308
337
  async sendTransactionSync(parameters) {
309
338
  const { feePayer, ...rest } = parameters
310
339
  const client = getClient(typeof feePayer === 'string' ? { feePayer } : {})
311
- const { account, prepared } = await withManagedAccessKey(
312
- async (account, keyAuthorization) => ({
313
- account,
314
- prepared: await prepareTransactionRequest(client, {
315
- account,
316
- ...rest,
317
- ...(feePayer ? { feePayer: true } : {}),
318
- ...(keyAuthorization ? { keyAuthorization } : {}),
319
- type: 'tempo',
320
- } as never),
321
- }),
340
+ const prepared = await prepareManagedTransaction(
341
+ client,
342
+ {
343
+ ...rest,
344
+ ...(feePayer ? { feePayer: true } : {}),
345
+ },
346
+ {
347
+ calls: parameters.calls as AccessKeyTransaction.create.Options['calls'],
348
+ chainId: parameters.chainId,
349
+ },
322
350
  )
323
- const signed = await account.signTransaction(prepared as never)
324
- const result = await client.request({
325
- method: 'eth_sendRawTransactionSync' as never,
326
- params: [signed],
327
- })
328
- AccessKey.removePending(account, { store })
329
- return result
351
+ return await prepared.sendSync()
330
352
  },
331
353
  async signPersonalMessage({ address, data }) {
332
- await loadManagedKey(address)
333
- const account = getAccount({ address, signable: true })
354
+ const account = await loadManagedAccount(address)
334
355
  return await account.signMessage({ message: { raw: data } })
335
356
  },
336
357
  async signTransaction(parameters) {
337
358
  const { feePayer, ...rest } = parameters
338
359
  const client = getClient(typeof feePayer === 'string' ? { feePayer } : {})
339
- const { account, prepared } = await withManagedAccessKey(
340
- async (account, keyAuthorization) => ({
341
- account,
342
- prepared: await prepareTransactionRequest(client, {
343
- account,
344
- ...rest,
345
- ...(feePayer ? { feePayer: true } : {}),
346
- ...(keyAuthorization ? { keyAuthorization } : {}),
347
- type: 'tempo',
348
- } as never),
349
- }),
360
+ const prepared = await prepareManagedTransaction(
361
+ client,
362
+ {
363
+ ...rest,
364
+ ...(feePayer ? { feePayer: true } : {}),
365
+ },
366
+ {
367
+ calls: parameters.calls as AccessKeyTransaction.create.Options['calls'],
368
+ chainId: parameters.chainId,
369
+ },
350
370
  )
351
- return await account.signTransaction(prepared as never)
371
+ return await prepared.sign()
352
372
  },
353
373
  async signTypedData({ address, data }) {
354
- await loadManagedKey(address)
355
- const account = getAccount({ address, signable: true })
374
+ const account = await loadManagedAccount(address)
356
375
  return await account.signTypedData(JSON.parse(data) as never)
357
376
  },
358
377
  },