@ckb-ccc/core 0.0.4-alpha.5 → 0.0.4-alpha.8

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.
@@ -2,7 +2,7 @@ import { Script } from "../ckb";
2
2
  import { KnownScript } from "./client";
3
3
  import { ClientJsonRpc } from "./jsonRpc";
4
4
  export declare class ClientPublicMainnet extends ClientJsonRpc {
5
- constructor(timeout?: number);
5
+ constructor(url?: string, timeout?: number);
6
6
  getAddressPrefix(): Promise<string>;
7
7
  getKnownScript(script: KnownScript): Promise<Pick<Script, "codeHash" | "hashType">>;
8
8
  }
@@ -1 +1 @@
1
- {"version":3,"file":"clientPublicMainnet.d.ts","sourceRoot":"","sources":["../../src/client/clientPublicMainnet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAEvC,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAE1C,qBAAa,mBAAoB,SAAQ,aAAa;gBACxC,OAAO,CAAC,EAAE,MAAM;IAItB,gBAAgB,IAAI,OAAO,CAAC,MAAM,CAAC;IAInC,cAAc,CAClB,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,UAAU,GAAG,UAAU,CAAC,CAAC;CAGlD"}
1
+ {"version":3,"file":"clientPublicMainnet.d.ts","sourceRoot":"","sources":["../../src/client/clientPublicMainnet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAEvC,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAE1C,qBAAa,mBAAoB,SAAQ,aAAa;gBACxC,GAAG,SAAgC,EAAE,OAAO,CAAC,EAAE,MAAM;IAI3D,gBAAgB,IAAI,OAAO,CAAC,MAAM,CAAC;IAInC,cAAc,CAClB,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,UAAU,GAAG,UAAU,CAAC,CAAC;CAGlD"}
@@ -1,8 +1,8 @@
1
1
  import { MAINNET_SCRIPTS } from "./clientPublicMainnet.advanced";
2
2
  import { ClientJsonRpc } from "./jsonRpc";
3
3
  export class ClientPublicMainnet extends ClientJsonRpc {
4
- constructor(timeout) {
5
- super("https://mainnet.ckbapp.dev/", timeout);
4
+ constructor(url = "https://mainnet.ckbapp.dev/", timeout) {
5
+ super(url, timeout);
6
6
  }
7
7
  async getAddressPrefix() {
8
8
  return "ckb";
@@ -2,7 +2,7 @@ import { Script } from "../ckb";
2
2
  import { KnownScript } from "./client";
3
3
  import { ClientJsonRpc } from "./jsonRpc";
4
4
  export declare class ClientPublicTestnet extends ClientJsonRpc {
5
- constructor(timeout?: number);
5
+ constructor(url?: string, timeout?: number);
6
6
  getAddressPrefix(): Promise<string>;
7
7
  getKnownScript(script: KnownScript): Promise<Pick<Script, "codeHash" | "hashType">>;
8
8
  }
@@ -1 +1 @@
1
- {"version":3,"file":"clientPublicTestnet.d.ts","sourceRoot":"","sources":["../../src/client/clientPublicTestnet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAEvC,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAE1C,qBAAa,mBAAoB,SAAQ,aAAa;gBACxC,OAAO,CAAC,EAAE,MAAM;IAItB,gBAAgB,IAAI,OAAO,CAAC,MAAM,CAAC;IAInC,cAAc,CAClB,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,UAAU,GAAG,UAAU,CAAC,CAAC;CAGlD"}
1
+ {"version":3,"file":"clientPublicTestnet.d.ts","sourceRoot":"","sources":["../../src/client/clientPublicTestnet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAEvC,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAE1C,qBAAa,mBAAoB,SAAQ,aAAa;gBACxC,GAAG,SAAgC,EAAE,OAAO,CAAC,EAAE,MAAM;IAI3D,gBAAgB,IAAI,OAAO,CAAC,MAAM,CAAC;IAInC,cAAc,CAClB,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,UAAU,GAAG,UAAU,CAAC,CAAC;CAGlD"}
@@ -1,8 +1,8 @@
1
1
  import { TESTNET_SCRIPTS } from "./clientPublicTestnet.advanced";
2
2
  import { ClientJsonRpc } from "./jsonRpc";
3
3
  export class ClientPublicTestnet extends ClientJsonRpc {
4
- constructor(timeout) {
5
- super("https://testnet.ckbapp.dev/", timeout);
4
+ constructor(url = "https://testnet.ckbapp.dev/", timeout) {
5
+ super(url, timeout);
6
6
  }
7
7
  async getAddressPrefix() {
8
8
  return "ckt";
@@ -32,6 +32,13 @@ export declare abstract class SignerBtc extends Signer {
32
32
  * @returns A promise that resolves to an array of Address objects.
33
33
  */
34
34
  getAddressObjs(): Promise<Address[]>;
35
+ /**
36
+ * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
37
+ *
38
+ * @param txLike - The transaction to prepare, represented as a TransactionLike object.
39
+ * @returns A promise that resolves to the prepared Transaction object.
40
+ */
41
+ prepareTransaction(txLike: TransactionLike): Promise<Transaction>;
35
42
  /**
36
43
  * Signs a transaction without modifying it.
37
44
  *
@@ -1 +1 @@
1
- {"version":3,"file":"signerBtc.d.ts","sourceRoot":"","sources":["../../../src/signer/btc/signerBtc.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAExC,OAAO,EAAE,WAAW,EAAE,eAAe,EAAe,MAAM,WAAW,CAAC;AAEtE,O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AAM,EAAE,eAAe,GAAG,OAAO,CAAC,WAAW,CAAC;CAgCzE"}
@@ -6,7 +6,7 @@ import { Transaction, WitnessArgs } from "../../ckb";
6
6
  import { KnownScript } from "../../client";
7
7
  import { hexFrom } from "../../hex";
8
8
  import { numToBytes } from "../../num";
9
- import { getSignHashInfo } from "../helpers";
9
+ import { getSignHashInfo, prepareSighashAllWitness } from "../helpers";
10
10
  import { Signer } from "../signer";
11
11
  /**
12
12
  * An abstract class extending the Signer class for Bitcoin-like signing operations.
@@ -34,6 +34,16 @@ export class SignerBtc extends Signer {
34
34
  await Address.fromKnownScript(KnownScript.OmniLock, hexFrom([0x04, ...hash, 0x00]), this.client),
35
35
  ];
36
36
  }
37
+ /**
38
+ * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
39
+ *
40
+ * @param txLike - The transaction to prepare, represented as a TransactionLike object.
41
+ * @returns A promise that resolves to the prepared Transaction object.
42
+ */
43
+ async prepareTransaction(txLike) {
44
+ const { script } = await this.getRecommendedAddressObj();
45
+ return prepareSighashAllWitness(txLike, script, 85, this.client);
46
+ }
37
47
  /**
38
48
  * Signs a transaction without modifying it.
39
49
  *
@@ -24,6 +24,13 @@ export declare abstract class SignerEvm extends Signer {
24
24
  * @returns A promise that resolves to an array of Address objects.
25
25
  */
26
26
  getAddressObjs(): Promise<Address[]>;
27
+ /**
28
+ * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
29
+ *
30
+ * @param txLike - The transaction to prepare, represented as a TransactionLike object.
31
+ * @returns A promise that resolves to the prepared Transaction object.
32
+ */
33
+ prepareTransaction(txLike: TransactionLike): Promise<Transaction>;
27
34
  /**
28
35
  * Signs a transaction without modifying it.
29
36
  *
@@ -1 +1 @@
1
- {"version":3,"file":"signerEvm.d.ts","sourceRoot":"","sources":["../../../src/signer/evm/signerEvm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAExC,OAAO,EAAE,WAAW,EAAE,eAAe,EAAe,MAAM,WAAW,CAAC;AAKtE,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnC;;;GAGG;AACH,8BAAsB,SAAU,SAAQ,MAAM;IAC5C;;;;OAIG;IACH,QAAQ,CAAC,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;IAEzC;;;;OAIG;IACG,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAI3C;;;;OAIG;IACG,cAAc,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;IAW1C;;;;;OAKG;IACG,mBAAmB,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,WAAW,CAAC;CAgCzE"}
1
+ {"version":3,"file":"signerEvm.d.ts","sourceRoot":"","sources":["../../../src/signer/evm/signerEvm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAExC,OAAO,EAAE,WAAW,EAAE,eAAe,EAAe,MAAM,WAAW,CAAC;AAKtE,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnC;;;GAGG;AACH,8BAAsB,SAAU,SAAQ,MAAM;IAC5C;;;;OAIG;IACH,QAAQ,CAAC,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;IAEzC;;;;OAIG;IACG,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAI3C;;;;OAIG;IACG,cAAc,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;IAW1C;;;;;OAKG;IACG,kBAAkB,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,WAAW,CAAC;IAKvE;;;;;OAKG;IACG,mBAAmB,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,WAAW,CAAC;CAgCzE"}
@@ -4,7 +4,7 @@ import { Transaction, WitnessArgs } from "../../ckb";
4
4
  import { KnownScript } from "../../client";
5
5
  import { hexFrom } from "../../hex";
6
6
  import { numToBytes } from "../../num";
7
- import { getSignHashInfo } from "../helpers";
7
+ import { getSignHashInfo, prepareSighashAllWitness } from "../helpers";
8
8
  import { Signer } from "../signer";
9
9
  /**
10
10
  * An abstract class extending Signer for Ethereum Virtual Machine (EVM) based signing operations.
@@ -30,6 +30,16 @@ export class SignerEvm extends Signer {
30
30
  await Address.fromKnownScript(KnownScript.OmniLock, hexFrom([0x12, ...bytesFrom(account), 0x00]), this.client),
31
31
  ];
32
32
  }
33
+ /**
34
+ * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
35
+ *
36
+ * @param txLike - The transaction to prepare, represented as a TransactionLike object.
37
+ * @returns A promise that resolves to the prepared Transaction object.
38
+ */
39
+ async prepareTransaction(txLike) {
40
+ const { script } = await this.getRecommendedAddressObj();
41
+ return prepareSighashAllWitness(txLike, script, 85, this.client);
42
+ }
33
43
  /**
34
44
  * Signs a transaction without modifying it.
35
45
  *
@@ -1,4 +1,4 @@
1
- import { ScriptLike, TransactionLike } from "../ckb";
1
+ import { ScriptLike, Transaction, TransactionLike } from "../ckb";
2
2
  import { Client } from "../client";
3
3
  import { Hex } from "../hex";
4
4
  /**
@@ -6,12 +6,13 @@ import { Hex } from "../hex";
6
6
  *
7
7
  * @param txLike - The transaction to sign, represented as a TransactionLike object.
8
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.
9
10
  * @returns A promise that resolves to an object containing the signing message and the witness position,
10
11
  * or undefined if no matching input is found.
11
12
  *
12
13
  * @example
13
14
  * ```typescript
14
- * const signHashInfo = await getSignHashInfo(transactionLike, scriptLike);
15
+ * const signHashInfo = await getSignHashInfo(transactionLike, scriptLike, client);
15
16
  * if (signHashInfo) {
16
17
  * console.log(signHashInfo.message); // Outputs the signing message
17
18
  * console.log(signHashInfo.position); // Outputs the witness position
@@ -22,4 +23,18 @@ export declare function getSignHashInfo(txLike: TransactionLike, scriptLike: Scr
22
23
  message: Hex;
23
24
  position: number;
24
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>;
25
40
  //# sourceMappingURL=helpers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/signer/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,UAAU,EAAe,eAAe,EAAE,MAAM,QAAQ,CAAC;AAC1E,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnC,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAE7B;;;;;;;;;;;;;;;;GAgBG;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"}
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,CA6BtB"}
@@ -1,16 +1,18 @@
1
- import { Script, Transaction } from "../ckb";
1
+ import { Script, Transaction, WitnessArgs, } from "../ckb";
2
2
  import { Hasher } from "../hasher";
3
+ import { hexFrom } from "../hex";
3
4
  /**
4
5
  * Computes the signing hash information for a given transaction and script.
5
6
  *
6
7
  * @param txLike - The transaction to sign, represented as a TransactionLike object.
7
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.
8
10
  * @returns A promise that resolves to an object containing the signing message and the witness position,
9
11
  * or undefined if no matching input is found.
10
12
  *
11
13
  * @example
12
14
  * ```typescript
13
- * const signHashInfo = await getSignHashInfo(transactionLike, scriptLike);
15
+ * const signHashInfo = await getSignHashInfo(transactionLike, scriptLike, client);
14
16
  * if (signHashInfo) {
15
17
  * console.log(signHashInfo.message); // Outputs the signing message
16
18
  * console.log(signHashInfo.position); // Outputs the witness position
@@ -30,7 +32,7 @@ export async function getSignHashInfo(txLike, scriptLike, client) {
30
32
  throw Error("Unable to resolve inputs info");
31
33
  }
32
34
  if (!script.eq(input.cellOutput.lock)) {
33
- return;
35
+ continue;
34
36
  }
35
37
  if (position === -1) {
36
38
  position = i;
@@ -49,3 +51,40 @@ export async function getSignHashInfo(txLike, scriptLike, client) {
49
51
  position,
50
52
  };
51
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 witness = tx.witnesses[position]
85
+ ? WitnessArgs.fromBytes(tx.witnesses[position])
86
+ : WitnessArgs.from({});
87
+ witness.lock = hexFrom(Array.from(new Array(lockLen), () => 0));
88
+ tx.witnesses[position] = hexFrom(witness.toBytes());
89
+ return tx;
90
+ }
@@ -70,6 +70,14 @@ export declare abstract class Signer {
70
70
  * @returns A promise that resolves to the signed Transaction object.
71
71
  */
72
72
  signTransaction(tx: TransactionLike): Promise<Transaction>;
73
+ /**
74
+ * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
75
+ *
76
+ * @param _ - The transaction to prepare, represented as a TransactionLike object.
77
+ * @returns A promise that resolves to the prepared Transaction object.
78
+ * @throws Will throw an error if not implemented.
79
+ */
80
+ prepareTransaction(_: TransactionLike): Promise<Transaction>;
73
81
  /**
74
82
  * Signs a transaction without sending it. This method is not implemented and should be overridden by subclasses.
75
83
  *
@@ -1 +1 @@
1
- {"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../../src/signer/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,WAAW,EAAE,eAAe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,eAAe,CAAC,EAAE,EAAE,eAAe,GAAG,OAAO,CAAC,WAAW,CAAC;IAKhE;;;;;;OAMG;IACH,kBAAkB,CAAC,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,WAAW,CAAC;IAI5D;;;;;;OAMG;IACH,mBAAmB,CAAC,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,WAAW,CAAC;CAG9D;AAED;;GAEG;AACH,oBAAY,UAAU;IACpB,GAAG,QAAQ;IACX,GAAG,QAAQ;CACZ;AAED;;GAEG;AACH,qBAAa,UAAU;IAEZ,IAAI,EAAE,UAAU;IAChB,MAAM,EAAE,MAAM;gBADd,IAAI,EAAE,UAAU,EAChB,MAAM,EAAE,MAAM;CAExB;AAED;;GAEG;AACH,MAAM,MAAM,MAAM,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,UAAU,EAAE,CAAC;CACvB,CAAC"}
@@ -58,7 +58,18 @@ export class Signer {
58
58
  * @returns A promise that resolves to the signed Transaction object.
59
59
  */
60
60
  async signTransaction(tx) {
61
- return this.signOnlyTransaction(tx);
61
+ const preparedTx = await this.prepareTransaction(tx);
62
+ return this.signOnlyTransaction(preparedTx);
63
+ }
64
+ /**
65
+ * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
66
+ *
67
+ * @param _ - The transaction to prepare, represented as a TransactionLike object.
68
+ * @returns A promise that resolves to the prepared Transaction object.
69
+ * @throws Will throw an error if not implemented.
70
+ */
71
+ prepareTransaction(_) {
72
+ throw Error("Signer.prepareTransaction not implemented");
62
73
  }
63
74
  /**
64
75
  * Signs a transaction without sending it. This method is not implemented and should be overridden by subclasses.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckb-ccc/core",
3
- "version": "0.0.4-alpha.5",
3
+ "version": "0.0.4-alpha.8",
4
4
  "description": "Common Chains Connector Core",
5
5
  "author": "Hanssen0 <hanssen0@hanssen0.com>",
6
6
  "license": "MIT",
@@ -47,5 +47,5 @@
47
47
  "buffer": "^6.0.3",
48
48
  "cross-fetch": "^4.0.0"
49
49
  },
50
- "gitHead": "20746c0e9160fbf6eb519d5081897ae55042275a"
50
+ "gitHead": "98556e415832a41aeedb467e8a72b8861fe6a32d"
51
51
  }
@@ -4,8 +4,8 @@ import { MAINNET_SCRIPTS } from "./clientPublicMainnet.advanced";
4
4
  import { ClientJsonRpc } from "./jsonRpc";
5
5
 
6
6
  export class ClientPublicMainnet extends ClientJsonRpc {
7
- constructor(timeout?: number) {
8
- super("https://mainnet.ckbapp.dev/", timeout);
7
+ constructor(url = "https://mainnet.ckbapp.dev/", timeout?: number) {
8
+ super(url, timeout);
9
9
  }
10
10
 
11
11
  async getAddressPrefix(): Promise<string> {
@@ -4,8 +4,8 @@ import { TESTNET_SCRIPTS } from "./clientPublicTestnet.advanced";
4
4
  import { ClientJsonRpc } from "./jsonRpc";
5
5
 
6
6
  export class ClientPublicTestnet extends ClientJsonRpc {
7
- constructor(timeout?: number) {
8
- super("https://testnet.ckbapp.dev/", timeout);
7
+ constructor(url = "https://testnet.ckbapp.dev/", timeout?: number) {
8
+ super(url, timeout);
9
9
  }
10
10
 
11
11
  async getAddressPrefix(): Promise<string> {
@@ -6,7 +6,7 @@ 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 } from "../helpers";
9
+ import { getSignHashInfo, prepareSighashAllWitness } from "../helpers";
10
10
  import { Signer } from "../signer";
11
11
 
12
12
  /**
@@ -56,6 +56,17 @@ export abstract class SignerBtc extends Signer {
56
56
  ];
57
57
  }
58
58
 
59
+ /**
60
+ * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
61
+ *
62
+ * @param txLike - The transaction to prepare, represented as a TransactionLike object.
63
+ * @returns A promise that resolves to the prepared Transaction object.
64
+ */
65
+ async prepareTransaction(txLike: TransactionLike): Promise<Transaction> {
66
+ const { script } = await this.getRecommendedAddressObj();
67
+ return prepareSighashAllWitness(txLike, script, 85, this.client);
68
+ }
69
+
59
70
  /**
60
71
  * Signs a transaction without modifying it.
61
72
  *
@@ -4,7 +4,7 @@ import { 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 } from "../helpers";
7
+ import { getSignHashInfo, prepareSighashAllWitness } from "../helpers";
8
8
  import { Signer } from "../signer";
9
9
 
10
10
  /**
@@ -44,6 +44,17 @@ export abstract class SignerEvm extends Signer {
44
44
  ];
45
45
  }
46
46
 
47
+ /**
48
+ * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
49
+ *
50
+ * @param txLike - The transaction to prepare, represented as a TransactionLike object.
51
+ * @returns A promise that resolves to the prepared Transaction object.
52
+ */
53
+ async prepareTransaction(txLike: TransactionLike): Promise<Transaction> {
54
+ const { script } = await this.getRecommendedAddressObj();
55
+ return prepareSighashAllWitness(txLike, script, 85, this.client);
56
+ }
57
+
47
58
  /**
48
59
  * Signs a transaction without modifying it.
49
60
  *
@@ -1,19 +1,26 @@
1
- import { Script, ScriptLike, Transaction, TransactionLike } from "../ckb";
1
+ import {
2
+ Script,
3
+ ScriptLike,
4
+ Transaction,
5
+ TransactionLike,
6
+ WitnessArgs,
7
+ } from "../ckb";
2
8
  import { Client } from "../client";
3
9
  import { Hasher } from "../hasher";
4
- import { Hex } from "../hex";
10
+ import { Hex, hexFrom } from "../hex";
5
11
 
6
12
  /**
7
13
  * Computes the signing hash information for a given transaction and script.
8
14
  *
9
15
  * @param txLike - The transaction to sign, represented as a TransactionLike object.
10
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.
11
18
  * @returns A promise that resolves to an object containing the signing message and the witness position,
12
19
  * or undefined if no matching input is found.
13
20
  *
14
21
  * @example
15
22
  * ```typescript
16
- * const signHashInfo = await getSignHashInfo(transactionLike, scriptLike);
23
+ * const signHashInfo = await getSignHashInfo(transactionLike, scriptLike, client);
17
24
  * if (signHashInfo) {
18
25
  * console.log(signHashInfo.message); // Outputs the signing message
19
26
  * console.log(signHashInfo.position); // Outputs the witness position
@@ -40,7 +47,7 @@ export async function getSignHashInfo(
40
47
  }
41
48
 
42
49
  if (!script.eq(input.cellOutput.lock)) {
43
- return;
50
+ continue;
44
51
  }
45
52
 
46
53
  if (position === -1) {
@@ -64,3 +71,52 @@ export async function getSignHashInfo(
64
71
  position,
65
72
  };
66
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 witness = tx.witnesses[position]
116
+ ? WitnessArgs.fromBytes(tx.witnesses[position])
117
+ : WitnessArgs.from({});
118
+ witness.lock = hexFrom(Array.from(new Array(lockLen), () => 0));
119
+ tx.witnesses[position] = hexFrom(witness.toBytes());
120
+
121
+ return tx;
122
+ }
@@ -91,7 +91,19 @@ export abstract class Signer {
91
91
  * @returns A promise that resolves to the signed Transaction object.
92
92
  */
93
93
  async signTransaction(tx: TransactionLike): Promise<Transaction> {
94
- return this.signOnlyTransaction(tx);
94
+ const preparedTx = await this.prepareTransaction(tx);
95
+ return this.signOnlyTransaction(preparedTx);
96
+ }
97
+
98
+ /**
99
+ * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
100
+ *
101
+ * @param _ - The transaction to prepare, represented as a TransactionLike object.
102
+ * @returns A promise that resolves to the prepared Transaction object.
103
+ * @throws Will throw an error if not implemented.
104
+ */
105
+ prepareTransaction(_: TransactionLike): Promise<Transaction> {
106
+ throw Error("Signer.prepareTransaction not implemented");
95
107
  }
96
108
 
97
109
  /**