@ckb-ccc/core 0.0.4-alpha.11 → 0.0.4-alpha.13

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.
@@ -39,7 +39,6 @@ export class SignerBtcPublicKeyReadonly extends SignerBtc {
39
39
  async isConnected() {
40
40
  return true;
41
41
  }
42
- ;
43
42
  /**
44
43
  * Gets the Bitcoin account associated with the signer.
45
44
  *
@@ -38,7 +38,6 @@ export class SignerCkbScriptReadonly extends Signer {
38
38
  async isConnected() {
39
39
  return true;
40
40
  }
41
- ;
42
41
  /**
43
42
  * Gets the internal address for the script.
44
43
  *
@@ -1,5 +1,6 @@
1
1
  import { Address } from "../../address";
2
- import { Transaction, TransactionLike } from "../../ckb";
2
+ import { BytesLike } from "../../bytes";
3
+ import { Script, Transaction, TransactionLike } from "../../ckb";
3
4
  import { Signer } from "../signer";
4
5
  /**
5
6
  * An abstract class extending Signer for Ethereum Virtual Machine (EVM) based signing operations.
@@ -24,6 +25,8 @@ export declare abstract class SignerEvm extends Signer {
24
25
  * @returns A promise that resolves to an array of Address objects.
25
26
  */
26
27
  getAddressObjs(): Promise<Address[]>;
28
+ _getOmniLockEvmAddressObj(account: string): Promise<Address>;
29
+ _getOmniLockOldEvmAddressObj(account: string): Promise<Address>;
27
30
  /**
28
31
  * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
29
32
  *
@@ -38,5 +41,6 @@ export declare abstract class SignerEvm extends Signer {
38
41
  * @returns A promise that resolves to a signed Transaction object.
39
42
  */
40
43
  signOnlyTransaction(txLike: TransactionLike): Promise<Transaction>;
44
+ _signOmniLockScriptForTransaction(tx: Transaction, script: Script, messageTransformer: (hash: string) => BytesLike): Promise<Transaction>;
41
45
  }
42
46
  //# sourceMappingURL=signerEvm.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"signerEvm.d.ts","sourceRoot":"","sources":["../../../src/signer/evm/signerEvm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE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O,CAAC,OAAO,CAAC;IAQrE;;;;;OAKG;IACG,kBAAkB,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,WAAW,CAAC;IAYvE;;;;;OAKG;IACG,mBAAmB,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,WAAW,CAAC;IAsBlE,iCAAiC,CACrC,EAAE,EAAE,WAAW,EACf,MAAM,EAAE,MAAM,EACd,kBAAkB,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,SAAS,GAC9C,OAAO,CAAC,WAAW,CAAC;CA6BxB"}
@@ -27,9 +27,16 @@ export class SignerEvm extends Signer {
27
27
  async getAddressObjs() {
28
28
  const account = await this.getEvmAccount();
29
29
  return [
30
- await Address.fromKnownScript(KnownScript.OmniLock, hexFrom([0x12, ...bytesFrom(account), 0x00]), this.client),
30
+ await this._getOmniLockEvmAddressObj(account),
31
+ await this._getOmniLockOldEvmAddressObj(account),
31
32
  ];
32
33
  }
34
+ async _getOmniLockEvmAddressObj(account) {
35
+ return Address.fromKnownScript(KnownScript.OmniLock, hexFrom([0x12, ...bytesFrom(account), 0x00]), this.client);
36
+ }
37
+ async _getOmniLockOldEvmAddressObj(account) {
38
+ return Address.fromKnownScript(KnownScript.OmniLock, hexFrom([0x1, ...bytesFrom(account), 0x00]), this.client);
39
+ }
33
40
  /**
34
41
  * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
35
42
  *
@@ -37,8 +44,8 @@ export class SignerEvm extends Signer {
37
44
  * @returns A promise that resolves to the prepared Transaction object.
38
45
  */
39
46
  async prepareTransaction(txLike) {
40
- const { script } = await this.getRecommendedAddressObj();
41
- return prepareSighashAllWitness(txLike, script, 85, this.client);
47
+ const addresses = await this.getAddressObjs();
48
+ return addresses.reduce((txPromise, { script }) => txPromise.then((tx) => prepareSighashAllWitness(tx, script, 85, this.client)), Promise.resolve(Transaction.from(txLike)));
42
49
  }
43
50
  /**
44
51
  * Signs a transaction without modifying it.
@@ -47,13 +54,20 @@ export class SignerEvm extends Signer {
47
54
  * @returns A promise that resolves to a signed Transaction object.
48
55
  */
49
56
  async signOnlyTransaction(txLike) {
50
- const tx = Transaction.from(txLike);
51
- const { script } = await this.getRecommendedAddressObj();
57
+ let tx = Transaction.from(txLike);
58
+ const account = await this.getEvmAccount();
59
+ const { script: evmScript } = await this._getOmniLockEvmAddressObj(account);
60
+ const { script: oldEvmScript } = await this._getOmniLockOldEvmAddressObj(account);
61
+ tx = await this._signOmniLockScriptForTransaction(tx, evmScript, (hash) => `CKB transaction: ${hash}`);
62
+ tx = await this._signOmniLockScriptForTransaction(tx, oldEvmScript, (hash) => bytesFrom(hash));
63
+ return tx;
64
+ }
65
+ async _signOmniLockScriptForTransaction(tx, script, messageTransformer) {
52
66
  const info = await getSignHashInfo(tx, script, this.client);
53
67
  if (!info) {
54
68
  return tx;
55
69
  }
56
- const signature = bytesFrom(await this.signMessage(`CKB transaction: ${info.message}`));
70
+ const signature = bytesFrom(await this.signMessage(messageTransformer(info.message)));
57
71
  if (signature[signature.length - 1] >= 27) {
58
72
  signature[signature.length - 1] -= 27;
59
73
  }
@@ -42,7 +42,6 @@ export class SignerEvmAddressReadonly extends SignerEvm {
42
42
  async isConnected() {
43
43
  return true;
44
44
  }
45
- ;
46
45
  /**
47
46
  * Gets the EVM account associated with the signer.
48
47
  *
@@ -1 +1 @@
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
+ {"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"}
@@ -81,8 +81,9 @@ export async function prepareSighashAllWitness(txLike, scriptLike, lockLen, clie
81
81
  if (position === -1) {
82
82
  return tx;
83
83
  }
84
- const witness = tx.witnesses[position]
85
- ? WitnessArgs.fromBytes(tx.witnesses[position])
84
+ const rawWitness = tx.witnesses[position];
85
+ const witness = (rawWitness ?? "0x") !== "0x"
86
+ ? WitnessArgs.fromBytes(rawWitness)
86
87
  : WitnessArgs.from({});
87
88
  witness.lock = hexFrom(Array.from(new Array(lockLen), () => 0));
88
89
  tx.witnesses[position] = hexFrom(witness.toBytes());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckb-ccc/core",
3
- "version": "0.0.4-alpha.11",
3
+ "version": "0.0.4-alpha.13",
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": "db0297c0753349e2650d2845f42f546316078d5d"
50
+ "gitHead": "92a34e4c924a2b41702bcc1247edd5a01fe6925b"
51
51
  }
@@ -49,7 +49,7 @@ export class SignerBtcPublicKeyReadonly extends SignerBtc {
49
49
  */
50
50
  async isConnected(): Promise<boolean> {
51
51
  return true;
52
- };
52
+ }
53
53
 
54
54
  /**
55
55
  * Gets the Bitcoin account associated with the signer.
@@ -45,7 +45,7 @@ export class SignerCkbScriptReadonly extends Signer {
45
45
  */
46
46
  async isConnected(): Promise<boolean> {
47
47
  return true;
48
- };
48
+ }
49
49
 
50
50
  /**
51
51
  * Gets the internal address for the script.
@@ -1,6 +1,6 @@
1
1
  import { Address } from "../../address";
2
- import { bytesConcat, bytesFrom } from "../../bytes";
3
- import { Transaction, TransactionLike, WitnessArgs } from "../../ckb";
2
+ import { BytesLike, bytesConcat, bytesFrom } from "../../bytes";
3
+ 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";
@@ -36,14 +36,27 @@ export abstract class SignerEvm extends Signer {
36
36
  async getAddressObjs(): Promise<Address[]> {
37
37
  const account = await this.getEvmAccount();
38
38
  return [
39
- await Address.fromKnownScript(
40
- KnownScript.OmniLock,
41
- hexFrom([0x12, ...bytesFrom(account), 0x00]),
42
- this.client,
43
- ),
39
+ await this._getOmniLockEvmAddressObj(account),
40
+ await this._getOmniLockOldEvmAddressObj(account),
44
41
  ];
45
42
  }
46
43
 
44
+ async _getOmniLockEvmAddressObj(account: string): Promise<Address> {
45
+ return Address.fromKnownScript(
46
+ KnownScript.OmniLock,
47
+ hexFrom([0x12, ...bytesFrom(account), 0x00]),
48
+ this.client,
49
+ );
50
+ }
51
+
52
+ async _getOmniLockOldEvmAddressObj(account: string): Promise<Address> {
53
+ return Address.fromKnownScript(
54
+ KnownScript.OmniLock,
55
+ hexFrom([0x1, ...bytesFrom(account), 0x00]),
56
+ this.client,
57
+ );
58
+ }
59
+
47
60
  /**
48
61
  * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
49
62
  *
@@ -51,8 +64,15 @@ export abstract class SignerEvm extends Signer {
51
64
  * @returns A promise that resolves to the prepared Transaction object.
52
65
  */
53
66
  async prepareTransaction(txLike: TransactionLike): Promise<Transaction> {
54
- const { script } = await this.getRecommendedAddressObj();
55
- return prepareSighashAllWitness(txLike, script, 85, this.client);
67
+ const addresses = await this.getAddressObjs();
68
+
69
+ return addresses.reduce(
70
+ (txPromise, { script }) =>
71
+ txPromise.then((tx) =>
72
+ prepareSighashAllWitness(tx, script, 85, this.client),
73
+ ),
74
+ Promise.resolve(Transaction.from(txLike)),
75
+ );
56
76
  }
57
77
 
58
78
  /**
@@ -62,16 +82,39 @@ export abstract class SignerEvm extends Signer {
62
82
  * @returns A promise that resolves to a signed Transaction object.
63
83
  */
64
84
  async signOnlyTransaction(txLike: TransactionLike): Promise<Transaction> {
65
- const tx = Transaction.from(txLike);
85
+ let tx = Transaction.from(txLike);
86
+
87
+ const account = await this.getEvmAccount();
88
+ const { script: evmScript } = await this._getOmniLockEvmAddressObj(account);
89
+ const { script: oldEvmScript } =
90
+ await this._getOmniLockOldEvmAddressObj(account);
91
+
92
+ tx = await this._signOmniLockScriptForTransaction(
93
+ tx,
94
+ evmScript,
95
+ (hash) => `CKB transaction: ${hash}`,
96
+ );
97
+ tx = await this._signOmniLockScriptForTransaction(
98
+ tx,
99
+ oldEvmScript,
100
+ (hash) => bytesFrom(hash),
101
+ );
102
+
103
+ return tx;
104
+ }
66
105
 
67
- const { script } = await this.getRecommendedAddressObj();
106
+ async _signOmniLockScriptForTransaction(
107
+ tx: Transaction,
108
+ script: Script,
109
+ messageTransformer: (hash: string) => BytesLike,
110
+ ): Promise<Transaction> {
68
111
  const info = await getSignHashInfo(tx, script, this.client);
69
112
  if (!info) {
70
113
  return tx;
71
114
  }
72
115
 
73
116
  const signature = bytesFrom(
74
- await this.signMessage(`CKB transaction: ${info.message}`),
117
+ await this.signMessage(messageTransformer(info.message)),
75
118
  );
76
119
  if (signature[signature.length - 1] >= 27) {
77
120
  signature[signature.length - 1] -= 27;
@@ -50,7 +50,7 @@ export class SignerEvmAddressReadonly extends SignerEvm {
50
50
  */
51
51
  async isConnected(): Promise<boolean> {
52
52
  return true;
53
- };
53
+ }
54
54
 
55
55
  /**
56
56
  * Gets the EVM account associated with the signer.
@@ -112,9 +112,11 @@ export async function prepareSighashAllWitness(
112
112
  return tx;
113
113
  }
114
114
 
115
- const witness = tx.witnesses[position]
116
- ? WitnessArgs.fromBytes(tx.witnesses[position])
117
- : WitnessArgs.from({});
115
+ const rawWitness = tx.witnesses[position];
116
+ const witness =
117
+ (rawWitness ?? "0x") !== "0x"
118
+ ? WitnessArgs.fromBytes(rawWitness)
119
+ : WitnessArgs.from({});
118
120
  witness.lock = hexFrom(Array.from(new Array(lockLen), () => 0));
119
121
  tx.witnesses[position] = hexFrom(witness.toBytes());
120
122