-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathindex.ts
More file actions
79 lines (70 loc) · 2.63 KB
/
index.ts
File metadata and controls
79 lines (70 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { State } from '@0xsequence/wallet-core'
import { Payload } from '@0xsequence/wallet-primitives'
import { Address, Hex } from 'ox'
import { UnsignedMigration } from '../types.js'
import { MigrationEncoder_v1v3 } from './v1/encoder_v1_v3.js'
export interface MigrationEncoder<FromConfigType, ToConfigType, ToContextType, ConvertOptionsType, PrepareOptionsType> {
fromVersion: number
toVersion: number
/**
* Converts from `FromConfigType` to `ToConfigType`
* @param fromConfig The configuration to convert from
* @param options The convert options
* @returns The converted configuration
*/
convertConfig: (fromConfig: FromConfigType, options: ConvertOptionsType) => Promise<ToConfigType>
/**
* Prepares a migration for a given wallet address and context
* @param walletAddress The wallet address to prepare the migration for
* @param contexts The contexts to prepare the migration for
* @param toConfig The configuration to prepare the migration for
* @param options The prepare options
* @returns The migration payload to be signed
*/
prepareMigration: (
walletAddress: Address.Address,
toContext: ToContextType,
toConfig: ToConfigType,
options: PrepareOptionsType,
) => Promise<UnsignedMigration>
/**
* Encodes the a transaction for a given migration
* @param migration The migration to encode the transaction for
* @returns The encoded transaction
*/
toTransactionData: (migration: State.Migration) => Promise<{
to: Address.Address
data: Hex.Hex
}>
/**
* Decodes the payload from a migration
* @param payload The payload to decode
* @returns The decoded address and resulting image hash for the migration payload
*/
decodePayload: (payload: Payload.Calls) => Promise<{
address: Address.Address
toImageHash: Hex.Hex
}>
}
export interface Migrator<FromWallet, ToWallet, ConvertOptionsType> {
fromVersion: number
toVersion: number
convertWallet: (fromWallet: FromWallet, options: ConvertOptionsType) => Promise<ToWallet>
}
export const encoders: MigrationEncoder<any, any, any, any, any>[] = [new MigrationEncoder_v1v3()]
export function getMigrationEncoder<
FromConfigType,
ToConfigType,
ToContextType,
ConvertOptionsType,
PrepareOptionsType,
>(
fromVersion: number,
toVersion: number,
): MigrationEncoder<FromConfigType, ToConfigType, ToContextType, ConvertOptionsType, PrepareOptionsType> {
const encoder = encoders.find((encoder) => encoder.fromVersion === fromVersion && encoder.toVersion === toVersion)
if (!encoder) {
throw new Error(`Unsupported from version: ${fromVersion} to version: ${toVersion}`)
}
return encoder
}