turing-wallet-provider 1.5.1 → 1.5.3

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 CHANGED
@@ -176,33 +176,93 @@ broadcastTXsraw(txs.map((tx) => ({ txraw: tx.uncheckedSerialize() })));
176
176
 
177
177
  ## signAssociatedTransaction
178
178
 
179
+ 使用钱包签名父子(关联)交易。钱包根据源交易构建子交易并统一签名。
180
+
181
+ ### 用法
182
+
183
+ ```ts
184
+ const { txraws } = await Turing.signAssociatedTransaction({
185
+ sourceTxraw,
186
+ sourceUtxos,
187
+ inputs,
188
+ outputs,
189
+ });
190
+ ```
191
+
192
+ ### 参数
193
+
179
194
  ```ts
180
- //使用示例
181
195
  interface Input {
182
- txId?: string;
183
- script?: string; //仅支持hex类型,不支持asm
184
- satoshis?: number;
185
- outputIndex: number;
186
- scriptSigType: "p2pkh" | "tbc20" | "tbc20_contract" | "other";
187
- unfinishedScriptSig?: string; // 对于"other"类型,用于提供自定义脚本(不包含交易数据的锁定脚本)模板,仅支持hex类型,不支持asm。签名部分用097369676e6174757265替代,09为7369676e6174757265长度
188
- ftVersion?: 1 | 2; // 对于"tbc20_contract"类型,FT版本(1或2)
189
- contractTxId?: string; // 对于"tbc20_contract"类型,合约交易ID
196
+ txId?: string; // 交易 ID(源交易输入需提供)
197
+ script?: string; // 锁定脚本,仅支持 hex 格式
198
+ satoshis?: number; // satoshi 值
199
+ outputIndex: number; // 输出索引
200
+ scriptSigType:
201
+ | "p2pkh"
202
+ | "tbc20" // 普通 FT 转账解锁
203
+ | "tbc20_contract" // 普通 FT 在合约/swap 场景下的解锁
204
+ | "tbc20_coin" // 稳定币转账解锁(FT.getFTunlock + isCoin)
205
+ | "tbc20_coin_contract" // 稳定币在合约/swap 场景下的解锁(FT.getFTunlockSwap + isCoin)
206
+ | "other"; // 脚本签名类型
207
+ unfinishedScriptSig?: string; // "other" 类型的自定义脚本模板(hex 格式),签名部分用 097369676e6174757265 替代
208
+ ftVersion?: 1 | 2; // "tbc20_contract"/"tbc20_coin_contract" 类型的 FT 版本
209
+ contractTxId?: string; // "tbc20_contract"/"tbc20_coin_contract" 类型的合约交易 ID
190
210
  }
191
211
 
192
212
  interface Output {
193
- script: string; //仅支持hex类型,不支持asm
194
- satoshis: number;
213
+ script: string; // 输出锁定脚本,仅支持 hex 格式
214
+ satoshis: number; // 输出的 satoshi 值
195
215
  }
196
216
 
197
217
  interface SignAssociatedTransactionRequestData {
198
- sourceTxraw: string;
199
- sourceUtxos: Input[];
200
- inputs?: Input[][];
201
- outputs?: Output[][];
218
+ sourceTxraw: string; // 源头交易的原始 hex 字符串
219
+ sourceUtxos: Input[]; // 源头交易的 UTXO 数组
220
+ inputs?: Input[][]; // 子交易输入的二维数组
221
+ outputs?: Output[][]; // 子交易输出的二维数组
222
+ }
223
+ ```
224
+
225
+ | 参数 | 类型 | 必填 | 说明 |
226
+ | --- | --- | --- | --- |
227
+ | `sourceTxraw` | `string` | 是 | 源头交易的原始 hex 字符串 |
228
+ | `sourceUtxos` | `Input[]` | 是 | 源头交易的 UTXO 数组 |
229
+ | `inputs` | `Input[][]` | 否 | 二维数组 — 每个子数组代表一个子交易的输入 |
230
+ | `outputs` | `Output[][]` | 否 | 二维数组 — 每个子数组代表一个子交易的输出 |
231
+
232
+ ### 返回值
233
+
234
+ ```ts
235
+ interface SignAssociatedTransactionResponse {
236
+ txraws: string[]; // 已组装的待广播交易 hex 字符串数组(包含父子交易)
202
237
  }
238
+ ```
239
+
240
+ | 字段 | 类型 | 说明 |
241
+ | --- | --- | --- |
242
+ | `txraws` | `string[]` | 包含父子交易的已组装待广播交易数组 |
243
+
244
+ ### 错误处理
203
245
 
204
- //ft转移示例参数
205
- const sourceUtxos: intput[] = [
246
+ ```ts
247
+ try {
248
+ const { txraws } = await Turing.signAssociatedTransaction({
249
+ sourceTxraw,
250
+ sourceUtxos,
251
+ inputs,
252
+ outputs,
253
+ });
254
+ console.log("交易:", txraws);
255
+ } catch (error) {
256
+ console.error("签名失败:", error);
257
+ }
258
+ ```
259
+
260
+ ### 示例
261
+
262
+ FT 转移示例:
263
+
264
+ ```ts
265
+ const sourceUtxos: Input[] = [
206
266
  {
207
267
  txId: "",
208
268
  outputIndex: 0,
@@ -219,7 +279,7 @@ const sourceUtxos: intput[] = [
219
279
  },
220
280
  ];
221
281
 
222
- const inputs: intput[][] = [
282
+ const inputs: Input[][] = [
223
283
  [
224
284
  { outputIndex: 0, scriptSigType: "tbc20" },
225
285
  { outputIndex: 2, scriptSigType: "p2pkh" },
@@ -230,7 +290,7 @@ const inputs: intput[][] = [
230
290
  ],
231
291
  ];
232
292
 
233
- const outputs: output[][] = [
293
+ const outputs: Output[][] = [
234
294
  [
235
295
  {
236
296
  script: ftcode,
@@ -261,8 +321,113 @@ const outputs: output[][] = [
261
321
  ],
262
322
  ];
263
323
 
264
- const wallet = useTuringWallet();
265
- const { txraws } = await wallet.signAssociatedTransaction(params);
324
+ const { txraws } = await Turing.signAssociatedTransaction({
325
+ sourceTxraw,
326
+ sourceUtxos,
327
+ inputs,
328
+ outputs,
329
+ });
330
+ ```
331
+
332
+ ### 稳定币 (tbc20_coin / tbc20_coin_contract)
333
+
334
+ 稳定币是一种特殊的 FT,解锁脚本与普通 FT 不同,所以提供了专用的 `scriptSigType`:
335
+
336
+ - `"tbc20_coin"`:稳定币普通转账解锁
337
+ - `"tbc20_coin_contract"`:稳定币参与合约 / swap 场景的解锁(同时需要 `contractTxId` 与 `ftVersion`)
338
+
339
+ #### 子交易 (`inputs` / `outputs`)
340
+
341
+ 只要把对应输入的 `scriptSigType` 标成 `"tbc20_coin"` 或 `"tbc20_coin_contract"` 即可,**其它字段与普通 FT 写法完全一致**,无需关心 sequence / nLockTime。
342
+
343
+ #### 源交易 (`sourceTxraw`)
344
+
345
+ `sourceTxraw` 是调用方自己组装好的原始交易,**如果里面包含稳定币输入**,必须在构建时自行满足以下两个条件:
346
+
347
+ 1. 每个稳定币输入的 `sequence` 设为 `0xFFFFFFFE`
348
+ 2. 整笔交易的 `nLockTime` 设为所有被花费稳定币 UTXO tape 中 `lockTime` 的最大值
349
+
350
+ 参考写法:
351
+
352
+ ```ts
353
+ import { Transaction } from "tbc-lib-js";
354
+ import { stableCoin } from "tbc-contract";
355
+
356
+ const tx = new Transaction().from(coinUtxos).from(payUtxo);
357
+ // ...addOutput(...)
358
+
359
+ for (let i = 0; i < coinUtxos.length; i++) {
360
+ tx.setInputSequence(i, 0xFFFFFFFE);
361
+ }
362
+
363
+ let lockTimeMax = 0;
364
+ for (let i = 0; i < coinUtxos.length; i++) {
365
+ const lt = stableCoin.getLockTimeFromTape(
366
+ preTXs[i].outputs[coinUtxos[i].outputIndex + 1].script
367
+ );
368
+ lockTimeMax = Math.max(lockTimeMax, lt);
369
+ }
370
+ tx.setLockTime(lockTimeMax);
371
+
372
+ const sourceTxraw = tx.uncheckedSerialize();
373
+ ```
374
+
375
+ > 对应的 `sourceUtxos` 条目仍要填 `"tbc20_coin"` 或 `"tbc20_coin_contract"`。
376
+
377
+ #### 稳定币示例
378
+
379
+ 父交易花掉 1 个稳定币 UTXO + 1 个 P2PKH UTXO,产生新的稳定币 UTXO(output 0/1 为 code/tape,output 2 为 P2PKH 找零);子交易再次花掉这个新稳定币 UTXO:
380
+
381
+ ```ts
382
+ const sourceUtxos: Input[] = [
383
+ {
384
+ txId: coinUtxo.txId,
385
+ outputIndex: coinUtxo.outputIndex,
386
+ satoshis: 500,
387
+ script: coinCodeHex,
388
+ scriptSigType: "tbc20_coin",
389
+ },
390
+ {
391
+ txId: payUtxo.txId,
392
+ outputIndex: payUtxo.outputIndex,
393
+ satoshis: 10000,
394
+ script: p2pkhHex,
395
+ scriptSigType: "p2pkh",
396
+ },
397
+ ];
398
+
399
+ const inputs: Input[][] = [
400
+ [
401
+ { outputIndex: 0, scriptSigType: "tbc20_coin" },
402
+ { outputIndex: 2, scriptSigType: "p2pkh" },
403
+ ],
404
+ ];
405
+
406
+ const outputs: Output[][] = [
407
+ [
408
+ { script: newCoinCodeHex, satoshis: 500 },
409
+ { script: newCoinTapeHex, satoshis: 0 },
410
+ { script: p2pkhChangeHex, satoshis: 8000 },
411
+ ],
412
+ ];
413
+
414
+ const { txraws } = await Turing.signAssociatedTransaction({
415
+ sourceTxraw,
416
+ sourceUtxos,
417
+ inputs,
418
+ outputs,
419
+ });
420
+ ```
421
+
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
+ }
266
431
  ```
267
432
 
268
433
  ## sendTransaction
@@ -314,7 +479,7 @@ interface RequestParam {
314
479
  poolNFT_version?: 1 | 2; // 强制为 2,若提供为别的值转为 2
315
480
  serviceFeeRate?: number;
316
481
  serverProvider_tag?: string;
317
- lpPlan?: 1 | 2; // 默认 1
482
+ lpPlan?: 1 | 2 | 3 | 4 | 5; // 默认 1
318
483
  domain?: string;
319
484
  isLockTime?: boolean;
320
485
  lockTime?: number | string; // 锁仓至指定区块高度(POOLNFT 相关),或冻结至指定 unix 时间戳(STABLECOIN_FREEZE),大数请使用 string
@@ -481,12 +646,12 @@ const params = [
481
646
  ft_contract_address: "", // 必填,FT 合约地址
482
647
  serverProvider_tag: "", // 必填,服务提供商标签
483
648
  poolNFT_version: 2, // 可选,强制为 2
484
- serviceFeeRate: 25, // 可选,0-100 整数,默认 25
649
+ serviceFeeRate: 25, // 可选,正整数,默认 25
485
650
  with_lock: false, // 可选,默认 false;为 true 时创建带哈希锁的池子
486
651
  pubKeyLock: ["pubkey1", "pubkey2"], // with_lock 为 true 时必填
487
652
  lpCostAddress: "", // with_lock 为 true 时必填,扣除流动性添加成本的地址
488
653
  lpCostAmount: 0, // with_lock 为 true 时必填,扣除流动性添加成本的 TBC 数量
489
- lpPlan: 1, // 可选,1 或 2,默认 1
654
+ lpPlan: 1, // 可选,1-5,默认 1
490
655
  isLockTime: false, // 可选,默认 false
491
656
  broadcastEnabled: true, // 可选,默认 true
492
657
  domain: "", // 可选
@@ -600,7 +765,7 @@ const params = [
600
765
  address: "", // 必填,接收地址
601
766
  tbc_amount: 0, // 必填,用于交换的 TBC 数量
602
767
  poolNFT_version: 2, // 可选,强制为 2
603
- lpPlan: 1, // 可选,1 或 2,默认 1
768
+ lpPlan: 1, // 可选,1-5,默认 1
604
769
  broadcastEnabled: true, // 可选,默认 true
605
770
  domain: "", // 可选
606
771
  },
@@ -623,7 +788,7 @@ const params = [
623
788
  address: "", // 必填,接收地址
624
789
  ft_amount: 0, // 必填,用于交换的 FT 数量
625
790
  poolNFT_version: 2, // 可选,强制为 2
626
- lpPlan: 1, // 可选,1 或 2,默认 1
791
+ lpPlan: 1, // 可选,1-5,默认 1
627
792
  broadcastEnabled: true, // 可选,默认 true
628
793
  domain: "", // 可选
629
794
  },
@@ -62,7 +62,7 @@ export type SendTransaction = {
62
62
  poolNFT_version?: 1 | 2;
63
63
  serviceFeeRate?: number;
64
64
  serviceProvider_flag?: string;
65
- lpPlan?: 1 | 2;
65
+ lpPlan?: 1 | 2 | 3 | 4 | 5;
66
66
  domain?: string;
67
67
  isLockTime?: boolean;
68
68
  lockTime?: number | string;
@@ -125,7 +125,7 @@ export type Input = {
125
125
  script?: string;
126
126
  satoshis?: number;
127
127
  outputIndex: number;
128
- scriptSigType: "p2pkh" | "tbc20" | "tbc20_contract" | "other";
128
+ scriptSigType: "p2pkh" | "tbc20" | "tbc20_contract" | "tbc20_coin" | "tbc20_coin_contract" | "other";
129
129
  unfinishedScriptSig?: string;
130
130
  ftVersion?: 1 | 2;
131
131
  contractTxId?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "turing-wallet-provider",
3
- "version": "1.5.1",
3
+ "version": "1.5.3",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [