turing-wallet-provider 1.4.14 → 1.4.15
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 +209 -0
- package/dist/types/providerTypes.d.ts +45 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -831,3 +831,212 @@ if (result.txraw) {
|
|
|
831
831
|
// 可以自行广播这个交易
|
|
832
832
|
}
|
|
833
833
|
```
|
|
834
|
+
|
|
835
|
+
## btc.sendTransaction
|
|
836
|
+
|
|
837
|
+
通过 `btc` 对象发送 BTC 链上的交易。
|
|
838
|
+
|
|
839
|
+
### 参数说明
|
|
840
|
+
|
|
841
|
+
```ts
|
|
842
|
+
interface BtcSendTransaction {
|
|
843
|
+
toAddress: string; // 接收地址
|
|
844
|
+
amount: string; // 转账金额,字符串格式,单位为 satoshis
|
|
845
|
+
broadcastEnabled?: boolean; // 是否广播交易,默认为 true
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
interface BtcSendTransactionResponse {
|
|
849
|
+
txid?: string; // 交易哈希(广播成功时返回)
|
|
850
|
+
txraw?: string; // 交易原始数据(broadcastEnabled为false时返回)
|
|
851
|
+
error?: string; // 错误信息
|
|
852
|
+
}
|
|
853
|
+
```
|
|
854
|
+
|
|
855
|
+
### 使用示例
|
|
856
|
+
|
|
857
|
+
#### 转账 BTC
|
|
858
|
+
|
|
859
|
+
```ts
|
|
860
|
+
const wallet = useTuringWallet();
|
|
861
|
+
|
|
862
|
+
const result = await wallet.btc.sendTransaction({
|
|
863
|
+
toAddress: "bc1qxyz...",
|
|
864
|
+
amount: "100000", // 100000 satoshis
|
|
865
|
+
broadcastEnabled: true,
|
|
866
|
+
});
|
|
867
|
+
|
|
868
|
+
if (result.txid) {
|
|
869
|
+
console.log("交易哈希:", result.txid);
|
|
870
|
+
} else if (result.error) {
|
|
871
|
+
console.error("交易失败:", result.error);
|
|
872
|
+
}
|
|
873
|
+
```
|
|
874
|
+
|
|
875
|
+
#### 不广播,仅返回签名交易
|
|
876
|
+
|
|
877
|
+
```ts
|
|
878
|
+
const wallet = useTuringWallet();
|
|
879
|
+
|
|
880
|
+
const result = await wallet.btc.sendTransaction({
|
|
881
|
+
toAddress: "bc1qxyz...",
|
|
882
|
+
amount: "100000",
|
|
883
|
+
broadcastEnabled: false,
|
|
884
|
+
});
|
|
885
|
+
|
|
886
|
+
if (result.txraw) {
|
|
887
|
+
console.log("交易原始数据:", result.txraw);
|
|
888
|
+
}
|
|
889
|
+
```
|
|
890
|
+
|
|
891
|
+
## btc.signTransaction
|
|
892
|
+
|
|
893
|
+
通过 `btc` 对象对单个 BTC 裸交易进行签名,支持 legacy、segwit_v0、taproot 三种签名类型。taproot 类型下通过 `leafHashesHex` 区分 key path 和 script path:对应输入为 `undefined` 则走 key path,有值则走 script path。
|
|
894
|
+
|
|
895
|
+
### 参数说明
|
|
896
|
+
|
|
897
|
+
```ts
|
|
898
|
+
type BtcSigHashType = "legacy" | "segwit_v0" | "taproot";
|
|
899
|
+
|
|
900
|
+
interface BtcSignTransaction {
|
|
901
|
+
txHex: string; // 裸交易 hex
|
|
902
|
+
type: BtcSigHashType; // 签名哈希类型
|
|
903
|
+
prevOutScriptsHex: string[]; // 每个输入对应的前置输出脚本 hex
|
|
904
|
+
values?: number[]; // 每个输入对应的金额(satoshis),segwit_v0/taproot 必填
|
|
905
|
+
leafHashesHex?: (string | undefined)[]; // 每个输入对应的叶子哈希,仅 taproot 类型使用,undefined 表示 key path,有值表示 script path
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
interface BtcSignTransactionResponse {
|
|
909
|
+
sigs?: string[]; // 每个输入对应的签名 hex
|
|
910
|
+
error?: string; // 错误信息
|
|
911
|
+
}
|
|
912
|
+
```
|
|
913
|
+
|
|
914
|
+
### 使用示例
|
|
915
|
+
|
|
916
|
+
#### Legacy (P2PKH) 签名
|
|
917
|
+
|
|
918
|
+
```ts
|
|
919
|
+
const wallet = useTuringWallet();
|
|
920
|
+
|
|
921
|
+
const result = await wallet.btc.signTransaction({
|
|
922
|
+
txHex: "0200000001...",
|
|
923
|
+
type: "legacy",
|
|
924
|
+
prevOutScriptsHex: ["76a914...88ac"],
|
|
925
|
+
});
|
|
926
|
+
|
|
927
|
+
if (result.sigs) {
|
|
928
|
+
console.log("签名列表:", result.sigs);
|
|
929
|
+
} else if (result.error) {
|
|
930
|
+
console.error("签名失败:", result.error);
|
|
931
|
+
}
|
|
932
|
+
```
|
|
933
|
+
|
|
934
|
+
#### SegWit V0 (P2WPKH) 签名
|
|
935
|
+
|
|
936
|
+
```ts
|
|
937
|
+
const wallet = useTuringWallet();
|
|
938
|
+
|
|
939
|
+
const result = await wallet.btc.signTransaction({
|
|
940
|
+
txHex: "0200000001...",
|
|
941
|
+
type: "segwit_v0",
|
|
942
|
+
prevOutScriptsHex: ["76a914...88ac"],
|
|
943
|
+
values: [100000],
|
|
944
|
+
});
|
|
945
|
+
|
|
946
|
+
if (result.sigs) {
|
|
947
|
+
console.log("签名列表:", result.sigs);
|
|
948
|
+
}
|
|
949
|
+
```
|
|
950
|
+
|
|
951
|
+
#### Taproot Key Path 签名
|
|
952
|
+
|
|
953
|
+
```ts
|
|
954
|
+
const wallet = useTuringWallet();
|
|
955
|
+
|
|
956
|
+
const result = await wallet.btc.signTransaction({
|
|
957
|
+
txHex: "0200000001...",
|
|
958
|
+
type: "taproot",
|
|
959
|
+
prevOutScriptsHex: ["5120..."],
|
|
960
|
+
values: [100000],
|
|
961
|
+
// leafHashesHex 不传或对应位置为 undefined,走 key path
|
|
962
|
+
});
|
|
963
|
+
|
|
964
|
+
if (result.sigs) {
|
|
965
|
+
console.log("签名列表:", result.sigs);
|
|
966
|
+
}
|
|
967
|
+
```
|
|
968
|
+
|
|
969
|
+
#### Taproot Script Path 签名
|
|
970
|
+
|
|
971
|
+
```ts
|
|
972
|
+
const wallet = useTuringWallet();
|
|
973
|
+
|
|
974
|
+
const result = await wallet.btc.signTransaction({
|
|
975
|
+
txHex: "0200000001...",
|
|
976
|
+
type: "taproot",
|
|
977
|
+
prevOutScriptsHex: ["5120..."],
|
|
978
|
+
values: [100000],
|
|
979
|
+
leafHashesHex: ["ab12cd34..."], // 对应输入有 leafHash,走 script path
|
|
980
|
+
});
|
|
981
|
+
|
|
982
|
+
if (result.sigs) {
|
|
983
|
+
console.log("签名列表:", result.sigs);
|
|
984
|
+
}
|
|
985
|
+
```
|
|
986
|
+
|
|
987
|
+
## btc.sendBatchRequest
|
|
988
|
+
|
|
989
|
+
通过 `btc` 对象批量提交 BTC 相关请求,每个请求独立执行,一个失败不影响其他请求。
|
|
990
|
+
|
|
991
|
+
### 支持的方法
|
|
992
|
+
|
|
993
|
+
- `sendTransaction` - 发送交易
|
|
994
|
+
- `signTransaction` - 签名交易
|
|
995
|
+
|
|
996
|
+
### 参数说明
|
|
997
|
+
|
|
998
|
+
```ts
|
|
999
|
+
type BtcBatchRequestMethod = "sendTransaction" | "signTransaction";
|
|
1000
|
+
|
|
1001
|
+
interface BtcBatchRequest {
|
|
1002
|
+
method: BtcBatchRequestMethod;
|
|
1003
|
+
params: BtcSendTransaction | BtcSignTransaction;
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
type BtcBatchResponse = Array<BtcSendTransactionResponse | BtcSignTransactionResponse>;
|
|
1007
|
+
```
|
|
1008
|
+
|
|
1009
|
+
### 使用示例
|
|
1010
|
+
|
|
1011
|
+
```ts
|
|
1012
|
+
const wallet = useTuringWallet();
|
|
1013
|
+
|
|
1014
|
+
const requests = [
|
|
1015
|
+
{
|
|
1016
|
+
method: "sendTransaction",
|
|
1017
|
+
params: {
|
|
1018
|
+
toAddress: "bc1qxyz...",
|
|
1019
|
+
amount: "100000",
|
|
1020
|
+
broadcastEnabled: true,
|
|
1021
|
+
},
|
|
1022
|
+
},
|
|
1023
|
+
{
|
|
1024
|
+
method: "signTransaction",
|
|
1025
|
+
params: {
|
|
1026
|
+
txHex: "0200000001...",
|
|
1027
|
+
type: "legacy",
|
|
1028
|
+
prevOutScriptsHex: ["76a914...88ac"],
|
|
1029
|
+
},
|
|
1030
|
+
},
|
|
1031
|
+
];
|
|
1032
|
+
|
|
1033
|
+
const results = await wallet.btc.sendBatchRequest(requests);
|
|
1034
|
+
|
|
1035
|
+
results.forEach((result, index) => {
|
|
1036
|
+
if (result.error) {
|
|
1037
|
+
console.error(`Request ${index + 1} failed:`, result.error);
|
|
1038
|
+
} else {
|
|
1039
|
+
console.log(`Request ${index + 1} success:`, result);
|
|
1040
|
+
}
|
|
1041
|
+
});
|
|
1042
|
+
```
|
|
@@ -179,6 +179,50 @@ export type EvmProvider = {
|
|
|
179
179
|
sendTransaction: (params: EvmSendTransaction) => Promise<EvmSendTransactionResponse | undefined>;
|
|
180
180
|
};
|
|
181
181
|
|
|
182
|
+
export type BtcSendTransaction = {
|
|
183
|
+
toAddress: string;
|
|
184
|
+
amount: string;
|
|
185
|
+
broadcastEnabled?: boolean;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
export type BtcSendTransactionResponse = {
|
|
189
|
+
txid?: string;
|
|
190
|
+
txraw?: string;
|
|
191
|
+
error?: string;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
export type BtcSigHashType = "legacy" | "segwit_v0" | "taproot";
|
|
195
|
+
|
|
196
|
+
export type BtcSignTransaction = {
|
|
197
|
+
txHex: string;
|
|
198
|
+
type: BtcSigHashType;
|
|
199
|
+
prevOutScriptsHex: string[];
|
|
200
|
+
values?: number[];
|
|
201
|
+
leafHashesHex?: (string | undefined)[];
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
export type BtcSignTransactionResponse = {
|
|
205
|
+
sigs?: string[];
|
|
206
|
+
error?: string;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
export type BtcBatchRequestMethod = "sendTransaction" | "signTransaction";
|
|
210
|
+
|
|
211
|
+
export type BtcBatchRequest = {
|
|
212
|
+
method: BtcBatchRequestMethod;
|
|
213
|
+
params: BtcSendTransaction | BtcSignTransaction;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
export type BtcBatchResponse = Array<
|
|
217
|
+
BtcSendTransactionResponse | BtcSignTransactionResponse
|
|
218
|
+
>;
|
|
219
|
+
|
|
220
|
+
export type BtcProvider = {
|
|
221
|
+
sendTransaction: (params: BtcSendTransaction) => Promise<BtcSendTransactionResponse | undefined>;
|
|
222
|
+
signTransaction: (params: BtcSignTransaction) => Promise<BtcSignTransactionResponse | undefined>;
|
|
223
|
+
sendBatchRequest: (requests: BtcBatchRequest[]) => Promise<BtcBatchResponse | undefined>;
|
|
224
|
+
};
|
|
225
|
+
|
|
182
226
|
export type TuringProviderType = {
|
|
183
227
|
isReady: boolean;
|
|
184
228
|
connect: () => Promise<ConnectResponse | undefined>;
|
|
@@ -203,4 +247,5 @@ export type TuringProviderType = {
|
|
|
203
247
|
) => Promise<SignAssociatedTransactionResponse | undefined>;
|
|
204
248
|
sendBatchRequest: (requests: BatchRequest[]) => Promise<BatchResponse | undefined>;
|
|
205
249
|
evm: EvmProvider;
|
|
250
|
+
btc: BtcProvider;
|
|
206
251
|
};
|