@sip-protocol/sdk 0.3.1 → 0.4.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.
- package/dist/browser.d.mts +2 -2
- package/dist/browser.d.ts +2 -2
- package/dist/browser.js +1028 -146
- package/dist/browser.mjs +49 -1
- package/dist/chunk-AOZIY3GU.mjs +12995 -0
- package/dist/chunk-BCLIX5T2.mjs +12940 -0
- package/dist/chunk-EU4UEWWG.mjs +12164 -0
- package/dist/chunk-FKXPHKYD.mjs +12955 -0
- package/dist/chunk-OPQ2GQIO.mjs +13013 -0
- package/dist/index-BcWNakUD.d.ts +7990 -0
- package/dist/index-BsKY3Hr0.d.mts +7990 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +999 -117
- package/dist/index.mjs +49 -1
- package/package.json +2 -1
- package/src/adapters/near-intents.ts +8 -0
- package/src/bitcoin/index.ts +51 -0
- package/src/bitcoin/silent-payments.ts +865 -0
- package/src/bitcoin/taproot.ts +590 -0
- package/src/cosmos/ibc-stealth.ts +825 -0
- package/src/cosmos/index.ts +83 -0
- package/src/cosmos/stealth.ts +487 -0
- package/src/index.ts +51 -0
- package/src/move/aptos.ts +369 -0
- package/src/move/index.ts +35 -0
- package/src/move/sui.ts +367 -0
- package/src/oracle/types.ts +8 -0
- package/src/settlement/backends/direct-chain.ts +8 -0
- package/src/settlement/backends/near-intents.ts +11 -0
- package/src/stealth.ts +3 -3
- package/src/validation.ts +42 -1
- package/src/wallet/aptos/adapter.ts +422 -0
- package/src/wallet/aptos/index.ts +10 -0
- package/src/wallet/aptos/mock.ts +410 -0
- package/src/wallet/aptos/types.ts +278 -0
- package/src/wallet/bitcoin/adapter.ts +470 -0
- package/src/wallet/bitcoin/index.ts +38 -0
- package/src/wallet/bitcoin/mock.ts +516 -0
- package/src/wallet/bitcoin/types.ts +274 -0
- package/src/wallet/cosmos/adapter.ts +484 -0
- package/src/wallet/cosmos/index.ts +63 -0
- package/src/wallet/cosmos/mock.ts +596 -0
- package/src/wallet/cosmos/types.ts +462 -0
- package/src/wallet/index.ts +127 -0
- package/src/wallet/sui/adapter.ts +471 -0
- package/src/wallet/sui/index.ts +10 -0
- package/src/wallet/sui/mock.ts +439 -0
- package/src/wallet/sui/types.ts +245 -0
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mock Aptos Wallet Adapter
|
|
3
|
+
*
|
|
4
|
+
* Testing implementation of Aptos wallet adapter.
|
|
5
|
+
* Provides full mock functionality without requiring browser environment.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type {
|
|
9
|
+
HexString,
|
|
10
|
+
Asset,
|
|
11
|
+
Signature,
|
|
12
|
+
UnsignedTransaction,
|
|
13
|
+
SignedTransaction,
|
|
14
|
+
TransactionReceipt,
|
|
15
|
+
} from '@sip-protocol/types'
|
|
16
|
+
import { WalletErrorCode } from '@sip-protocol/types'
|
|
17
|
+
import { BaseWalletAdapter } from '../base-adapter'
|
|
18
|
+
import { WalletError } from '../errors'
|
|
19
|
+
import type {
|
|
20
|
+
AptosWalletProvider,
|
|
21
|
+
AptosAccountInfo,
|
|
22
|
+
AptosTransaction,
|
|
23
|
+
AptosSignMessagePayload,
|
|
24
|
+
AptosSignMessageResponse,
|
|
25
|
+
} from './types'
|
|
26
|
+
import { aptosPublicKeyToHex, getDefaultAptosRpcEndpoint } from './types'
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Configuration for mock Aptos adapter
|
|
30
|
+
*/
|
|
31
|
+
export interface MockAptosAdapterConfig {
|
|
32
|
+
/** Mock address (0x-prefixed hex) */
|
|
33
|
+
address?: string
|
|
34
|
+
/** Mock public key (0x-prefixed hex) */
|
|
35
|
+
publicKey?: string
|
|
36
|
+
/** Mock balance in Octas */
|
|
37
|
+
balance?: bigint
|
|
38
|
+
/** Token balances by coin type */
|
|
39
|
+
tokenBalances?: Record<string, bigint>
|
|
40
|
+
/** Whether to simulate connection failure */
|
|
41
|
+
shouldFailConnect?: boolean
|
|
42
|
+
/** Whether to simulate signing failure */
|
|
43
|
+
shouldFailSign?: boolean
|
|
44
|
+
/** Whether to simulate transaction failure */
|
|
45
|
+
shouldFailTransaction?: boolean
|
|
46
|
+
/** Simulated network */
|
|
47
|
+
network?: string
|
|
48
|
+
/** Simulated latency in ms */
|
|
49
|
+
latency?: number
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Mock Aptos wallet adapter for testing
|
|
54
|
+
*
|
|
55
|
+
* Provides full Aptos wallet functionality with mock data.
|
|
56
|
+
* No browser environment or actual wallet required.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* const wallet = new MockAptosAdapter({
|
|
61
|
+
* address: '0x1234...abcd',
|
|
62
|
+
* balance: 100_000_000n, // 1 APT
|
|
63
|
+
* })
|
|
64
|
+
*
|
|
65
|
+
* await wallet.connect()
|
|
66
|
+
* const balance = await wallet.getBalance() // 100_000_000n
|
|
67
|
+
*
|
|
68
|
+
* // Simulate failures
|
|
69
|
+
* const failingWallet = new MockAptosAdapter({
|
|
70
|
+
* shouldFailSign: true,
|
|
71
|
+
* })
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export class MockAptosAdapter extends BaseWalletAdapter {
|
|
75
|
+
readonly chain = 'aptos' as const
|
|
76
|
+
readonly name = 'mock-aptos'
|
|
77
|
+
|
|
78
|
+
private mockAddress: HexString
|
|
79
|
+
private mockPublicKey: HexString
|
|
80
|
+
private mockBalance: bigint
|
|
81
|
+
private mockTokenBalances: Map<string, bigint>
|
|
82
|
+
private shouldFailConnect: boolean
|
|
83
|
+
private shouldFailSign: boolean
|
|
84
|
+
private shouldFailTransaction: boolean
|
|
85
|
+
private network: string
|
|
86
|
+
private latency: number
|
|
87
|
+
|
|
88
|
+
// Track signed transactions for verification
|
|
89
|
+
private signedTransactions: AptosTransaction[] = []
|
|
90
|
+
private sentTransactions: string[] = []
|
|
91
|
+
|
|
92
|
+
constructor(config: MockAptosAdapterConfig = {}) {
|
|
93
|
+
super()
|
|
94
|
+
this.mockAddress = (config.address ?? '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef') as HexString
|
|
95
|
+
this.mockPublicKey = (config.publicKey ?? '0xabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd') as HexString
|
|
96
|
+
this.mockBalance = config.balance ?? 100_000_000n // 1 APT default
|
|
97
|
+
this.mockTokenBalances = new Map(Object.entries(config.tokenBalances ?? {}))
|
|
98
|
+
this.shouldFailConnect = config.shouldFailConnect ?? false
|
|
99
|
+
this.shouldFailSign = config.shouldFailSign ?? false
|
|
100
|
+
this.shouldFailTransaction = config.shouldFailTransaction ?? false
|
|
101
|
+
this.network = config.network ?? 'mainnet'
|
|
102
|
+
this.latency = config.latency ?? 10
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Get the current Aptos network
|
|
107
|
+
*/
|
|
108
|
+
getNetwork(): string {
|
|
109
|
+
return this.network
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Get RPC endpoint
|
|
114
|
+
*/
|
|
115
|
+
getRpcEndpoint(): string {
|
|
116
|
+
return getDefaultAptosRpcEndpoint(this.network)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Connect to the mock wallet
|
|
121
|
+
*/
|
|
122
|
+
async connect(): Promise<void> {
|
|
123
|
+
this._connectionState = 'connecting'
|
|
124
|
+
|
|
125
|
+
// Simulate network latency
|
|
126
|
+
await this.simulateLatency()
|
|
127
|
+
|
|
128
|
+
if (this.shouldFailConnect) {
|
|
129
|
+
this.setError(WalletErrorCode.CONNECTION_FAILED, 'Mock connection failure')
|
|
130
|
+
throw new WalletError('Mock connection failure', WalletErrorCode.CONNECTION_FAILED)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
this.setConnected(this.mockAddress, this.mockPublicKey)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Disconnect from the mock wallet
|
|
138
|
+
*/
|
|
139
|
+
async disconnect(): Promise<void> {
|
|
140
|
+
await this.simulateLatency()
|
|
141
|
+
this.setDisconnected('User disconnected')
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Sign a message
|
|
146
|
+
*/
|
|
147
|
+
async signMessage(message: Uint8Array): Promise<Signature> {
|
|
148
|
+
this.requireConnected()
|
|
149
|
+
await this.simulateLatency()
|
|
150
|
+
|
|
151
|
+
if (this.shouldFailSign) {
|
|
152
|
+
throw new WalletError('Mock signing failure', WalletErrorCode.SIGNING_REJECTED)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Create deterministic mock signature (64 bytes)
|
|
156
|
+
const mockSig = new Uint8Array(64)
|
|
157
|
+
for (let i = 0; i < 64; i++) {
|
|
158
|
+
mockSig[i] = (message[i % message.length] ?? 0) ^ (i * 7) ^ this.mockAddress.charCodeAt(i % this.mockAddress.length)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
signature: ('0x' + Buffer.from(mockSig).toString('hex')) as HexString,
|
|
163
|
+
publicKey: this._publicKey as HexString,
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Sign a transaction
|
|
169
|
+
*/
|
|
170
|
+
async signTransaction(tx: UnsignedTransaction): Promise<SignedTransaction> {
|
|
171
|
+
this.requireConnected()
|
|
172
|
+
await this.simulateLatency()
|
|
173
|
+
|
|
174
|
+
if (this.shouldFailSign) {
|
|
175
|
+
throw new WalletError('Mock signing failure', WalletErrorCode.SIGNING_REJECTED)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const aptosTx = tx.data as AptosTransaction
|
|
179
|
+
this.signedTransactions.push(aptosTx)
|
|
180
|
+
|
|
181
|
+
// Create mock signed bytes
|
|
182
|
+
const txDataStr = JSON.stringify(tx.data)
|
|
183
|
+
const signature = await this.signMessage(new TextEncoder().encode(txDataStr))
|
|
184
|
+
|
|
185
|
+
return {
|
|
186
|
+
unsigned: tx,
|
|
187
|
+
signatures: [signature],
|
|
188
|
+
serialized: signature.signature,
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Sign and send a transaction
|
|
194
|
+
*/
|
|
195
|
+
async signAndSendTransaction(tx: UnsignedTransaction): Promise<TransactionReceipt> {
|
|
196
|
+
this.requireConnected()
|
|
197
|
+
await this.simulateLatency()
|
|
198
|
+
|
|
199
|
+
if (this.shouldFailSign) {
|
|
200
|
+
throw new WalletError('Mock signing failure', WalletErrorCode.SIGNING_REJECTED)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (this.shouldFailTransaction) {
|
|
204
|
+
throw new WalletError('Mock transaction failure', WalletErrorCode.TRANSACTION_FAILED)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Generate mock transaction hash
|
|
208
|
+
const txHash = `0x${Date.now().toString(16)}${Math.random().toString(16).slice(2)}`
|
|
209
|
+
this.sentTransactions.push(txHash)
|
|
210
|
+
|
|
211
|
+
return {
|
|
212
|
+
txHash: txHash as HexString,
|
|
213
|
+
status: 'confirmed',
|
|
214
|
+
blockNumber: BigInt(Math.floor(Math.random() * 1000000)),
|
|
215
|
+
timestamp: Date.now(),
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Get native APT balance
|
|
221
|
+
*/
|
|
222
|
+
async getBalance(): Promise<bigint> {
|
|
223
|
+
this.requireConnected()
|
|
224
|
+
await this.simulateLatency()
|
|
225
|
+
return this.mockBalance
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Get token balance
|
|
230
|
+
*/
|
|
231
|
+
async getTokenBalance(asset: Asset): Promise<bigint> {
|
|
232
|
+
this.requireConnected()
|
|
233
|
+
await this.simulateLatency()
|
|
234
|
+
|
|
235
|
+
if (asset.chain !== 'aptos') {
|
|
236
|
+
throw new WalletError(
|
|
237
|
+
`Asset chain ${asset.chain} not supported by Aptos adapter`,
|
|
238
|
+
WalletErrorCode.UNSUPPORTED_CHAIN
|
|
239
|
+
)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Native APT
|
|
243
|
+
if (!asset.address || asset.address === '0x1::aptos_coin::AptosCoin') {
|
|
244
|
+
return this.mockBalance
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const coinType = asset.address
|
|
248
|
+
return this.mockTokenBalances.get(coinType) ?? 0n
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ── Mock Control Methods ──────────────────────────────────────────────────
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Set mock balance
|
|
255
|
+
*/
|
|
256
|
+
setMockBalance(balance: bigint): void {
|
|
257
|
+
this.mockBalance = balance
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Set mock token balance
|
|
262
|
+
*/
|
|
263
|
+
setMockTokenBalance(coinType: string, balance: bigint): void {
|
|
264
|
+
this.mockTokenBalances.set(coinType, balance)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Get all signed transactions (for verification)
|
|
269
|
+
*/
|
|
270
|
+
getSignedTransactions(): AptosTransaction[] {
|
|
271
|
+
return [...this.signedTransactions]
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Get all sent transaction hashes (for verification)
|
|
276
|
+
*/
|
|
277
|
+
getSentTransactions(): string[] {
|
|
278
|
+
return [...this.sentTransactions]
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Clear transaction history
|
|
283
|
+
*/
|
|
284
|
+
clearTransactionHistory(): void {
|
|
285
|
+
this.signedTransactions = []
|
|
286
|
+
this.sentTransactions = []
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Simulate an account change event
|
|
291
|
+
*/
|
|
292
|
+
simulateAccountChange(newAddress: HexString, newPublicKey?: HexString): void {
|
|
293
|
+
const previousAddress = this._address
|
|
294
|
+
this.mockAddress = newAddress
|
|
295
|
+
this._address = newAddress as string
|
|
296
|
+
|
|
297
|
+
if (newPublicKey) {
|
|
298
|
+
this.mockPublicKey = newPublicKey
|
|
299
|
+
this._publicKey = newPublicKey
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
this.emitAccountChanged(previousAddress, newAddress)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Simulate a disconnect event
|
|
307
|
+
*/
|
|
308
|
+
simulateDisconnect(): void {
|
|
309
|
+
this.setDisconnected('Simulated disconnect')
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
private async simulateLatency(): Promise<void> {
|
|
313
|
+
if (this.latency > 0) {
|
|
314
|
+
await new Promise((resolve) => setTimeout(resolve, this.latency))
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Create a mock Aptos wallet provider for testing real adapter
|
|
321
|
+
*/
|
|
322
|
+
export function createMockAptosProvider(
|
|
323
|
+
config: MockAptosAdapterConfig = {}
|
|
324
|
+
): AptosWalletProvider {
|
|
325
|
+
const address = config.address ?? '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'
|
|
326
|
+
const publicKey = config.publicKey ?? '0xabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd'
|
|
327
|
+
const network = config.network ?? 'mainnet'
|
|
328
|
+
|
|
329
|
+
const account: AptosAccountInfo = {
|
|
330
|
+
address,
|
|
331
|
+
publicKey,
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return {
|
|
335
|
+
async connect(): Promise<AptosAccountInfo> {
|
|
336
|
+
if (config.shouldFailConnect) {
|
|
337
|
+
throw new Error('User rejected the request')
|
|
338
|
+
}
|
|
339
|
+
return account
|
|
340
|
+
},
|
|
341
|
+
|
|
342
|
+
async disconnect(): Promise<void> {
|
|
343
|
+
// No-op for mock
|
|
344
|
+
},
|
|
345
|
+
|
|
346
|
+
async account(): Promise<AptosAccountInfo> {
|
|
347
|
+
return account
|
|
348
|
+
},
|
|
349
|
+
|
|
350
|
+
async network(): Promise<string> {
|
|
351
|
+
return network
|
|
352
|
+
},
|
|
353
|
+
|
|
354
|
+
async signTransaction(transaction: AptosTransaction): Promise<Uint8Array> {
|
|
355
|
+
if (config.shouldFailSign) {
|
|
356
|
+
throw new Error('User rejected the request')
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// Return mock signed bytes
|
|
360
|
+
const mockSig = new Uint8Array(64)
|
|
361
|
+
for (let i = 0; i < 64; i++) {
|
|
362
|
+
mockSig[i] = i
|
|
363
|
+
}
|
|
364
|
+
return mockSig
|
|
365
|
+
},
|
|
366
|
+
|
|
367
|
+
async signAndSubmitTransaction(transaction: AptosTransaction): Promise<{ hash: string }> {
|
|
368
|
+
if (config.shouldFailSign) {
|
|
369
|
+
throw new Error('User rejected the request')
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
if (config.shouldFailTransaction) {
|
|
373
|
+
throw new Error('Transaction failed')
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const hash = `0x${Date.now().toString(16)}${Math.random().toString(16).slice(2)}`
|
|
377
|
+
return { hash }
|
|
378
|
+
},
|
|
379
|
+
|
|
380
|
+
async signMessage(payload: AptosSignMessagePayload): Promise<AptosSignMessageResponse> {
|
|
381
|
+
if (config.shouldFailSign) {
|
|
382
|
+
throw new Error('User rejected the request')
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const mockSig = new Uint8Array(64)
|
|
386
|
+
for (let i = 0; i < 64; i++) {
|
|
387
|
+
mockSig[i] = payload.message.charCodeAt(i % payload.message.length) ^ i
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
return {
|
|
391
|
+
signature: Buffer.from(mockSig).toString('hex'),
|
|
392
|
+
fullMessage: payload.message,
|
|
393
|
+
address: payload.address ? address : undefined,
|
|
394
|
+
}
|
|
395
|
+
},
|
|
396
|
+
|
|
397
|
+
async isConnected(): Promise<boolean> {
|
|
398
|
+
return true
|
|
399
|
+
},
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Create a mock Aptos adapter
|
|
405
|
+
*/
|
|
406
|
+
export function createMockAptosAdapter(
|
|
407
|
+
config: MockAptosAdapterConfig = {}
|
|
408
|
+
): MockAptosAdapter {
|
|
409
|
+
return new MockAptosAdapter(config)
|
|
410
|
+
}
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Aptos Wallet Types
|
|
3
|
+
*
|
|
4
|
+
* Type definitions for Aptos wallet adapters.
|
|
5
|
+
* Supports Petra, Martian, Pontem, and other Aptos wallets.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { HexString } from '@sip-protocol/types'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Supported Aptos wallet names
|
|
12
|
+
*/
|
|
13
|
+
export type AptosWalletName = 'petra' | 'martian' | 'pontem' | 'generic'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Aptos network configuration
|
|
17
|
+
*/
|
|
18
|
+
export interface AptosNetwork {
|
|
19
|
+
name: string
|
|
20
|
+
chainId: string
|
|
21
|
+
url?: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Aptos account information
|
|
26
|
+
*/
|
|
27
|
+
export interface AptosAccountInfo {
|
|
28
|
+
/** Account address (0x-prefixed hex, 64 characters) */
|
|
29
|
+
address: string
|
|
30
|
+
/** Public key (0x-prefixed hex) */
|
|
31
|
+
publicKey: string
|
|
32
|
+
/** Authentication key (optional) */
|
|
33
|
+
authKey?: string
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Aptos transaction payload
|
|
38
|
+
*/
|
|
39
|
+
export interface AptosTransactionPayload {
|
|
40
|
+
/** Transaction type */
|
|
41
|
+
type: 'entry_function_payload' | 'script_payload' | 'module_bundle_payload'
|
|
42
|
+
/** Function identifier (e.g., "0x1::coin::transfer") */
|
|
43
|
+
function?: string
|
|
44
|
+
/** Type arguments */
|
|
45
|
+
type_arguments?: string[]
|
|
46
|
+
/** Function arguments */
|
|
47
|
+
arguments?: any[]
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Aptos transaction options
|
|
52
|
+
*/
|
|
53
|
+
export interface AptosTransactionOptions {
|
|
54
|
+
/** Maximum gas amount */
|
|
55
|
+
max_gas_amount?: string
|
|
56
|
+
/** Gas unit price */
|
|
57
|
+
gas_unit_price?: string
|
|
58
|
+
/** Expiration timestamp (seconds) */
|
|
59
|
+
expiration_timestamp_secs?: string
|
|
60
|
+
/** Sequence number */
|
|
61
|
+
sequence_number?: string
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Aptos transaction object
|
|
66
|
+
*/
|
|
67
|
+
export interface AptosTransaction {
|
|
68
|
+
/** Transaction payload */
|
|
69
|
+
payload: AptosTransactionPayload
|
|
70
|
+
/** Transaction options */
|
|
71
|
+
options?: AptosTransactionOptions
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Signed Aptos transaction
|
|
76
|
+
*/
|
|
77
|
+
export interface SignedAptosTransaction {
|
|
78
|
+
/** Transaction hash */
|
|
79
|
+
hash: string
|
|
80
|
+
/** Signed transaction bytes (hex) */
|
|
81
|
+
signature: string
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Aptos sign message payload
|
|
86
|
+
*/
|
|
87
|
+
export interface AptosSignMessagePayload {
|
|
88
|
+
/** Message to sign */
|
|
89
|
+
message: string
|
|
90
|
+
/** Nonce for replay protection */
|
|
91
|
+
nonce: string
|
|
92
|
+
/** Include address in response */
|
|
93
|
+
address?: boolean
|
|
94
|
+
/** Include application info */
|
|
95
|
+
application?: boolean
|
|
96
|
+
/** Include chain ID */
|
|
97
|
+
chainId?: boolean
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Aptos sign message response
|
|
102
|
+
*/
|
|
103
|
+
export interface AptosSignMessageResponse {
|
|
104
|
+
/** Signature (hex) */
|
|
105
|
+
signature: string
|
|
106
|
+
/** Full message that was signed */
|
|
107
|
+
fullMessage: string
|
|
108
|
+
/** Message prefix (if any) */
|
|
109
|
+
prefix?: string
|
|
110
|
+
/** Address that signed (if requested) */
|
|
111
|
+
address?: string
|
|
112
|
+
/** Application info (if requested) */
|
|
113
|
+
application?: string
|
|
114
|
+
/** Chain ID (if requested) */
|
|
115
|
+
chainId?: number
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Petra wallet API interface
|
|
120
|
+
*
|
|
121
|
+
* @see https://petra.app/docs/api
|
|
122
|
+
*/
|
|
123
|
+
export interface PetraAPI {
|
|
124
|
+
/**
|
|
125
|
+
* Connect to the wallet
|
|
126
|
+
*/
|
|
127
|
+
connect(): Promise<AptosAccountInfo>
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Disconnect from the wallet
|
|
131
|
+
*/
|
|
132
|
+
disconnect(): Promise<void>
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Get current account
|
|
136
|
+
*/
|
|
137
|
+
account(): Promise<AptosAccountInfo>
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Get current network
|
|
141
|
+
*/
|
|
142
|
+
network(): Promise<string>
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Sign a transaction
|
|
146
|
+
*/
|
|
147
|
+
signTransaction(transaction: AptosTransaction): Promise<Uint8Array>
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Sign and submit a transaction
|
|
151
|
+
*/
|
|
152
|
+
signAndSubmitTransaction(transaction: AptosTransaction): Promise<{ hash: string }>
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Sign a message
|
|
156
|
+
*/
|
|
157
|
+
signMessage(payload: AptosSignMessagePayload): Promise<AptosSignMessageResponse>
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Check if wallet is connected
|
|
161
|
+
*/
|
|
162
|
+
isConnected(): Promise<boolean>
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Event listeners
|
|
166
|
+
*/
|
|
167
|
+
onAccountChange?: (handler: (account: AptosAccountInfo) => void) => void
|
|
168
|
+
onNetworkChange?: (handler: (network: { networkName: string }) => void) => void
|
|
169
|
+
onDisconnect?: (handler: () => void) => void
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Martian wallet API interface
|
|
174
|
+
*
|
|
175
|
+
* Similar to Petra with slight variations
|
|
176
|
+
*/
|
|
177
|
+
export interface MartianAPI extends PetraAPI {
|
|
178
|
+
// Martian-specific methods can be added here
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Generic Aptos wallet provider interface
|
|
183
|
+
*/
|
|
184
|
+
export interface AptosWalletProvider {
|
|
185
|
+
connect(): Promise<AptosAccountInfo>
|
|
186
|
+
disconnect(): Promise<void>
|
|
187
|
+
account(): Promise<AptosAccountInfo>
|
|
188
|
+
network(): Promise<string>
|
|
189
|
+
signTransaction(transaction: AptosTransaction): Promise<Uint8Array>
|
|
190
|
+
signAndSubmitTransaction(transaction: AptosTransaction): Promise<{ hash: string }>
|
|
191
|
+
signMessage(payload: AptosSignMessagePayload): Promise<AptosSignMessageResponse>
|
|
192
|
+
isConnected(): Promise<boolean>
|
|
193
|
+
|
|
194
|
+
// Event handling
|
|
195
|
+
onAccountChange?: (handler: (account: AptosAccountInfo) => void) => void
|
|
196
|
+
onNetworkChange?: (handler: (network: { networkName: string }) => void) => void
|
|
197
|
+
onDisconnect?: (handler: () => void) => void
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Window interface for Aptos wallets
|
|
202
|
+
*/
|
|
203
|
+
declare global {
|
|
204
|
+
interface Window {
|
|
205
|
+
petra?: PetraAPI
|
|
206
|
+
martian?: MartianAPI
|
|
207
|
+
pontem?: AptosWalletProvider
|
|
208
|
+
aptos?: AptosWalletProvider
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Aptos wallet adapter configuration
|
|
214
|
+
*/
|
|
215
|
+
export interface AptosAdapterConfig {
|
|
216
|
+
/** Wallet name (default: 'petra') */
|
|
217
|
+
wallet?: AptosWalletName
|
|
218
|
+
/** Custom provider (for testing) */
|
|
219
|
+
provider?: AptosWalletProvider
|
|
220
|
+
/** Network name (default: 'mainnet') */
|
|
221
|
+
network?: string
|
|
222
|
+
/** RPC endpoint (optional) */
|
|
223
|
+
rpcEndpoint?: string
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Get Aptos wallet provider from window
|
|
228
|
+
*/
|
|
229
|
+
export function getAptosProvider(walletName: AptosWalletName): AptosWalletProvider | undefined {
|
|
230
|
+
if (typeof window === 'undefined') return undefined
|
|
231
|
+
|
|
232
|
+
switch (walletName) {
|
|
233
|
+
case 'petra':
|
|
234
|
+
return window.petra as AptosWalletProvider | undefined
|
|
235
|
+
case 'martian':
|
|
236
|
+
return window.martian as AptosWalletProvider | undefined
|
|
237
|
+
case 'pontem':
|
|
238
|
+
return window.pontem
|
|
239
|
+
case 'generic':
|
|
240
|
+
return window.aptos
|
|
241
|
+
default:
|
|
242
|
+
return undefined
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Convert Aptos public key to hex format
|
|
248
|
+
*
|
|
249
|
+
* Ensures proper 0x prefix and formatting
|
|
250
|
+
*/
|
|
251
|
+
export function aptosPublicKeyToHex(publicKey: string | Uint8Array): HexString {
|
|
252
|
+
if (typeof publicKey === 'string') {
|
|
253
|
+
// Already a string, ensure 0x prefix
|
|
254
|
+
return (publicKey.startsWith('0x') ? publicKey : `0x${publicKey}`) as HexString
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Convert Uint8Array to hex
|
|
258
|
+
const hex = Array.from(publicKey)
|
|
259
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
260
|
+
.join('')
|
|
261
|
+
return `0x${hex}` as HexString
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Default RPC endpoints for Aptos networks
|
|
266
|
+
*/
|
|
267
|
+
export const DEFAULT_APTOS_RPC_ENDPOINTS: Record<string, string> = {
|
|
268
|
+
mainnet: 'https://fullnode.mainnet.aptoslabs.com/v1',
|
|
269
|
+
testnet: 'https://fullnode.testnet.aptoslabs.com/v1',
|
|
270
|
+
devnet: 'https://fullnode.devnet.aptoslabs.com/v1',
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Get default RPC endpoint for network
|
|
275
|
+
*/
|
|
276
|
+
export function getDefaultAptosRpcEndpoint(network: string): string {
|
|
277
|
+
return DEFAULT_APTOS_RPC_ENDPOINTS[network.toLowerCase()] ?? DEFAULT_APTOS_RPC_ENDPOINTS.mainnet
|
|
278
|
+
}
|