turing-wallet-provider 1.5.3 → 1.5.5
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/README.md +82 -131
- package/dist/types/providerTypes.d.ts +11 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -65,6 +65,71 @@ const {name,platform,version} = await wallet.getInfo();
|
|
|
65
65
|
{Turing,android,1.0.0}//示例的返回值
|
|
66
66
|
```
|
|
67
67
|
|
|
68
|
+
## getNetwork
|
|
69
|
+
|
|
70
|
+
获取已连接账户当前激活的网络。
|
|
71
|
+
|
|
72
|
+
### 用法
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
const wallet = useTuringWallet();
|
|
76
|
+
const network = await wallet.getNetwork();
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### 参数
|
|
80
|
+
|
|
81
|
+
该方法不接受任何参数。
|
|
82
|
+
|
|
83
|
+
### 返回值
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
interface GetNetworkResponse {
|
|
87
|
+
network: "tbc" | "btc" | "eth" | "bnb" | "all";
|
|
88
|
+
type: "mainnet" | "testnet";
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
| 字段 | 类型 | 说明 |
|
|
93
|
+
| --- | --- | --- |
|
|
94
|
+
| `network` | `"tbc" \| "btc" \| "eth" \| "bnb" \| "all"` | 当前激活的链。`"all"` 表示用户处于钱包的"所有网络"视图,未选择具体的链。 |
|
|
95
|
+
| `type` | `"mainnet" \| "testnet"` | 主网 / 测试网。仅 `tbc` 可能为 `"testnet"`,其余链恒为 `"mainnet"`。 |
|
|
96
|
+
|
|
97
|
+
### 错误处理
|
|
98
|
+
|
|
99
|
+
| 错误 | 原因 |
|
|
100
|
+
| --- | --- |
|
|
101
|
+
| `User not connected` | 尚未调用 `wallet.connect()`,或用户已断开连接。 |
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
const wallet = useTuringWallet();
|
|
105
|
+
|
|
106
|
+
try {
|
|
107
|
+
const info = await wallet.getNetwork();
|
|
108
|
+
console.log(info);
|
|
109
|
+
} catch (error) {
|
|
110
|
+
console.error("Failed to get network:", error);
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### 订阅变化
|
|
115
|
+
|
|
116
|
+
钱包内切换网络时,dapp 会收到 `TuringNetworkChanged` 事件并附带新的网络信息,无需轮询 `getNetwork()`。
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
window.addEventListener("TuringNetworkChanged", (event) => {
|
|
120
|
+
const { network } = event.detail;
|
|
121
|
+
// `network` 与 wallet.getNetwork() 的返回值同结构
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
interface TuringNetworkChangedDetail {
|
|
127
|
+
network: GetNetworkResponse;
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
仅在 dapp 已连接期间触发,且只推送 `connect()` 之后的变化(初始网络可通过 `wallet.getNetwork()` 获取)。
|
|
132
|
+
|
|
68
133
|
## signMessage
|
|
69
134
|
|
|
70
135
|
```ts
|
|
@@ -202,11 +267,10 @@ interface Input {
|
|
|
202
267
|
| "tbc20" // 普通 FT 转账解锁
|
|
203
268
|
| "tbc20_contract" // 普通 FT 在合约/swap 场景下的解锁
|
|
204
269
|
| "tbc20_coin" // 稳定币转账解锁(FT.getFTunlock + isCoin)
|
|
205
|
-
| "tbc20_coin_contract" // 稳定币在合约/swap 场景下的解锁(FT.getFTunlockSwap + isCoin)
|
|
206
270
|
| "other"; // 脚本签名类型
|
|
207
271
|
unfinishedScriptSig?: string; // "other" 类型的自定义脚本模板(hex 格式),签名部分用 097369676e6174757265 替代
|
|
208
|
-
ftVersion?: 1 | 2; // "tbc20_contract"
|
|
209
|
-
contractTxId?: string; // "tbc20_contract"
|
|
272
|
+
ftVersion?: 1 | 2; // "tbc20_contract" 类型的 FT 版本
|
|
273
|
+
contractTxId?: string; // "tbc20_contract" 类型的合约交易 ID
|
|
210
274
|
}
|
|
211
275
|
|
|
212
276
|
interface Output {
|
|
@@ -329,16 +393,15 @@ const { txraws } = await Turing.signAssociatedTransaction({
|
|
|
329
393
|
});
|
|
330
394
|
```
|
|
331
395
|
|
|
332
|
-
###
|
|
396
|
+
### 稳定币转移 (tbc20_coin)
|
|
333
397
|
|
|
334
|
-
稳定币是一种特殊的 FT
|
|
398
|
+
稳定币是一种特殊的 FT,转移时的解锁脚本与普通 FT 不同,所以提供了专用的 `scriptSigType`:
|
|
335
399
|
|
|
336
400
|
- `"tbc20_coin"`:稳定币普通转账解锁
|
|
337
|
-
- `"tbc20_coin_contract"`:稳定币参与合约 / swap 场景的解锁(同时需要 `contractTxId` 与 `ftVersion`)
|
|
338
401
|
|
|
339
402
|
#### 子交易 (`inputs` / `outputs`)
|
|
340
403
|
|
|
341
|
-
只要把对应输入的 `scriptSigType` 标成 `"tbc20_coin"`
|
|
404
|
+
只要把对应输入的 `scriptSigType` 标成 `"tbc20_coin"` 即可,**其它字段与普通 FT 写法完全一致**,无需关心 sequence / nLockTime。
|
|
342
405
|
|
|
343
406
|
#### 源交易 (`sourceTxraw`)
|
|
344
407
|
|
|
@@ -372,7 +435,7 @@ tx.setLockTime(lockTimeMax);
|
|
|
372
435
|
const sourceTxraw = tx.uncheckedSerialize();
|
|
373
436
|
```
|
|
374
437
|
|
|
375
|
-
> 对应的 `sourceUtxos` 条目仍要填 `"tbc20_coin"
|
|
438
|
+
> 对应的 `sourceUtxos` 条目仍要填 `"tbc20_coin"`。
|
|
376
439
|
|
|
377
440
|
#### 稳定币示例
|
|
378
441
|
|
|
@@ -419,17 +482,6 @@ const { txraws } = await Turing.signAssociatedTransaction({
|
|
|
419
482
|
});
|
|
420
483
|
```
|
|
421
484
|
|
|
422
|
-
如果稳定币要走合约 / swap 路径,把 `scriptSigType` 改成 `"tbc20_coin_contract"`,并附上 `contractTxId` 与 `ftVersion`:
|
|
423
|
-
|
|
424
|
-
```ts
|
|
425
|
-
{
|
|
426
|
-
outputIndex: 0,
|
|
427
|
-
scriptSigType: "tbc20_coin_contract",
|
|
428
|
-
contractTxId: "<合约交易 ID>",
|
|
429
|
-
ftVersion: 2,
|
|
430
|
-
}
|
|
431
|
-
```
|
|
432
|
-
|
|
433
485
|
## sendTransaction
|
|
434
486
|
|
|
435
487
|
使用钱包发送交易。支持多种交易类型,包括 P2PKH、NFT 操作、FT 操作和 PoolNFT 操作。
|
|
@@ -460,7 +512,7 @@ interface NFTData {
|
|
|
460
512
|
}
|
|
461
513
|
|
|
462
514
|
interface RequestParam {
|
|
463
|
-
flag: "P2PKH" | "COLLECTION_CREATE" | "NFT_CREATE" | "NFT_TRANSFER" | "FT_MINT" | "FT_TRANSFER" | "FT_MERGE" | "POOLNFT_MINT" | "POOLNFT_INIT" | "POOLNFT_LP_INCREASE" | "POOLNFT_LP_CONSUME" | "POOLNFT_LP_BURN" | "POOLNFT_SWAP_TO_TOKEN" | "POOLNFT_SWAP_TO_TBC" | "POOLNFT_MERGE" | "FTLP_MERGE" | "
|
|
515
|
+
flag: "P2PKH" | "COLLECTION_CREATE" | "NFT_CREATE" | "NFT_TRANSFER" | "FT_MINT" | "FT_TRANSFER" | "FT_MERGE" | "POOLNFT_MINT" | "POOLNFT_INIT" | "POOLNFT_LP_INCREASE" | "POOLNFT_LP_CONSUME" | "POOLNFT_LP_BURN" | "POOLNFT_SWAP_TO_TOKEN" | "POOLNFT_SWAP_TO_TBC" | "POOLNFT_MERGE" | "FTLP_MERGE" | "STABLECOIN_TRANSFER" | "STABLECOIN_MERGE";
|
|
464
516
|
address?: string;
|
|
465
517
|
satoshis?: number | string; // 单位为 satoshis,大数请使用 string
|
|
466
518
|
collection_data?: string;
|
|
@@ -468,7 +520,7 @@ interface RequestParam {
|
|
|
468
520
|
nft_data?: string;
|
|
469
521
|
collection_id?: string;
|
|
470
522
|
nft_contract_address?: string;
|
|
471
|
-
ft_contract_address?: string; // FT 或稳定币合约交易 ID
|
|
523
|
+
ft_contract_address?: string; // FT 或稳定币合约交易 ID
|
|
472
524
|
tbc_amount?: number | string; // 大数请使用 string
|
|
473
525
|
ft_amount?: number | string; // 大数请使用 string
|
|
474
526
|
merge_times?: number;
|
|
@@ -482,11 +534,8 @@ interface RequestParam {
|
|
|
482
534
|
lpPlan?: 1 | 2 | 3 | 4 | 5; // 默认 1
|
|
483
535
|
domain?: string;
|
|
484
536
|
isLockTime?: boolean;
|
|
485
|
-
lockTime?: number | string; // 锁仓至指定区块高度(POOLNFT
|
|
537
|
+
lockTime?: number | string; // 锁仓至指定区块高度(POOLNFT 相关),大数请使用 string
|
|
486
538
|
broadcastEnabled?: boolean;
|
|
487
|
-
mint_message?: string; // 铸造/增发跨链信息(STABLECOIN_CREATE / STABLECOIN_MINT 使用)
|
|
488
|
-
utxo_txid?: string; // 目标 UTXO 的交易 ID(STABLECOIN_FREEZE / STABLECOIN_UNFREEZE 使用)
|
|
489
|
-
utxo_index?: number; // 目标 UTXO 的输出索引(STABLECOIN_FREEZE / STABLECOIN_UNFREEZE 使用)
|
|
490
539
|
}
|
|
491
540
|
|
|
492
541
|
const params = [param: RequestParam];
|
|
@@ -837,54 +886,6 @@ const { txid } = await wallet.sendTransaction(params); // txid 为多个 Merge
|
|
|
837
886
|
// const { error } = await wallet.sendTransaction(params); // 发生错误时
|
|
838
887
|
```
|
|
839
888
|
|
|
840
|
-
### STABLECOIN_CREATE
|
|
841
|
-
|
|
842
|
-
发行稳定币合约(仅需执行一次)。稳定币继承自 FT,构造方式与 FT 相同。
|
|
843
|
-
|
|
844
|
-
```ts
|
|
845
|
-
const params = [
|
|
846
|
-
{
|
|
847
|
-
flag: "STABLECOIN_CREATE", // 必填
|
|
848
|
-
ft_data: JSON.stringify({ // 必填,JSON 格式的 FTData
|
|
849
|
-
name: "USD Test",
|
|
850
|
-
symbol: "USDT",
|
|
851
|
-
decimal: 6,
|
|
852
|
-
amount: 100000000,
|
|
853
|
-
}),
|
|
854
|
-
address: "", // 必填,初始接收地址(一般是管理员自身)
|
|
855
|
-
mint_message: "SourceChain: BSC, TXID: 34434...", // 必填,跨链信息,起始链名称和交易 id
|
|
856
|
-
broadcastEnabled: true, // 可选,默认 true
|
|
857
|
-
domain: "", // 可选
|
|
858
|
-
},
|
|
859
|
-
];
|
|
860
|
-
|
|
861
|
-
const { txid } = await wallet.sendTransaction(params); // txid 有两个,用逗号隔开,第一个 txid 即为稳定币合约 ID
|
|
862
|
-
// const { txraw } = await wallet.sendTransaction(params); // broadcastEnabled 为 false 时,返回的 txraw 有两个,用逗号隔开,第一个 txraw 对应的 txid 即为稳定币合约 ID,需批量广播,保证前面的 txraw 先广播
|
|
863
|
-
// const { error } = await wallet.sendTransaction(params); // 发生错误时
|
|
864
|
-
```
|
|
865
|
-
|
|
866
|
-
### STABLECOIN_MINT
|
|
867
|
-
|
|
868
|
-
增发稳定币(仅管理员可操作)。
|
|
869
|
-
|
|
870
|
-
```ts
|
|
871
|
-
const params = [
|
|
872
|
-
{
|
|
873
|
-
flag: "STABLECOIN_MINT", // 必填
|
|
874
|
-
ft_contract_address: "", // 必填,稳定币合约交易 ID(STABLECOIN_CREATE 返回的第一个 txid)
|
|
875
|
-
address: "", // 必填,接收新铸稳定币的地址
|
|
876
|
-
ft_amount: 50000, // 必填,增发数量,大数请使用 string
|
|
877
|
-
mint_message: "SourceChain: BSC, TXID: 34434...", // 必填,跨链信息,起始链名称和交易 id
|
|
878
|
-
broadcastEnabled: true, // 可选,默认 true
|
|
879
|
-
domain: "", // 可选
|
|
880
|
-
},
|
|
881
|
-
];
|
|
882
|
-
|
|
883
|
-
const { txid } = await wallet.sendTransaction(params);
|
|
884
|
-
// const { txraw } = await wallet.sendTransaction(params); // broadcastEnabled 为 false 时
|
|
885
|
-
// const { error } = await wallet.sendTransaction(params); // 发生错误时
|
|
886
|
-
```
|
|
887
|
-
|
|
888
889
|
### STABLECOIN_TRANSFER
|
|
889
890
|
|
|
890
891
|
转移稳定币。
|
|
@@ -893,7 +894,7 @@ const { txid } = await wallet.sendTransaction(params);
|
|
|
893
894
|
const params = [
|
|
894
895
|
{
|
|
895
896
|
flag: "STABLECOIN_TRANSFER", // 必填
|
|
896
|
-
ft_contract_address: "", // 必填,稳定币合约交易 ID
|
|
897
|
+
ft_contract_address: "", // 必填,稳定币合约交易 ID
|
|
897
898
|
address: "", // 必填,接收地址
|
|
898
899
|
ft_amount: 1000, // 必填,转移数量,大数请使用 string
|
|
899
900
|
tbc_amount: 0, // 可选,同时转 TBC 和稳定币时设置此值
|
|
@@ -907,51 +908,6 @@ const { txid } = await wallet.sendTransaction(params);
|
|
|
907
908
|
// const { error } = await wallet.sendTransaction(params); // 发生错误时
|
|
908
909
|
```
|
|
909
910
|
|
|
910
|
-
### STABLECOIN_FREEZE
|
|
911
|
-
|
|
912
|
-
冻结指定地址的稳定币 UTXO(仅管理员可操作)。冻结后,持有者须等到冻结到期才能使用该 UTXO。
|
|
913
|
-
|
|
914
|
-
```ts
|
|
915
|
-
const params = [
|
|
916
|
-
{
|
|
917
|
-
flag: "STABLECOIN_FREEZE", // 必填
|
|
918
|
-
ft_contract_address: "", // 必填,稳定币合约交易 ID(STABLECOIN_CREATE 返回的第一个 txid)
|
|
919
|
-
address: "", // 必填,被冻结的目标地址
|
|
920
|
-
utxo_txid: "", // 必填,目标稳定币 UTXO 的交易 ID
|
|
921
|
-
utxo_index: 0, // 必填,目标稳定币 UTXO 的输出索引
|
|
922
|
-
lockTime: 1774410989, // 必填,冻结至指定 unix 时间戳
|
|
923
|
-
broadcastEnabled: true, // 可选,默认 true
|
|
924
|
-
domain: "", // 可选
|
|
925
|
-
},
|
|
926
|
-
];
|
|
927
|
-
|
|
928
|
-
const { txid } = await wallet.sendTransaction(params);
|
|
929
|
-
// const { txraw } = await wallet.sendTransaction(params); // broadcastEnabled 为 false 时
|
|
930
|
-
// const { error } = await wallet.sendTransaction(params); // 发生错误时
|
|
931
|
-
```
|
|
932
|
-
|
|
933
|
-
### STABLECOIN_UNFREEZE
|
|
934
|
-
|
|
935
|
-
解冻指定地址的稳定币 UTXO(仅管理员可操作)。
|
|
936
|
-
|
|
937
|
-
```ts
|
|
938
|
-
const params = [
|
|
939
|
-
{
|
|
940
|
-
flag: "STABLECOIN_UNFREEZE", // 必填
|
|
941
|
-
ft_contract_address: "", // 必填,稳定币合约交易 ID(STABLECOIN_CREATE 返回的第一个 txid)
|
|
942
|
-
address: "", // 必填,被解冻的目标地址
|
|
943
|
-
utxo_txid: "", // 必填,目标稳定币 UTXO 的交易 ID
|
|
944
|
-
utxo_index: 0, // 必填,目标稳定币 UTXO 的输出索引
|
|
945
|
-
broadcastEnabled: true, // 可选,默认 true
|
|
946
|
-
domain: "", // 可选
|
|
947
|
-
},
|
|
948
|
-
];
|
|
949
|
-
|
|
950
|
-
const { txid } = await wallet.sendTransaction(params);
|
|
951
|
-
// const { txraw } = await wallet.sendTransaction(params); // broadcastEnabled 为 false 时
|
|
952
|
-
// const { error } = await wallet.sendTransaction(params); // 发生错误时
|
|
953
|
-
```
|
|
954
|
-
|
|
955
911
|
### STABLECOIN_MERGE
|
|
956
912
|
|
|
957
913
|
合并稳定币 UTXO(要求所有 coinutxo 均已上链)。
|
|
@@ -960,7 +916,7 @@ const { txid } = await wallet.sendTransaction(params);
|
|
|
960
916
|
const params = [
|
|
961
917
|
{
|
|
962
918
|
flag: "STABLECOIN_MERGE", // 必填
|
|
963
|
-
ft_contract_address: "", // 必填,稳定币合约交易 ID
|
|
919
|
+
ft_contract_address: "", // 必填,稳定币合约交易 ID
|
|
964
920
|
domain: "", // 可选
|
|
965
921
|
},
|
|
966
922
|
];
|
|
@@ -1176,15 +1132,10 @@ const requests = [
|
|
|
1176
1132
|
{
|
|
1177
1133
|
method: "sendTransaction",
|
|
1178
1134
|
params: {
|
|
1179
|
-
flag: "
|
|
1180
|
-
|
|
1181
|
-
name: "USD Test",
|
|
1182
|
-
symbol: "USDT",
|
|
1183
|
-
decimal: 6,
|
|
1184
|
-
amount: 100000000,
|
|
1185
|
-
}),
|
|
1135
|
+
flag: "STABLECOIN_TRANSFER",
|
|
1136
|
+
ft_contract_address: "",
|
|
1186
1137
|
address: "",
|
|
1187
|
-
|
|
1138
|
+
ft_amount: 1000,
|
|
1188
1139
|
broadcastEnabled: true,
|
|
1189
1140
|
domain: "",
|
|
1190
1141
|
},
|
|
@@ -1193,7 +1144,7 @@ const requests = [
|
|
|
1193
1144
|
method: "signMessage",
|
|
1194
1145
|
params: {
|
|
1195
1146
|
message: JSON.stringify({
|
|
1196
|
-
action: "
|
|
1147
|
+
action: "transfer_stablecoin",
|
|
1197
1148
|
}),
|
|
1198
1149
|
encoding: "utf8",
|
|
1199
1150
|
},
|
|
@@ -1203,10 +1154,10 @@ const requests = [
|
|
|
1203
1154
|
|
|
1204
1155
|
const results = await wallet.sendBatchRequest(requests);
|
|
1205
1156
|
|
|
1206
|
-
// results[0] => sendTransaction 的结果: { txid: "txid1
|
|
1157
|
+
// results[0] => sendTransaction 的结果: { txid: "txid1" }
|
|
1207
1158
|
// results[1] => signMessage 的结果: { address, pubkey, sig, message }
|
|
1208
1159
|
// 其中 message 为包含 txids 的完整 JSON,例如:
|
|
1209
|
-
// { "action": "
|
|
1160
|
+
// { "action": "transfer_stablecoin", "txids": ["txid1"] }
|
|
1210
1161
|
```
|
|
1211
1162
|
|
|
1212
1163
|
#### 执行流程
|
|
@@ -17,6 +17,15 @@ export type Info = {
|
|
|
17
17
|
version: string;
|
|
18
18
|
};
|
|
19
19
|
|
|
20
|
+
export type GetNetworkResponse = {
|
|
21
|
+
network: "tbc" | "btc" | "eth" | "bnb" | "all";
|
|
22
|
+
type: "mainnet" | "testnet";
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type TuringNetworkChangedDetail = {
|
|
26
|
+
network: GetNetworkResponse;
|
|
27
|
+
};
|
|
28
|
+
|
|
20
29
|
export type TransactionFlag =
|
|
21
30
|
| "P2PKH"
|
|
22
31
|
| "COLLECTION_CREATE"
|
|
@@ -34,11 +43,7 @@ export type TransactionFlag =
|
|
|
34
43
|
| "POOLNFT_SWAP_TO_TBC"
|
|
35
44
|
| "POOLNFT_MERGE"
|
|
36
45
|
| "FTLP_MERGE"
|
|
37
|
-
| "STABLECOIN_CREATE"
|
|
38
|
-
| "STABLECOIN_MINT"
|
|
39
46
|
| "STABLECOIN_TRANSFER"
|
|
40
|
-
| "STABLECOIN_FREEZE"
|
|
41
|
-
| "STABLECOIN_UNFREEZE"
|
|
42
47
|
| "STABLECOIN_MERGE";
|
|
43
48
|
|
|
44
49
|
|
|
@@ -67,9 +72,6 @@ export type SendTransaction = {
|
|
|
67
72
|
isLockTime?: boolean;
|
|
68
73
|
lockTime?: number | string;
|
|
69
74
|
broadcastEnabled?: boolean;
|
|
70
|
-
mint_message?: string;
|
|
71
|
-
utxo_txid?: string;
|
|
72
|
-
utxo_index?: number;
|
|
73
75
|
};
|
|
74
76
|
|
|
75
77
|
export type SendTransactionResponse = {
|
|
@@ -125,7 +127,7 @@ export type Input = {
|
|
|
125
127
|
script?: string;
|
|
126
128
|
satoshis?: number;
|
|
127
129
|
outputIndex: number;
|
|
128
|
-
scriptSigType: "p2pkh" | "tbc20" | "tbc20_contract" | "tbc20_coin" | "
|
|
130
|
+
scriptSigType: "p2pkh" | "tbc20" | "tbc20_contract" | "tbc20_coin" | "other";
|
|
129
131
|
unfinishedScriptSig?: string;
|
|
130
132
|
ftVersion?: 1 | 2;
|
|
131
133
|
contractTxId?: string;
|
|
@@ -241,6 +243,7 @@ export type TuringProviderType = {
|
|
|
241
243
|
getPubKey: () => Promise<PubKey | undefined>;
|
|
242
244
|
getAddress: () => Promise<Address | undefined>;
|
|
243
245
|
getInfo: () => Promise<Info | undefined>;
|
|
246
|
+
getNetwork: () => Promise<GetNetworkResponse | undefined>;
|
|
244
247
|
sendTransaction: (
|
|
245
248
|
params: SendTransaction[]
|
|
246
249
|
) => Promise<SendTransactionResponse | undefined>;
|