@ckb-ccc/core 0.0.3 → 0.0.4-alpha.1

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 (52) hide show
  1. package/README.md +32 -0
  2. package/dist/signer/btc/index.d.ts +3 -0
  3. package/dist/signer/btc/index.d.ts.map +1 -0
  4. package/dist/signer/btc/index.js +2 -0
  5. package/dist/signer/btc/signerBtc.d.ts +12 -0
  6. package/dist/signer/btc/signerBtc.d.ts.map +1 -0
  7. package/dist/signer/btc/signerBtc.js +35 -0
  8. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts +12 -0
  9. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -0
  10. package/dist/signer/btc/signerBtcPublicKeyReadonly.js +16 -0
  11. package/dist/signer/ckb/index.d.ts +2 -0
  12. package/dist/signer/ckb/index.d.ts.map +1 -0
  13. package/dist/signer/ckb/index.js +1 -0
  14. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +12 -0
  15. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -0
  16. package/dist/signer/ckb/signerCkbScriptReadonly.js +16 -0
  17. package/dist/signer/evm/index.d.ts +3 -0
  18. package/dist/signer/evm/index.d.ts.map +1 -0
  19. package/dist/signer/evm/index.js +2 -0
  20. package/dist/signer/evm/signerEvm.d.ts +10 -0
  21. package/dist/signer/evm/signerEvm.d.ts.map +1 -0
  22. package/dist/signer/evm/signerEvm.js +34 -0
  23. package/dist/signer/evm/signerEvmAddressReadonly.d.ts +10 -0
  24. package/dist/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -0
  25. package/dist/signer/evm/signerEvmAddressReadonly.js +12 -0
  26. package/dist/signer/index.d.ts +3 -2
  27. package/dist/signer/index.d.ts.map +1 -1
  28. package/dist/signer/index.js +3 -2
  29. package/dist/signer/signer.d.ts +16 -2
  30. package/dist/signer/signer.d.ts.map +1 -1
  31. package/dist/signer/signer.js +17 -0
  32. package/package.json +5 -4
  33. package/src/address/address.advanced.ts +11 -11
  34. package/src/address/index.ts +1 -3
  35. package/src/signer/btc/index.ts +2 -0
  36. package/src/signer/btc/signerBtc.ts +65 -0
  37. package/src/signer/btc/signerBtcPublicKeyReadonly.ts +27 -0
  38. package/src/signer/ckb/index.ts +1 -0
  39. package/src/signer/ckb/signerCkbScriptReadonly.ts +24 -0
  40. package/src/signer/evm/index.ts +2 -0
  41. package/src/signer/evm/signerEvm.ts +58 -0
  42. package/src/signer/evm/signerEvmAddressReadonly.ts +19 -0
  43. package/src/signer/index.ts +3 -2
  44. package/src/signer/signer.ts +24 -2
  45. package/dist/signer/signerReadonly.d.ts +0 -7
  46. package/dist/signer/signerReadonly.d.ts.map +0 -1
  47. package/dist/signer/signerReadonly.js +0 -9
  48. package/dist/signer/signerReadonlyCkbScript.d.ts +0 -12
  49. package/dist/signer/signerReadonlyCkbScript.d.ts.map +0 -1
  50. package/dist/signer/signerReadonlyCkbScript.js +0 -17
  51. package/src/signer/signerReadonly.ts +0 -12
  52. package/src/signer/signerReadonlyCkbScript.ts +0 -25
package/README.md ADDED
@@ -0,0 +1,32 @@
1
+ <h1 align="center" style="font-size: 64px;">
2
+ CCC
3
+ </h1>
4
+
5
+ <p align="center">
6
+ <img alt="GitHub commit activity" src="https://img.shields.io/github/commit-activity/m/ckb-ecofund/ccc">
7
+ <img alt="GitHub last commit" src="https://img.shields.io/github/last-commit/ckb-ecofund/ccc/master">
8
+ <img alt="GitHub deployments" src="https://img.shields.io/github/deployments/ckb-ecofund/ccc/production">
9
+ <img alt="Demo" src="https://img.shields.io/website?url=https%3A%2F%2Fckbccc-demo.vercel.app%2F&label=Demo">
10
+ </p>
11
+
12
+ <p align="center">
13
+ "Common Chains Connector" is where CCC begins.
14
+ <br />
15
+ CCC helps you to interoperate wallets from different chain ecosystems with CKB,
16
+ <br />
17
+ fully enabling CKB's cryptographic freedom power.
18
+ </p>
19
+
20
+ ## Preview
21
+
22
+ <p align="center">
23
+ <a href="https://ckbccc-demo.vercel.app/">
24
+ <img src="https://raw.githubusercontent.com/ckb-ecofund/ccc/master/assets/preview.png" width="30%" />
25
+ </a>
26
+ </p>
27
+
28
+ This project is still under active development, and we are looking forward to your feedback. [Try its demo now here](https://ckbccc-demo.vercel.app/).
29
+
30
+ <h3 align="center">
31
+ Read more about CCC on its <a href="https://github.com/ckb-ecofund/ccc">GitHub Repo</a>.
32
+ </h3>
@@ -0,0 +1,3 @@
1
+ export * from "./signerBtc";
2
+ export * from "./signerBtcPublicKeyReadonly";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/signer/btc/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,8BAA8B,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from "./signerBtc";
2
+ export * from "./signerBtcPublicKeyReadonly";
@@ -0,0 +1,12 @@
1
+ import { Address } from "../../address";
2
+ import { Transaction } from "../../ckb";
3
+ import { HexLike } from "../../hex";
4
+ import { Signer } from "../signer";
5
+ export declare abstract class SignerBtc extends Signer {
6
+ abstract getBtcAccount(): Promise<string>;
7
+ abstract getBtcPublicKey(): Promise<HexLike>;
8
+ getInternalAddress(): Promise<string>;
9
+ getAddressObjs(): Promise<Address[]>;
10
+ signOnlyTransaction(tx: Transaction): Promise<Transaction>;
11
+ }
12
+ //# sourceMappingURL=signerBtc.d.ts.map
@@ -0,0 +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,MAAM,WAAW,CAAC;AAErD,OAAO,EAAE,OAAO,EAAW,MAAM,WAAW,CAAC;AAG7C,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnC,8BAAsB,SAAU,SAAQ,MAAM;IAC5C,QAAQ,CAAC,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;IAEzC,QAAQ,CAAC,eAAe,IAAI,OAAO,CAAC,OAAO,CAAC;IAEtC,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAIrC,cAAc,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;IAapC,mBAAmB,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;CA+BjE"}
@@ -0,0 +1,35 @@
1
+ import { ripemd160 } from "@noble/hashes/ripemd160";
2
+ import { sha256 } from "@noble/hashes/sha256";
3
+ import { Address } from "../../address";
4
+ import { bytesConcat, bytesFrom } from "../../bytes";
5
+ import { WitnessArgs } from "../../ckb";
6
+ import { KnownScript } from "../../client";
7
+ import { hexFrom } from "../../hex";
8
+ import { numToBytes } from "../../num";
9
+ import { getSignHashInfo } from "../helpers";
10
+ import { Signer } from "../signer";
11
+ export class SignerBtc extends Signer {
12
+ async getInternalAddress() {
13
+ return this.getBtcAccount();
14
+ }
15
+ async getAddressObjs() {
16
+ const publicKey = await this.getBtcPublicKey();
17
+ const hash = ripemd160(sha256(bytesFrom(publicKey)));
18
+ return [
19
+ await Address.fromKnownScript(KnownScript.OmniLock, hexFrom([0x04, ...hash, 0x00]), this.client),
20
+ ];
21
+ }
22
+ async signOnlyTransaction(tx) {
23
+ const { script } = await this.getRecommendedAddressObj();
24
+ const info = await getSignHashInfo(tx, script);
25
+ if (!info) {
26
+ return tx;
27
+ }
28
+ const signature = bytesFrom(await this.signMessage(`CKB (Bitcoin Layer) transaction: ${info.message}`), "base64");
29
+ signature[0] = 31 + ((signature[0] - 27) % 4);
30
+ const witness = WitnessArgs.fromBytes(tx.witnesses[info.position]);
31
+ witness.lock = hexFrom(bytesConcat(numToBytes(5 * 4 + signature.length, 4), numToBytes(4 * 4, 4), numToBytes(5 * 4 + signature.length, 4), numToBytes(5 * 4 + signature.length, 4), numToBytes(signature.length, 4), signature));
32
+ tx.witnesses[info.position] = hexFrom(witness.toBytes());
33
+ return tx;
34
+ }
35
+ }
@@ -0,0 +1,12 @@
1
+ import { Client } from "../../client";
2
+ import { Hex, HexLike } from "../../hex";
3
+ import { SignerBtc } from "./signerBtc";
4
+ export declare class SignerBtcPublicKeyReadonly extends SignerBtc {
5
+ private readonly account;
6
+ private readonly publicKey;
7
+ constructor(client: Client, account: string, publicKey: HexLike);
8
+ connect(): Promise<void>;
9
+ getBtcAccount(): Promise<string>;
10
+ getBtcPublicKey(): Promise<Hex>;
11
+ }
12
+ //# sourceMappingURL=signerBtcPublicKeyReadonly.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signerBtcPublicKeyReadonly.d.ts","sourceRoot":"","sources":["../../../src/signer/btc/signerBtcPublicKeyReadonly.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,GAAG,EAAE,OAAO,EAAW,MAAM,WAAW,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,qBAAa,0BAA2B,SAAQ,SAAS;IAKrD,OAAO,CAAC,QAAQ,CAAC,OAAO;IAJ1B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAM;gBAG9B,MAAM,EAAE,MAAM,EACG,OAAO,EAAE,MAAM,EAChC,SAAS,EAAE,OAAO;IAOd,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAExB,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;IAIhC,eAAe,IAAI,OAAO,CAAC,GAAG,CAAC;CAGtC"}
@@ -0,0 +1,16 @@
1
+ import { hexFrom } from "../../hex";
2
+ import { SignerBtc } from "./signerBtc";
3
+ export class SignerBtcPublicKeyReadonly extends SignerBtc {
4
+ constructor(client, account, publicKey) {
5
+ super(client);
6
+ this.account = account;
7
+ this.publicKey = hexFrom(publicKey);
8
+ }
9
+ async connect() { }
10
+ async getBtcAccount() {
11
+ return this.account;
12
+ }
13
+ async getBtcPublicKey() {
14
+ return this.publicKey;
15
+ }
16
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./signerCkbScriptReadonly";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/signer/ckb/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC"}
@@ -0,0 +1 @@
1
+ export * from "./signerCkbScriptReadonly";
@@ -0,0 +1,12 @@
1
+ import { Address } from "../../address";
2
+ import { ScriptLike } from "../../ckb";
3
+ import { Client } from "../../client";
4
+ import { Signer } from "../signer";
5
+ export declare class SignerCkbScriptReadonly extends Signer {
6
+ private readonly script;
7
+ constructor(client: Client, script: ScriptLike);
8
+ connect(): Promise<void>;
9
+ getInternalAddress(): Promise<string>;
10
+ getAddressObjs(): Promise<Address[]>;
11
+ }
12
+ //# sourceMappingURL=signerCkbScriptReadonly.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signerCkbScriptReadonly.d.ts","sourceRoot":"","sources":["../../../src/signer/ckb/signerCkbScriptReadonly.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAU,UAAU,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnC,qBAAa,uBAAwB,SAAQ,MAAM;IACjD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;gBAEpB,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU;IAMxC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAExB,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAIrC,cAAc,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;CAG3C"}
@@ -0,0 +1,16 @@
1
+ import { Address } from "../../address";
2
+ import { Script } from "../../ckb";
3
+ import { Signer } from "../signer";
4
+ export class SignerCkbScriptReadonly extends Signer {
5
+ constructor(client, script) {
6
+ super(client);
7
+ this.script = Script.from(script);
8
+ }
9
+ async connect() { }
10
+ async getInternalAddress() {
11
+ return this.getRecommendedAddress();
12
+ }
13
+ async getAddressObjs() {
14
+ return [await Address.fromScript(this.script, this.client)];
15
+ }
16
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./signerEvm";
2
+ export * from "./signerEvmAddressReadonly";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/signer/evm/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,4BAA4B,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from "./signerEvm";
2
+ export * from "./signerEvmAddressReadonly";
@@ -0,0 +1,10 @@
1
+ import { Address } from "../../address";
2
+ import { Transaction } from "../../ckb";
3
+ import { Signer } from "../signer";
4
+ export declare abstract class SignerEvm extends Signer {
5
+ abstract getEvmAccount(): Promise<string>;
6
+ getInternalAddress(): Promise<string>;
7
+ getAddressObjs(): Promise<Address[]>;
8
+ signOnlyTransaction(tx: Transaction): Promise<Transaction>;
9
+ }
10
+ //# sourceMappingURL=signerEvm.d.ts.map
@@ -0,0 +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,MAAM,WAAW,CAAC;AAKrD,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnC,8BAAsB,SAAU,SAAQ,MAAM;IAC5C,QAAQ,CAAC,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;IAEnC,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAIrC,cAAc,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;IAWpC,mBAAmB,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;CA8BjE"}
@@ -0,0 +1,34 @@
1
+ import { Address } from "../../address";
2
+ import { bytesConcat, bytesFrom } from "../../bytes";
3
+ import { WitnessArgs } from "../../ckb";
4
+ import { KnownScript } from "../../client";
5
+ import { hexFrom } from "../../hex";
6
+ import { numToBytes } from "../../num";
7
+ import { getSignHashInfo } from "../helpers";
8
+ import { Signer } from "../signer";
9
+ export class SignerEvm extends Signer {
10
+ async getInternalAddress() {
11
+ return this.getEvmAccount();
12
+ }
13
+ async getAddressObjs() {
14
+ const account = await this.getEvmAccount();
15
+ return [
16
+ await Address.fromKnownScript(KnownScript.OmniLock, hexFrom([0x12, ...bytesFrom(account), 0x00]), this.client),
17
+ ];
18
+ }
19
+ async signOnlyTransaction(tx) {
20
+ const { script } = await this.getRecommendedAddressObj();
21
+ const info = await getSignHashInfo(tx, script);
22
+ if (!info) {
23
+ return tx;
24
+ }
25
+ const signature = bytesFrom(await this.signMessage(`CKB transaction: ${info.message}`));
26
+ if (signature[signature.length - 1] >= 27) {
27
+ signature[signature.length - 1] -= 27;
28
+ }
29
+ const witness = WitnessArgs.fromBytes(tx.witnesses[info.position]);
30
+ witness.lock = hexFrom(bytesConcat(numToBytes(5 * 4 + signature.length, 4), numToBytes(4 * 4, 4), numToBytes(5 * 4 + signature.length, 4), numToBytes(5 * 4 + signature.length, 4), numToBytes(signature.length, 4), signature));
31
+ tx.witnesses[info.position] = hexFrom(witness.toBytes());
32
+ return tx;
33
+ }
34
+ }
@@ -0,0 +1,10 @@
1
+ import { Client } from "../../client";
2
+ import { HexLike } from "../../hex";
3
+ import { SignerEvm } from "./signerEvm";
4
+ export declare abstract class SignerEvmAddressReadonly extends SignerEvm {
5
+ private readonly address;
6
+ constructor(client: Client, address: HexLike);
7
+ connect(): Promise<void>;
8
+ getEvmAccount(): Promise<string>;
9
+ }
10
+ //# sourceMappingURL=signerEvmAddressReadonly.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signerEvmAddressReadonly.d.ts","sourceRoot":"","sources":["../../../src/signer/evm/signerEvmAddressReadonly.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAO,OAAO,EAAW,MAAM,WAAW,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,8BAAsB,wBAAyB,SAAQ,SAAS;IAC9D,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAM;gBAElB,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO;IAMtC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAExB,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;CAGvC"}
@@ -0,0 +1,12 @@
1
+ import { hexFrom } from "../../hex";
2
+ import { SignerEvm } from "./signerEvm";
3
+ export class SignerEvmAddressReadonly extends SignerEvm {
4
+ constructor(client, address) {
5
+ super(client);
6
+ this.address = hexFrom(address);
7
+ }
8
+ async connect() { }
9
+ async getEvmAccount() {
10
+ return this.address;
11
+ }
12
+ }
@@ -1,5 +1,6 @@
1
+ export * from "./btc";
2
+ export * from "./ckb";
3
+ export * from "./evm";
1
4
  export * from "./helpers";
2
5
  export * from "./signer";
3
- export * from "./signerReadonly";
4
- export * from "./signerReadonlyCkbScript";
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/signer/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,kBAAkB,CAAC;AACjC,cAAc,2BAA2B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/signer/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,OAAO,CAAC;AACtB,cAAc,OAAO,CAAC;AACtB,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC"}
@@ -1,4 +1,5 @@
1
+ export * from "./btc";
2
+ export * from "./ckb";
3
+ export * from "./evm";
1
4
  export * from "./helpers";
2
5
  export * from "./signer";
3
- export * from "./signerReadonly";
4
- export * from "./signerReadonlyCkbScript";
@@ -12,9 +12,23 @@ export declare abstract class Signer {
12
12
  getRecommendedAddressObj(_preference?: unknown): Promise<Address>;
13
13
  getRecommendedAddress(preference?: unknown): Promise<string>;
14
14
  getAddresses(): Promise<string[]>;
15
- abstract signMessage(message: string | BytesLike): Promise<string>;
15
+ signMessage(_: string | BytesLike): Promise<string>;
16
16
  sendTransaction(tx: Transaction): Promise<Hex>;
17
17
  signTransaction(tx: Transaction): Promise<Transaction>;
18
- abstract signOnlyTransaction(tx: Transaction): Promise<Transaction>;
18
+ signOnlyTransaction(_: Transaction): Promise<Transaction>;
19
19
  }
20
+ export declare enum SignerType {
21
+ EVM = "EVM",
22
+ BTC = "BTC"
23
+ }
24
+ export declare class SignerInfo {
25
+ type: SignerType;
26
+ signer: Signer;
27
+ constructor(type: SignerType, signer: Signer);
28
+ }
29
+ export type Wallet = {
30
+ name: string;
31
+ icon: string;
32
+ signers: SignerInfo[];
33
+ };
20
34
  //# sourceMappingURL=signer.d.ts.map
@@ -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,MAAM,QAAQ,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAE7B,8BAAsB,MAAM;aACE,MAAM,EAAE,MAAM;gBAAd,MAAM,EAAE,MAAM;IAE1C,QAAQ,CAAC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAEjC,QAAQ,CAAC,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAE9C,QAAQ,CAAC,cAAc,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;IACvC,wBAAwB,CAAC,WAAW,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAIjE,qBAAqB,CAAC,UAAU,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC;IAG5D,YAAY,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAMvC,QAAQ,CAAC,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;IAE5D,eAAe,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC;IAI9C,eAAe,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IAI5D,QAAQ,CAAC,mBAAmB,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;CACpE"}
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,MAAM,QAAQ,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAE7B,8BAAsB,MAAM;aACE,MAAM,EAAE,MAAM;gBAAd,MAAM,EAAE,MAAM;IAE1C,QAAQ,CAAC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAEjC,QAAQ,CAAC,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAE9C,QAAQ,CAAC,cAAc,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;IACvC,wBAAwB,CAAC,WAAW,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAIjE,qBAAqB,CAAC,UAAU,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC;IAG5D,YAAY,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAMvC,WAAW,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;IAI7C,eAAe,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC;IAI9C,eAAe,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IAI5D,mBAAmB,CAAC,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;CAG1D;AAED,oBAAY,UAAU;IACpB,GAAG,QAAQ;IACX,GAAG,QAAQ;CACZ;AAED,qBAAa,UAAU;IAEZ,IAAI,EAAE,UAAU;IAChB,MAAM,EAAE,MAAM;gBADd,IAAI,EAAE,UAAU,EAChB,MAAM,EAAE,MAAM;CAExB;AAED,MAAM,MAAM,MAAM,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,UAAU,EAAE,CAAC;CACvB,CAAC"}
@@ -11,10 +11,27 @@ export class Signer {
11
11
  async getAddresses() {
12
12
  return this.getAddressObjs().then((addresses) => addresses.map((address) => address.toString()));
13
13
  }
14
+ signMessage(_) {
15
+ throw Error("Signer.signMessage not implemented");
16
+ }
14
17
  async sendTransaction(tx) {
15
18
  return this.client.sendTransaction(await this.signTransaction(tx));
16
19
  }
17
20
  async signTransaction(tx) {
18
21
  return this.signOnlyTransaction(tx);
19
22
  }
23
+ signOnlyTransaction(_) {
24
+ throw Error("Signer.signOnlyTransaction not implemented");
25
+ }
26
+ }
27
+ export var SignerType;
28
+ (function (SignerType) {
29
+ SignerType["EVM"] = "EVM";
30
+ SignerType["BTC"] = "BTC";
31
+ })(SignerType || (SignerType = {}));
32
+ export class SignerInfo {
33
+ constructor(type, signer) {
34
+ this.type = type;
35
+ this.signer = signer;
36
+ }
20
37
  }
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@ckb-ccc/core",
3
- "version": "0.0.3",
3
+ "version": "0.0.4-alpha.1",
4
4
  "description": "Common Chains Connector Core",
5
5
  "author": "Hanssen0 <hanssen0@hanssen0.com>",
6
6
  "license": "MIT",
7
7
  "private": false,
8
- "homepage": "https://github.com/Hanssen0/ccc",
8
+ "homepage": "https://github.com/ckb-ecofund/ccc",
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "git://github.com/Hanssen0/ccc.git"
11
+ "url": "git://github.com/ckb-ecofund/ccc.git"
12
12
  },
13
13
  "main": "dist/index.js",
14
14
  "exports": {
@@ -40,11 +40,12 @@
40
40
  "access": "public"
41
41
  },
42
42
  "dependencies": {
43
+ "@noble/hashes": "^1.4.0",
43
44
  "abort-controller": "^3.0.0",
44
45
  "bech32": "^2.0.0",
45
46
  "blake2b": "^2.1.4",
46
47
  "buffer": "^6.0.3",
47
48
  "cross-fetch": "^4.0.0"
48
49
  },
49
- "gitHead": "9df1bbe0007136568914d8a73fe39f898d636a12"
50
+ "gitHead": "7dab1a1d6e3e380b26f43d0d0308bd4da1617029"
50
51
  }
@@ -1,8 +1,8 @@
1
1
  import { bech32, bech32m } from "bech32";
2
- import { hashTypeFromBytes } from "../ckb";
2
+ import { Script, hashTypeFromBytes } from "../ckb";
3
3
  import { Client, KnownScript } from "../client";
4
4
  import { hexFrom } from "../hex";
5
- import { type AddressLike } from "./index";
5
+ import { type Address } from "./index";
6
6
 
7
7
  export function addressPayloadFromString(address: string): {
8
8
  prefix: string;
@@ -42,7 +42,7 @@ export async function addressFromPayload(
42
42
  format: AddressFormat,
43
43
  payload: number[],
44
44
  client: Client,
45
- ): Promise<AddressLike> {
45
+ ): Promise<Address> {
46
46
  if (format === AddressFormat.Full) {
47
47
  if (payload.length < 32 + 1) {
48
48
  throw new Error(
@@ -51,11 +51,11 @@ export async function addressFromPayload(
51
51
  }
52
52
 
53
53
  return {
54
- script: {
54
+ script: Script.from({
55
55
  codeHash: hexFrom(payload.slice(0, 32)),
56
56
  hashType: hashTypeFromBytes(payload.slice(32, 33)),
57
57
  args: hexFrom(payload.slice(33)),
58
- },
58
+ }),
59
59
  prefix,
60
60
  };
61
61
  }
@@ -68,11 +68,11 @@ export async function addressFromPayload(
68
68
  }
69
69
 
70
70
  return {
71
- script: {
71
+ script: Script.from({
72
72
  codeHash: hexFrom(payload.slice(0, 32)),
73
73
  hashType: "data",
74
74
  args: hexFrom(payload.slice(32)),
75
- },
75
+ }),
76
76
  prefix,
77
77
  };
78
78
  }
@@ -85,11 +85,11 @@ export async function addressFromPayload(
85
85
  }
86
86
 
87
87
  return {
88
- script: {
88
+ script: Script.from({
89
89
  codeHash: hexFrom(payload.slice(0, 32)),
90
90
  hashType: "type",
91
91
  args: hexFrom(payload.slice(32)),
92
- },
92
+ }),
93
93
  prefix,
94
94
  };
95
95
  }
@@ -112,10 +112,10 @@ export async function addressFromPayload(
112
112
  }
113
113
 
114
114
  return {
115
- script: {
115
+ script: Script.from({
116
116
  ...(await client.getKnownScript(script)),
117
117
  args: hexFrom(payload.slice(1)),
118
- },
118
+ }),
119
119
  prefix,
120
120
  };
121
121
  }
@@ -45,9 +45,7 @@ export class Address {
45
45
  );
46
46
  }
47
47
 
48
- return Address.from(
49
- await addressFromPayload(prefix, format, payload, client),
50
- );
48
+ return addressFromPayload(prefix, format, payload, client);
51
49
  }
52
50
 
53
51
  static async fromScript(
@@ -0,0 +1,2 @@
1
+ export * from "./signerBtc";
2
+ export * from "./signerBtcPublicKeyReadonly";
@@ -0,0 +1,65 @@
1
+ import { ripemd160 } from "@noble/hashes/ripemd160";
2
+ import { sha256 } from "@noble/hashes/sha256";
3
+ import { Address } from "../../address";
4
+ import { bytesConcat, bytesFrom } from "../../bytes";
5
+ import { Transaction, WitnessArgs } from "../../ckb";
6
+ import { KnownScript } from "../../client";
7
+ import { HexLike, hexFrom } from "../../hex";
8
+ import { numToBytes } from "../../num";
9
+ import { getSignHashInfo } from "../helpers";
10
+ import { Signer } from "../signer";
11
+
12
+ export abstract class SignerBtc extends Signer {
13
+ abstract getBtcAccount(): Promise<string>;
14
+
15
+ abstract getBtcPublicKey(): Promise<HexLike>;
16
+
17
+ async getInternalAddress(): Promise<string> {
18
+ return this.getBtcAccount();
19
+ }
20
+
21
+ async getAddressObjs(): Promise<Address[]> {
22
+ const publicKey = await this.getBtcPublicKey();
23
+ const hash = ripemd160(sha256(bytesFrom(publicKey)));
24
+
25
+ return [
26
+ await Address.fromKnownScript(
27
+ KnownScript.OmniLock,
28
+ hexFrom([0x04, ...hash, 0x00]),
29
+ this.client,
30
+ ),
31
+ ];
32
+ }
33
+
34
+ async signOnlyTransaction(tx: Transaction): Promise<Transaction> {
35
+ const { script } = await this.getRecommendedAddressObj();
36
+ const info = await getSignHashInfo(tx, script);
37
+ if (!info) {
38
+ return tx;
39
+ }
40
+
41
+ const signature = bytesFrom(
42
+ await this.signMessage(
43
+ `CKB (Bitcoin Layer) transaction: ${info.message}`,
44
+ ),
45
+ "base64",
46
+ );
47
+ signature[0] = 31 + ((signature[0] - 27) % 4);
48
+
49
+ const witness = WitnessArgs.fromBytes(tx.witnesses[info.position]);
50
+ witness.lock = hexFrom(
51
+ bytesConcat(
52
+ numToBytes(5 * 4 + signature.length, 4),
53
+ numToBytes(4 * 4, 4),
54
+ numToBytes(5 * 4 + signature.length, 4),
55
+ numToBytes(5 * 4 + signature.length, 4),
56
+ numToBytes(signature.length, 4),
57
+ signature,
58
+ ),
59
+ );
60
+
61
+ tx.witnesses[info.position] = hexFrom(witness.toBytes());
62
+
63
+ return tx;
64
+ }
65
+ }
@@ -0,0 +1,27 @@
1
+ import { Client } from "../../client";
2
+ import { Hex, HexLike, hexFrom } from "../../hex";
3
+ import { SignerBtc } from "./signerBtc";
4
+
5
+ export class SignerBtcPublicKeyReadonly extends SignerBtc {
6
+ private readonly publicKey: Hex;
7
+
8
+ constructor(
9
+ client: Client,
10
+ private readonly account: string,
11
+ publicKey: HexLike,
12
+ ) {
13
+ super(client);
14
+
15
+ this.publicKey = hexFrom(publicKey);
16
+ }
17
+
18
+ async connect(): Promise<void> {}
19
+
20
+ async getBtcAccount(): Promise<string> {
21
+ return this.account;
22
+ }
23
+
24
+ async getBtcPublicKey(): Promise<Hex> {
25
+ return this.publicKey;
26
+ }
27
+ }
@@ -0,0 +1 @@
1
+ export * from "./signerCkbScriptReadonly";
@@ -0,0 +1,24 @@
1
+ import { Address } from "../../address";
2
+ import { Script, ScriptLike } from "../../ckb";
3
+ import { Client } from "../../client";
4
+ import { Signer } from "../signer";
5
+
6
+ export class SignerCkbScriptReadonly extends Signer {
7
+ private readonly script: Script;
8
+
9
+ constructor(client: Client, script: ScriptLike) {
10
+ super(client);
11
+
12
+ this.script = Script.from(script);
13
+ }
14
+
15
+ async connect(): Promise<void> {}
16
+
17
+ async getInternalAddress(): Promise<string> {
18
+ return this.getRecommendedAddress();
19
+ }
20
+
21
+ async getAddressObjs(): Promise<Address[]> {
22
+ return [await Address.fromScript(this.script, this.client)];
23
+ }
24
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./signerEvm";
2
+ export * from "./signerEvmAddressReadonly";
@@ -0,0 +1,58 @@
1
+ import { Address } from "../../address";
2
+ import { bytesConcat, bytesFrom } from "../../bytes";
3
+ import { Transaction, WitnessArgs } from "../../ckb";
4
+ import { KnownScript } from "../../client";
5
+ import { hexFrom } from "../../hex";
6
+ import { numToBytes } from "../../num";
7
+ import { getSignHashInfo } from "../helpers";
8
+ import { Signer } from "../signer";
9
+
10
+ export abstract class SignerEvm extends Signer {
11
+ abstract getEvmAccount(): Promise<string>;
12
+
13
+ async getInternalAddress(): Promise<string> {
14
+ return this.getEvmAccount();
15
+ }
16
+
17
+ async getAddressObjs(): Promise<Address[]> {
18
+ const account = await this.getEvmAccount();
19
+ return [
20
+ await Address.fromKnownScript(
21
+ KnownScript.OmniLock,
22
+ hexFrom([0x12, ...bytesFrom(account), 0x00]),
23
+ this.client,
24
+ ),
25
+ ];
26
+ }
27
+
28
+ async signOnlyTransaction(tx: Transaction): Promise<Transaction> {
29
+ const { script } = await this.getRecommendedAddressObj();
30
+ const info = await getSignHashInfo(tx, script);
31
+ if (!info) {
32
+ return tx;
33
+ }
34
+
35
+ const signature = bytesFrom(
36
+ await this.signMessage(`CKB transaction: ${info.message}`),
37
+ );
38
+ if (signature[signature.length - 1] >= 27) {
39
+ signature[signature.length - 1] -= 27;
40
+ }
41
+
42
+ const witness = WitnessArgs.fromBytes(tx.witnesses[info.position]);
43
+ witness.lock = hexFrom(
44
+ bytesConcat(
45
+ numToBytes(5 * 4 + signature.length, 4),
46
+ numToBytes(4 * 4, 4),
47
+ numToBytes(5 * 4 + signature.length, 4),
48
+ numToBytes(5 * 4 + signature.length, 4),
49
+ numToBytes(signature.length, 4),
50
+ signature,
51
+ ),
52
+ );
53
+
54
+ tx.witnesses[info.position] = hexFrom(witness.toBytes());
55
+
56
+ return tx;
57
+ }
58
+ }
@@ -0,0 +1,19 @@
1
+ import { Client } from "../../client";
2
+ import { Hex, HexLike, hexFrom } from "../../hex";
3
+ import { SignerEvm } from "./signerEvm";
4
+
5
+ export abstract class SignerEvmAddressReadonly extends SignerEvm {
6
+ private readonly address: Hex;
7
+
8
+ constructor(client: Client, address: HexLike) {
9
+ super(client);
10
+
11
+ this.address = hexFrom(address);
12
+ }
13
+
14
+ async connect(): Promise<void> {}
15
+
16
+ async getEvmAccount(): Promise<string> {
17
+ return this.address;
18
+ }
19
+ }
@@ -1,4 +1,5 @@
1
+ export * from "./btc";
2
+ export * from "./ckb";
3
+ export * from "./evm";
1
4
  export * from "./helpers";
2
5
  export * from "./signer";
3
- export * from "./signerReadonly";
4
- export * from "./signerReadonlyCkbScript";
@@ -25,7 +25,9 @@ export abstract class Signer {
25
25
  );
26
26
  }
27
27
 
28
- abstract signMessage(message: string | BytesLike): Promise<string>;
28
+ signMessage(_: string | BytesLike): Promise<string> {
29
+ throw Error("Signer.signMessage not implemented");
30
+ }
29
31
 
30
32
  async sendTransaction(tx: Transaction): Promise<Hex> {
31
33
  return this.client.sendTransaction(await this.signTransaction(tx));
@@ -35,5 +37,25 @@ export abstract class Signer {
35
37
  return this.signOnlyTransaction(tx);
36
38
  }
37
39
 
38
- abstract signOnlyTransaction(tx: Transaction): Promise<Transaction>;
40
+ signOnlyTransaction(_: Transaction): Promise<Transaction> {
41
+ throw Error("Signer.signOnlyTransaction not implemented");
42
+ }
43
+ }
44
+
45
+ export enum SignerType {
46
+ EVM = "EVM",
47
+ BTC = "BTC",
39
48
  }
49
+
50
+ export class SignerInfo {
51
+ constructor(
52
+ public type: SignerType,
53
+ public signer: Signer,
54
+ ) {}
55
+ }
56
+
57
+ export type Wallet = {
58
+ name: string;
59
+ icon: string;
60
+ signers: SignerInfo[];
61
+ };
@@ -1,7 +0,0 @@
1
- import { Signer } from ".";
2
- import { Transaction } from "../ckb";
3
- export declare abstract class SignerReadonly extends Signer {
4
- signMessage(): Promise<string>;
5
- signOnlyTransaction(): Promise<Transaction>;
6
- }
7
- //# sourceMappingURL=signerReadonly.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"signerReadonly.d.ts","sourceRoot":"","sources":["../../src/signer/signerReadonly.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,CAAC;AAC3B,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAErC,8BAAsB,cAAe,SAAQ,MAAM;IACjD,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC;IAI9B,mBAAmB,IAAI,OAAO,CAAC,WAAW,CAAC;CAG5C"}
@@ -1,9 +0,0 @@
1
- import { Signer } from ".";
2
- export class SignerReadonly extends Signer {
3
- signMessage() {
4
- throw new Error("Method not implemented.");
5
- }
6
- signOnlyTransaction() {
7
- throw new Error("Method not implemented.");
8
- }
9
- }
@@ -1,12 +0,0 @@
1
- import { Address } from "../address";
2
- import { Script } from "../ckb";
3
- import { Client } from "../client";
4
- import { SignerReadonly } from "./signerReadonly";
5
- export declare class SignerReadonlyCkbScript extends SignerReadonly {
6
- private readonly script;
7
- constructor(script: Script, client: Client);
8
- connect(): Promise<void>;
9
- getInternalAddress(): Promise<string>;
10
- getAddressObjs(): Promise<Address[]>;
11
- }
12
- //# sourceMappingURL=signerReadonlyCkbScript.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"signerReadonlyCkbScript.d.ts","sourceRoot":"","sources":["../../src/signer/signerReadonlyCkbScript.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD,qBAAa,uBAAwB,SAAQ,cAAc;IAEvD,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,MAAM,EAC/B,MAAM,EAAE,MAAM;IAKV,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAIxB,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAIrC,cAAc,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;CAG3C"}
@@ -1,17 +0,0 @@
1
- import { Address } from "../address";
2
- import { SignerReadonly } from "./signerReadonly";
3
- export class SignerReadonlyCkbScript extends SignerReadonly {
4
- constructor(script, client) {
5
- super(client);
6
- this.script = script;
7
- }
8
- async connect() {
9
- return;
10
- }
11
- async getInternalAddress() {
12
- return this.getRecommendedAddress();
13
- }
14
- async getAddressObjs() {
15
- return [await Address.fromScript(this.script, this.client)];
16
- }
17
- }
@@ -1,12 +0,0 @@
1
- import { Signer } from ".";
2
- import { Transaction } from "../ckb";
3
-
4
- export abstract class SignerReadonly extends Signer {
5
- signMessage(): Promise<string> {
6
- throw new Error("Method not implemented.");
7
- }
8
-
9
- signOnlyTransaction(): Promise<Transaction> {
10
- throw new Error("Method not implemented.");
11
- }
12
- }
@@ -1,25 +0,0 @@
1
- import { Address } from "../address";
2
- import { Script } from "../ckb";
3
- import { Client } from "../client";
4
- import { SignerReadonly } from "./signerReadonly";
5
-
6
- export class SignerReadonlyCkbScript extends SignerReadonly {
7
- constructor(
8
- private readonly script: Script,
9
- client: Client,
10
- ) {
11
- super(client);
12
- }
13
-
14
- async connect(): Promise<void> {
15
- return;
16
- }
17
-
18
- async getInternalAddress(): Promise<string> {
19
- return this.getRecommendedAddress();
20
- }
21
-
22
- async getAddressObjs(): Promise<Address[]> {
23
- return [await Address.fromScript(this.script, this.client)];
24
- }
25
- }