@ckb-ccc/core 0.0.4 → 0.0.5-alpha.0

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.
Files changed (99) hide show
  1. package/README.md +6 -0
  2. package/dist/ckb/transaction.d.ts +85 -2
  3. package/dist/ckb/transaction.d.ts.map +1 -1
  4. package/dist/ckb/transaction.js +152 -7
  5. package/dist/client/client.d.ts +8 -3
  6. package/dist/client/client.d.ts.map +1 -1
  7. package/dist/client/client.js +29 -2
  8. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  9. package/dist/client/clientPublicMainnet.advanced.js +4 -0
  10. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  11. package/dist/client/clientPublicTestnet.advanced.js +4 -0
  12. package/dist/client/clientTypes.d.ts +48 -1
  13. package/dist/client/clientTypes.d.ts.map +1 -1
  14. package/dist/client/clientTypes.js +27 -1
  15. package/dist/client/jsonRpc/index.d.ts +9 -1
  16. package/dist/client/jsonRpc/index.d.ts.map +1 -1
  17. package/dist/client/jsonRpc/index.js +15 -1
  18. package/dist/client/jsonRpc/transformers.d.ts +17 -3
  19. package/dist/client/jsonRpc/transformers.d.ts.map +1 -1
  20. package/dist/client/jsonRpc/transformers.js +37 -3
  21. package/dist/client/jsonRpc/types.d.ts +15 -0
  22. package/dist/client/jsonRpc/types.d.ts.map +1 -1
  23. package/dist/fixedPoint/index.d.ts.map +1 -1
  24. package/dist/fixedPoint/index.js +4 -1
  25. package/dist/fixedPoint/index.test.d.ts +2 -0
  26. package/dist/fixedPoint/index.test.d.ts.map +1 -0
  27. package/dist/fixedPoint/index.test.js +43 -0
  28. package/dist/signer/btc/signerBtc.d.ts.map +1 -1
  29. package/dist/signer/btc/signerBtc.js +2 -3
  30. package/dist/signer/evm/signerEvm.d.ts.map +1 -1
  31. package/dist/signer/evm/signerEvm.js +2 -3
  32. package/dist/signer/index.d.ts +0 -1
  33. package/dist/signer/index.d.ts.map +1 -1
  34. package/dist/signer/index.js +0 -1
  35. package/dist/signer/signer.d.ts +10 -2
  36. package/dist/signer/signer.d.ts.map +1 -1
  37. package/dist/signer/signer.js +9 -1
  38. package/dist.commonjs/ckb/transaction.d.ts +85 -2
  39. package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
  40. package/dist.commonjs/ckb/transaction.js +150 -5
  41. package/dist.commonjs/client/client.d.ts +8 -3
  42. package/dist.commonjs/client/client.d.ts.map +1 -1
  43. package/dist.commonjs/client/client.js +28 -1
  44. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  45. package/dist.commonjs/client/clientPublicMainnet.advanced.js +4 -0
  46. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  47. package/dist.commonjs/client/clientPublicTestnet.advanced.js +4 -0
  48. package/dist.commonjs/client/clientTypes.d.ts +48 -1
  49. package/dist.commonjs/client/clientTypes.d.ts.map +1 -1
  50. package/dist.commonjs/client/clientTypes.js +29 -0
  51. package/dist.commonjs/client/jsonRpc/index.d.ts +9 -1
  52. package/dist.commonjs/client/jsonRpc/index.d.ts.map +1 -1
  53. package/dist.commonjs/client/jsonRpc/index.js +15 -1
  54. package/dist.commonjs/client/jsonRpc/transformers.d.ts +17 -3
  55. package/dist.commonjs/client/jsonRpc/transformers.d.ts.map +1 -1
  56. package/dist.commonjs/client/jsonRpc/transformers.js +35 -1
  57. package/dist.commonjs/client/jsonRpc/types.d.ts +15 -0
  58. package/dist.commonjs/client/jsonRpc/types.d.ts.map +1 -1
  59. package/dist.commonjs/fixedPoint/index.d.ts.map +1 -1
  60. package/dist.commonjs/fixedPoint/index.js +4 -1
  61. package/dist.commonjs/fixedPoint/index.test.d.ts +2 -0
  62. package/dist.commonjs/fixedPoint/index.test.d.ts.map +1 -0
  63. package/dist.commonjs/fixedPoint/index.test.js +45 -0
  64. package/dist.commonjs/signer/btc/signerBtc.d.ts.map +1 -1
  65. package/dist.commonjs/signer/btc/signerBtc.js +2 -3
  66. package/dist.commonjs/signer/evm/signerEvm.d.ts.map +1 -1
  67. package/dist.commonjs/signer/evm/signerEvm.js +2 -3
  68. package/dist.commonjs/signer/index.d.ts +0 -1
  69. package/dist.commonjs/signer/index.d.ts.map +1 -1
  70. package/dist.commonjs/signer/index.js +0 -1
  71. package/dist.commonjs/signer/signer.d.ts +10 -2
  72. package/dist.commonjs/signer/signer.d.ts.map +1 -1
  73. package/dist.commonjs/signer/signer.js +9 -1
  74. package/dist.commonjs/tsconfig.commonjs.tsbuildinfo +1 -1
  75. package/jest.config.js +5 -0
  76. package/package.json +6 -2
  77. package/src/ckb/transaction.ts +198 -3
  78. package/src/client/client.ts +64 -3
  79. package/src/client/clientPublicMainnet.advanced.ts +5 -0
  80. package/src/client/clientPublicTestnet.advanced.ts +5 -0
  81. package/src/client/clientTypes.ts +70 -1
  82. package/src/client/jsonRpc/index.ts +38 -2
  83. package/src/client/jsonRpc/transformers.ts +73 -4
  84. package/src/client/jsonRpc/types.ts +16 -0
  85. package/src/fixedPoint/index.test.ts +55 -0
  86. package/src/fixedPoint/index.ts +7 -1
  87. package/src/signer/btc/signerBtc.ts +6 -3
  88. package/src/signer/evm/signerEvm.ts +2 -3
  89. package/src/signer/index.ts +0 -1
  90. package/src/signer/signer.ts +9 -0
  91. package/dist/signer/helpers.d.ts +0 -40
  92. package/dist/signer/helpers.d.ts.map +0 -1
  93. package/dist/signer/helpers.js +0 -91
  94. package/dist.commonjs/signer/helpers.d.ts +0 -40
  95. package/dist.commonjs/signer/helpers.d.ts.map +0 -1
  96. package/dist.commonjs/signer/helpers.js +0 -96
  97. package/src/signer/helpers.ts +0 -124
  98. package/tsconfig.base.json +0 -21
  99. package/tsconfig.commonjs.json +0 -8
@@ -0,0 +1,55 @@
1
+ import { fixedPointFrom, fixedPointToString } from ".";
2
+
3
+ describe("fixedPointToString", () => {
4
+ const cases: [bigint, string, number?][] = [
5
+ [0n, "0"],
6
+ [1n, "0.00000001"],
7
+ [100000000n, "1"],
8
+ [1000000000n, "10"],
9
+ [1010100000n, "10.101"],
10
+ [11n, "1.1", 1],
11
+ [0n, "0", 1],
12
+ [1n, "1", 0],
13
+ [0n, "0", 0],
14
+ ];
15
+
16
+ cases.forEach(([i, o, decimals]) =>
17
+ test(`${i} with ${decimals ?? "default"} decimals = "${o}"`, () => {
18
+ expect(fixedPointToString(i, decimals)).toBe(o);
19
+ }),
20
+ );
21
+ });
22
+
23
+ describe("fixedPointFrom string", () => {
24
+ const cases: [string, bigint, number?][] = [
25
+ ["0", 0n],
26
+ ["0.00000001", 1n],
27
+ ["1", 100000000n],
28
+ ["10", 1000000000n],
29
+ ["10.101", 1010100000n],
30
+ ["1.1", 11n, 1],
31
+ ["0", 0n, 1],
32
+ ["1", 1n, 0],
33
+ ["0", 0n, 0],
34
+ ];
35
+
36
+ cases.forEach(([i, o, decimals]) =>
37
+ test(`"${i}" = ${o} with ${decimals ?? "default"} decimals `, () => {
38
+ expect(fixedPointFrom(i, decimals)).toBe(o);
39
+ }),
40
+ );
41
+ });
42
+
43
+ describe("fixedPointFrom number", () => {
44
+ const cases: [number, bigint, number?][] = [
45
+ [0.00000001, 1n],
46
+ [10.101, 1010100000n],
47
+ [1.1, 11n, 1],
48
+ ];
49
+
50
+ cases.forEach(([i, o, decimals]) =>
51
+ test(`${i} = ${o} with ${decimals ?? "default"} decimals `, () => {
52
+ expect(fixedPointFrom(i, decimals)).toBe(o);
53
+ }),
54
+ );
55
+ });
@@ -28,6 +28,10 @@ export type FixedPointLike = bigint | string | number;
28
28
 
29
29
  export function fixedPointToString(val: FixedPointLike, decimals = 8): string {
30
30
  const str = fixedPointFrom(val).toString();
31
+ if (decimals === 0) {
32
+ return str;
33
+ }
34
+
31
35
  const l = str.length <= decimals ? "0" : str.slice(0, -decimals);
32
36
  const r = str.slice(-decimals).padStart(decimals, "0").replace(/0*$/, "");
33
37
  if (r === "") {
@@ -57,7 +61,9 @@ export function fixedPointFrom(val: FixedPointLike, decimals = 8): FixedPoint {
57
61
  return val;
58
62
  }
59
63
 
60
- const [l, r] = val.toString().split(".");
64
+ const [l, r] = (
65
+ typeof val === "number" ? val.toFixed(decimals) : val.toString()
66
+ ).split(".");
61
67
  const lVal = BigInt(l.padEnd(l.length + decimals, "0"));
62
68
  if (r === undefined) {
63
69
  return lVal;
@@ -6,7 +6,6 @@ import { Transaction, TransactionLike, WitnessArgs } from "../../ckb";
6
6
  import { KnownScript } from "../../client";
7
7
  import { HexLike, hexFrom } from "../../hex";
8
8
  import { numToBytes } from "../../num";
9
- import { getSignHashInfo, prepareSighashAllWitness } from "../helpers";
10
9
  import { Signer } from "../signer";
11
10
 
12
11
  /**
@@ -64,7 +63,11 @@ export abstract class SignerBtc extends Signer {
64
63
  */
65
64
  async prepareTransaction(txLike: TransactionLike): Promise<Transaction> {
66
65
  const { script } = await this.getRecommendedAddressObj();
67
- return prepareSighashAllWitness(txLike, script, 85, this.client);
66
+ return Transaction.from(txLike).prepareSighashAllWitness(
67
+ script,
68
+ 85,
69
+ this.client,
70
+ );
68
71
  }
69
72
 
70
73
  /**
@@ -76,7 +79,7 @@ export abstract class SignerBtc extends Signer {
76
79
  async signOnlyTransaction(txLike: TransactionLike): Promise<Transaction> {
77
80
  const tx = Transaction.from(txLike);
78
81
  const { script } = await this.getRecommendedAddressObj();
79
- const info = await getSignHashInfo(tx, script, this.client);
82
+ const info = await tx.getSignHashInfo(script, this.client);
80
83
  if (!info) {
81
84
  return tx;
82
85
  }
@@ -4,7 +4,6 @@ import { Script, Transaction, TransactionLike, WitnessArgs } from "../../ckb";
4
4
  import { KnownScript } from "../../client";
5
5
  import { hexFrom } from "../../hex";
6
6
  import { numToBytes } from "../../num";
7
- import { getSignHashInfo, prepareSighashAllWitness } from "../helpers";
8
7
  import { Signer } from "../signer";
9
8
 
10
9
  /**
@@ -69,7 +68,7 @@ export abstract class SignerEvm extends Signer {
69
68
  return addresses.reduce(
70
69
  (txPromise, { script }) =>
71
70
  txPromise.then((tx) =>
72
- prepareSighashAllWitness(tx, script, 85, this.client),
71
+ tx.prepareSighashAllWitness(script, 85, this.client),
73
72
  ),
74
73
  Promise.resolve(Transaction.from(txLike)),
75
74
  );
@@ -108,7 +107,7 @@ export abstract class SignerEvm extends Signer {
108
107
  script: Script,
109
108
  messageTransformer: (hash: string) => BytesLike,
110
109
  ): Promise<Transaction> {
111
- const info = await getSignHashInfo(tx, script, this.client);
110
+ const info = await tx.getSignHashInfo(script, this.client);
112
111
  if (!info) {
113
112
  return tx;
114
113
  }
@@ -1,5 +1,4 @@
1
1
  export * from "./btc";
2
2
  export * from "./ckb";
3
3
  export * from "./evm";
4
- export * from "./helpers";
5
4
  export * from "./signer";
@@ -29,6 +29,13 @@ export abstract class Signer {
29
29
  */
30
30
  abstract connect(): Promise<void>;
31
31
 
32
+ /**
33
+ * Disconnects to the signer.
34
+ *
35
+ * @returns A promise that resolves when disconnected.
36
+ */
37
+ async disconnect(): Promise<void> {}
38
+
32
39
  /**
33
40
  * Check if the signer is connected.
34
41
  *
@@ -142,6 +149,7 @@ export abstract class Signer {
142
149
  export enum SignerType {
143
150
  EVM = "EVM",
144
151
  BTC = "BTC",
152
+ CKB = "CKB",
145
153
  }
146
154
 
147
155
  /**
@@ -149,6 +157,7 @@ export enum SignerType {
149
157
  */
150
158
  export class SignerInfo {
151
159
  constructor(
160
+ public name: string,
152
161
  public type: SignerType,
153
162
  public signer: Signer,
154
163
  ) {}
@@ -1,40 +0,0 @@
1
- import { ScriptLike, Transaction, TransactionLike } from "../ckb";
2
- import { Client } from "../client";
3
- import { Hex } from "../hex";
4
- /**
5
- * Computes the signing hash information for a given transaction and script.
6
- *
7
- * @param txLike - The transaction to sign, represented as a TransactionLike object.
8
- * @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
9
- * @param client - The client for complete extra infos in the transaction.
10
- * @returns A promise that resolves to an object containing the signing message and the witness position,
11
- * or undefined if no matching input is found.
12
- *
13
- * @example
14
- * ```typescript
15
- * const signHashInfo = await getSignHashInfo(transactionLike, scriptLike, client);
16
- * if (signHashInfo) {
17
- * console.log(signHashInfo.message); // Outputs the signing message
18
- * console.log(signHashInfo.position); // Outputs the witness position
19
- * }
20
- * ```
21
- */
22
- export declare function getSignHashInfo(txLike: TransactionLike, scriptLike: ScriptLike, client: Client): Promise<{
23
- message: Hex;
24
- position: number;
25
- } | undefined>;
26
- /**
27
- * Prepare dummy witness for sighash all method
28
- *
29
- * @param txLike - The transaction to prepare, represented as a TransactionLike object.
30
- * @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
31
- * @param client - The client for complete extra infos in the transaction.
32
- * @returns A promise that resolves to the prepared transaction
33
- *
34
- * @example
35
- * ```typescript
36
- * const tx = await prepareSighashAllWitness(transactionLike, scriptLike, client);
37
- * ```
38
- */
39
- export declare function prepareSighashAllWitness(txLike: TransactionLike, scriptLike: ScriptLike, lockLen: number, client: Client): Promise<Transaction>;
40
- //# sourceMappingURL=helpers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/signer/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,UAAU,EACV,WAAW,EACX,eAAe,EAEhB,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnC,OAAO,EAAE,GAAG,EAAW,MAAM,QAAQ,CAAC;AAEtC;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,eAAe,CACnC,MAAM,EAAE,eAAe,EACvB,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC;IAAE,OAAO,EAAE,GAAG,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,CAAC,CAuCzD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,wBAAwB,CAC5C,MAAM,EAAE,eAAe,EACvB,UAAU,EAAE,UAAU,EACtB,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,WAAW,CAAC,CA+BtB"}
@@ -1,91 +0,0 @@
1
- import { Script, Transaction, WitnessArgs, } from "../ckb";
2
- import { Hasher } from "../hasher";
3
- import { hexFrom } from "../hex";
4
- /**
5
- * Computes the signing hash information for a given transaction and script.
6
- *
7
- * @param txLike - The transaction to sign, represented as a TransactionLike object.
8
- * @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
9
- * @param client - The client for complete extra infos in the transaction.
10
- * @returns A promise that resolves to an object containing the signing message and the witness position,
11
- * or undefined if no matching input is found.
12
- *
13
- * @example
14
- * ```typescript
15
- * const signHashInfo = await getSignHashInfo(transactionLike, scriptLike, client);
16
- * if (signHashInfo) {
17
- * console.log(signHashInfo.message); // Outputs the signing message
18
- * console.log(signHashInfo.position); // Outputs the witness position
19
- * }
20
- * ```
21
- */
22
- export async function getSignHashInfo(txLike, scriptLike, client) {
23
- const tx = Transaction.from(txLike);
24
- const script = Script.from(scriptLike);
25
- let position = -1;
26
- const hasher = new Hasher();
27
- hasher.update(tx.hash());
28
- for (let i = 0; i < tx.witnesses.length; i += 1) {
29
- if (tx.inputs[i]) {
30
- const input = await tx.inputs[i].completeExtraInfos(client);
31
- if (!input.cellOutput) {
32
- throw Error("Unable to resolve inputs info");
33
- }
34
- if (!script.eq(input.cellOutput.lock)) {
35
- continue;
36
- }
37
- if (position === -1) {
38
- position = i;
39
- }
40
- }
41
- if (position === -1) {
42
- return undefined;
43
- }
44
- Transaction.hashWitnessToHasher(tx.witnesses[i], hasher);
45
- }
46
- if (position === -1) {
47
- return undefined;
48
- }
49
- return {
50
- message: hasher.digest(),
51
- position,
52
- };
53
- }
54
- /**
55
- * Prepare dummy witness for sighash all method
56
- *
57
- * @param txLike - The transaction to prepare, represented as a TransactionLike object.
58
- * @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
59
- * @param client - The client for complete extra infos in the transaction.
60
- * @returns A promise that resolves to the prepared transaction
61
- *
62
- * @example
63
- * ```typescript
64
- * const tx = await prepareSighashAllWitness(transactionLike, scriptLike, client);
65
- * ```
66
- */
67
- export async function prepareSighashAllWitness(txLike, scriptLike, lockLen, client) {
68
- const tx = Transaction.from(txLike);
69
- const script = Script.from(scriptLike);
70
- let position = -1;
71
- for (let i = 0; i < tx.inputs.length; i += 1) {
72
- const input = await tx.inputs[i].completeExtraInfos(client);
73
- if (!input.cellOutput) {
74
- throw Error("Unable to resolve inputs info");
75
- }
76
- if (script.eq(input.cellOutput.lock)) {
77
- position = i;
78
- break;
79
- }
80
- }
81
- if (position === -1) {
82
- return tx;
83
- }
84
- const rawWitness = tx.witnesses[position];
85
- const witness = (rawWitness ?? "0x") !== "0x"
86
- ? WitnessArgs.fromBytes(rawWitness)
87
- : WitnessArgs.from({});
88
- witness.lock = hexFrom(Array.from(new Array(lockLen), () => 0));
89
- tx.witnesses[position] = hexFrom(witness.toBytes());
90
- return tx;
91
- }
@@ -1,40 +0,0 @@
1
- import { ScriptLike, Transaction, TransactionLike } from "../ckb";
2
- import { Client } from "../client";
3
- import { Hex } from "../hex";
4
- /**
5
- * Computes the signing hash information for a given transaction and script.
6
- *
7
- * @param txLike - The transaction to sign, represented as a TransactionLike object.
8
- * @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
9
- * @param client - The client for complete extra infos in the transaction.
10
- * @returns A promise that resolves to an object containing the signing message and the witness position,
11
- * or undefined if no matching input is found.
12
- *
13
- * @example
14
- * ```typescript
15
- * const signHashInfo = await getSignHashInfo(transactionLike, scriptLike, client);
16
- * if (signHashInfo) {
17
- * console.log(signHashInfo.message); // Outputs the signing message
18
- * console.log(signHashInfo.position); // Outputs the witness position
19
- * }
20
- * ```
21
- */
22
- export declare function getSignHashInfo(txLike: TransactionLike, scriptLike: ScriptLike, client: Client): Promise<{
23
- message: Hex;
24
- position: number;
25
- } | undefined>;
26
- /**
27
- * Prepare dummy witness for sighash all method
28
- *
29
- * @param txLike - The transaction to prepare, represented as a TransactionLike object.
30
- * @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
31
- * @param client - The client for complete extra infos in the transaction.
32
- * @returns A promise that resolves to the prepared transaction
33
- *
34
- * @example
35
- * ```typescript
36
- * const tx = await prepareSighashAllWitness(transactionLike, scriptLike, client);
37
- * ```
38
- */
39
- export declare function prepareSighashAllWitness(txLike: TransactionLike, scriptLike: ScriptLike, lockLen: number, client: Client): Promise<Transaction>;
40
- //# sourceMappingURL=helpers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/signer/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,UAAU,EACV,WAAW,EACX,eAAe,EAEhB,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnC,OAAO,EAAE,GAAG,EAAW,MAAM,QAAQ,CAAC;AAEtC;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,eAAe,CACnC,MAAM,EAAE,eAAe,EACvB,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC;IAAE,OAAO,EAAE,GAAG,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,CAAC,CAuCzD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,wBAAwB,CAC5C,MAAM,EAAE,eAAe,EACvB,UAAU,EAAE,UAAU,EACtB,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,WAAW,CAAC,CA+BtB"}
@@ -1,96 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.prepareSighashAllWitness = exports.getSignHashInfo = void 0;
4
- const ckb_1 = require("../ckb");
5
- const hasher_1 = require("../hasher");
6
- const hex_1 = require("../hex");
7
- /**
8
- * Computes the signing hash information for a given transaction and script.
9
- *
10
- * @param txLike - The transaction to sign, represented as a TransactionLike object.
11
- * @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
12
- * @param client - The client for complete extra infos in the transaction.
13
- * @returns A promise that resolves to an object containing the signing message and the witness position,
14
- * or undefined if no matching input is found.
15
- *
16
- * @example
17
- * ```typescript
18
- * const signHashInfo = await getSignHashInfo(transactionLike, scriptLike, client);
19
- * if (signHashInfo) {
20
- * console.log(signHashInfo.message); // Outputs the signing message
21
- * console.log(signHashInfo.position); // Outputs the witness position
22
- * }
23
- * ```
24
- */
25
- async function getSignHashInfo(txLike, scriptLike, client) {
26
- const tx = ckb_1.Transaction.from(txLike);
27
- const script = ckb_1.Script.from(scriptLike);
28
- let position = -1;
29
- const hasher = new hasher_1.Hasher();
30
- hasher.update(tx.hash());
31
- for (let i = 0; i < tx.witnesses.length; i += 1) {
32
- if (tx.inputs[i]) {
33
- const input = await tx.inputs[i].completeExtraInfos(client);
34
- if (!input.cellOutput) {
35
- throw Error("Unable to resolve inputs info");
36
- }
37
- if (!script.eq(input.cellOutput.lock)) {
38
- continue;
39
- }
40
- if (position === -1) {
41
- position = i;
42
- }
43
- }
44
- if (position === -1) {
45
- return undefined;
46
- }
47
- ckb_1.Transaction.hashWitnessToHasher(tx.witnesses[i], hasher);
48
- }
49
- if (position === -1) {
50
- return undefined;
51
- }
52
- return {
53
- message: hasher.digest(),
54
- position,
55
- };
56
- }
57
- exports.getSignHashInfo = getSignHashInfo;
58
- /**
59
- * Prepare dummy witness for sighash all method
60
- *
61
- * @param txLike - The transaction to prepare, represented as a TransactionLike object.
62
- * @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
63
- * @param client - The client for complete extra infos in the transaction.
64
- * @returns A promise that resolves to the prepared transaction
65
- *
66
- * @example
67
- * ```typescript
68
- * const tx = await prepareSighashAllWitness(transactionLike, scriptLike, client);
69
- * ```
70
- */
71
- async function prepareSighashAllWitness(txLike, scriptLike, lockLen, client) {
72
- const tx = ckb_1.Transaction.from(txLike);
73
- const script = ckb_1.Script.from(scriptLike);
74
- let position = -1;
75
- for (let i = 0; i < tx.inputs.length; i += 1) {
76
- const input = await tx.inputs[i].completeExtraInfos(client);
77
- if (!input.cellOutput) {
78
- throw Error("Unable to resolve inputs info");
79
- }
80
- if (script.eq(input.cellOutput.lock)) {
81
- position = i;
82
- break;
83
- }
84
- }
85
- if (position === -1) {
86
- return tx;
87
- }
88
- const rawWitness = tx.witnesses[position];
89
- const witness = (rawWitness ?? "0x") !== "0x"
90
- ? ckb_1.WitnessArgs.fromBytes(rawWitness)
91
- : ckb_1.WitnessArgs.from({});
92
- witness.lock = (0, hex_1.hexFrom)(Array.from(new Array(lockLen), () => 0));
93
- tx.witnesses[position] = (0, hex_1.hexFrom)(witness.toBytes());
94
- return tx;
95
- }
96
- exports.prepareSighashAllWitness = prepareSighashAllWitness;
@@ -1,124 +0,0 @@
1
- import {
2
- Script,
3
- ScriptLike,
4
- Transaction,
5
- TransactionLike,
6
- WitnessArgs,
7
- } from "../ckb";
8
- import { Client } from "../client";
9
- import { Hasher } from "../hasher";
10
- import { Hex, hexFrom } from "../hex";
11
-
12
- /**
13
- * Computes the signing hash information for a given transaction and script.
14
- *
15
- * @param txLike - The transaction to sign, represented as a TransactionLike object.
16
- * @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
17
- * @param client - The client for complete extra infos in the transaction.
18
- * @returns A promise that resolves to an object containing the signing message and the witness position,
19
- * or undefined if no matching input is found.
20
- *
21
- * @example
22
- * ```typescript
23
- * const signHashInfo = await getSignHashInfo(transactionLike, scriptLike, client);
24
- * if (signHashInfo) {
25
- * console.log(signHashInfo.message); // Outputs the signing message
26
- * console.log(signHashInfo.position); // Outputs the witness position
27
- * }
28
- * ```
29
- */
30
- export async function getSignHashInfo(
31
- txLike: TransactionLike,
32
- scriptLike: ScriptLike,
33
- client: Client,
34
- ): Promise<{ message: Hex; position: number } | undefined> {
35
- const tx = Transaction.from(txLike);
36
- const script = Script.from(scriptLike);
37
- let position = -1;
38
- const hasher = new Hasher();
39
- hasher.update(tx.hash());
40
-
41
- for (let i = 0; i < tx.witnesses.length; i += 1) {
42
- if (tx.inputs[i]) {
43
- const input = await tx.inputs[i].completeExtraInfos(client);
44
-
45
- if (!input.cellOutput) {
46
- throw Error("Unable to resolve inputs info");
47
- }
48
-
49
- if (!script.eq(input.cellOutput.lock)) {
50
- continue;
51
- }
52
-
53
- if (position === -1) {
54
- position = i;
55
- }
56
- }
57
-
58
- if (position === -1) {
59
- return undefined;
60
- }
61
-
62
- Transaction.hashWitnessToHasher(tx.witnesses[i], hasher);
63
- }
64
-
65
- if (position === -1) {
66
- return undefined;
67
- }
68
-
69
- return {
70
- message: hasher.digest(),
71
- position,
72
- };
73
- }
74
-
75
- /**
76
- * Prepare dummy witness for sighash all method
77
- *
78
- * @param txLike - The transaction to prepare, represented as a TransactionLike object.
79
- * @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
80
- * @param client - The client for complete extra infos in the transaction.
81
- * @returns A promise that resolves to the prepared transaction
82
- *
83
- * @example
84
- * ```typescript
85
- * const tx = await prepareSighashAllWitness(transactionLike, scriptLike, client);
86
- * ```
87
- */
88
- export async function prepareSighashAllWitness(
89
- txLike: TransactionLike,
90
- scriptLike: ScriptLike,
91
- lockLen: number,
92
- client: Client,
93
- ): Promise<Transaction> {
94
- const tx = Transaction.from(txLike);
95
- const script = Script.from(scriptLike);
96
-
97
- let position = -1;
98
-
99
- for (let i = 0; i < tx.inputs.length; i += 1) {
100
- const input = await tx.inputs[i].completeExtraInfos(client);
101
-
102
- if (!input.cellOutput) {
103
- throw Error("Unable to resolve inputs info");
104
- }
105
-
106
- if (script.eq(input.cellOutput.lock)) {
107
- position = i;
108
- break;
109
- }
110
- }
111
- if (position === -1) {
112
- return tx;
113
- }
114
-
115
- const rawWitness = tx.witnesses[position];
116
- const witness =
117
- (rawWitness ?? "0x") !== "0x"
118
- ? WitnessArgs.fromBytes(rawWitness)
119
- : WitnessArgs.from({});
120
- witness.lock = hexFrom(Array.from(new Array(lockLen), () => 0));
121
- tx.witnesses[position] = hexFrom(witness.toBytes());
122
-
123
- return tx;
124
- }
@@ -1,21 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es2020",
4
- "incremental": true,
5
- "allowJs": true,
6
- "importHelpers": false,
7
- "declaration": true,
8
- "declarationMap": true,
9
- "experimentalDecorators": true,
10
- "useDefineForClassFields": false,
11
- "esModuleInterop": true,
12
- "strict": true,
13
- "noImplicitAny": true,
14
- "strictBindCallApply": true,
15
- "strictNullChecks": true,
16
- "alwaysStrict": true,
17
- "noFallthroughCasesInSwitch": true,
18
- "forceConsistentCasingInFileNames": true
19
- },
20
- "include": ["src/**/*"]
21
- }
@@ -1,8 +0,0 @@
1
- {
2
- "extends": "./tsconfig.base.json",
3
- "compilerOptions": {
4
- "module": "NodeNext",
5
- "moduleResolution": "NodeNext",
6
- "outDir": "./dist.commonjs"
7
- }
8
- }