-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.ts
More file actions
195 lines (169 loc) · 4.72 KB
/
context.ts
File metadata and controls
195 lines (169 loc) · 4.72 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* Handler Context
* 管理小程序回调的注册中心,支持多实例
*/
import type {
BioAccount,
EcosystemTransferParams,
EcosystemDestroyParams,
UnsignedTransaction,
SignedTransaction,
HandlerContext as EcosystemHandlerContext,
} from '../types'
/** EVM 交易请求类型 */
export interface EvmTransactionRequest {
from: string
to?: string
value?: string
data?: string
gas?: string
gasPrice?: string
maxFeePerGas?: string
maxPriorityFeePerGas?: string
nonce?: string
chainId?: string
}
/** TRON 交易类型 */
export interface TronTransaction {
txID: string
raw_data: unknown
raw_data_hex: string
signature?: string[]
}
/** 小程序信息(用于在 Sheet 中显示) */
export interface MiniappInfo {
name: string
icon?: string
}
/**
* 从 handler context 构造统一的 miniapp 信息。
*/
export function toMiniappInfo(context: Pick<EcosystemHandlerContext, 'appName' | 'appIcon'>): MiniappInfo {
const icon = context.appIcon?.trim()
return icon
? { name: context.appName, icon }
: { name: context.appName }
}
/** 签名参数 */
export interface SigningParams {
message: string
address: string
chainName: string
app: MiniappInfo
}
/** 签名结果 */
export interface SigningResult {
signature: string
publicKey: string
}
/** 交易签名参数 */
export interface SignTransactionParams {
from: string
chain: string
unsignedTx: UnsignedTransaction
app: MiniappInfo
}
/** EVM 签名参数 */
export interface EvmSigningParams {
message: string
address: string
appName: string
}
/** EVM 交易参数 */
export interface EvmTransactionParams {
tx: EvmTransactionRequest
appName: string
}
/** TRON 签名参数 */
export interface TronSigningParams {
transaction: TronTransaction
appName: string
}
/** 转账对话框返回值 */
export interface TransferDialogResult {
/** 链上交易 ID(兼容旧字段) */
txHash: string
/** 语义化别名:交易 ID */
txId?: string
/** 可序列化交易对象(供调用方直接提交后端) */
transaction?: Record<string, unknown>
}
/** 销毁对话框返回值 */
export interface DestroyDialogResult {
/** 链上交易 ID(兼容旧字段) */
txHash: string
/** 语义化别名:交易 ID */
txId?: string
/** 可序列化交易对象(供调用方直接提交后端) */
transaction?: Record<string, unknown>
}
/** Handler 回调接口 */
export interface HandlerCallbacks {
// Bio (BioChain) callbacks
showWalletPicker: (opts?: { chain?: string; exclude?: string; app?: MiniappInfo }) => Promise<BioAccount | null>
getConnectedAccounts: () => BioAccount[]
showSigningDialog: (params: SigningParams) => Promise<SigningResult | null>
showTransferDialog: (params: EcosystemTransferParams & { app: MiniappInfo }) => Promise<TransferDialogResult | null>
showDestroyDialog?: (params: EcosystemDestroyParams & { app: MiniappInfo }) => Promise<DestroyDialogResult | null>
showSignTransactionDialog: (params: SignTransactionParams) => Promise<SignedTransaction | null>
// EVM (Ethereum/BSC) callbacks
showEvmWalletPicker?: (opts: { chainId: string; app?: MiniappInfo }) => Promise<BioAccount | null>
showEvmSigningDialog?: (params: EvmSigningParams) => Promise<{ signature: string } | null>
showEvmTransactionDialog?: (params: EvmTransactionParams) => Promise<{ txHash: string } | null>
// TRON callbacks
showTronWalletPicker?: (opts?: { app?: MiniappInfo }) => Promise<BioAccount | null>
showTronSigningDialog?: (params: TronSigningParams) => Promise<{ signedTransaction: TronTransaction } | null>
}
/** 回调注册表 */
const callbackRegistry = new Map<string, HandlerCallbacks>()
/**
* Handler 上下文管理
*/
export const HandlerContext = {
/**
* 注册回调
*/
register(appId: string, callbacks: HandlerCallbacks): void {
callbackRegistry.set(appId, callbacks)
},
/**
* 注销回调
*/
unregister(appId: string): void {
callbackRegistry.delete(appId)
},
/**
* 获取回调
*/
get(appId: string): HandlerCallbacks | undefined {
return callbackRegistry.get(appId)
},
/**
* 检查是否有注册的回调
*/
has(appId: string): boolean {
return callbackRegistry.has(appId)
},
/**
* 获取所有已注册的 appId
*/
getRegisteredApps(): string[] {
return Array.from(callbackRegistry.keys())
},
/**
* 清除所有回调(用于测试)
*/
clear(): void {
callbackRegistry.clear()
},
}
/**
* 获取指定应用的回调,如果不存在则抛出错误
*/
export function getCallbacksOrThrow(appId: string): HandlerCallbacks {
const callbacks = HandlerContext.get(appId)
if (!callbacks) {
throw new Error(`No callbacks registered for app: ${appId}`)
}
return callbacks
}