@ckb-ccc/core 0.0.3-alpha.0 → 0.0.4-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 (65) hide show
  1. package/dist/bytes/advanced.d.ts +2 -0
  2. package/dist/bytes/advanced.d.ts.map +1 -0
  3. package/dist/bytes/advanced.js +1 -0
  4. package/dist/bytes/index.d.ts +3 -3
  5. package/dist/bytes/index.d.ts.map +1 -1
  6. package/dist/bytes/index.js +6 -6
  7. package/dist/ckb/transaction.js +2 -2
  8. package/dist/hasher/index.d.ts.map +1 -1
  9. package/dist/hasher/index.js +2 -2
  10. package/dist/hex/index.d.ts.map +1 -1
  11. package/dist/hex/index.js +2 -3
  12. package/dist/signer/btc/index.d.ts +3 -0
  13. package/dist/signer/btc/index.d.ts.map +1 -0
  14. package/dist/signer/btc/index.js +2 -0
  15. package/dist/signer/btc/signerBtc.d.ts +12 -0
  16. package/dist/signer/btc/signerBtc.d.ts.map +1 -0
  17. package/dist/signer/btc/signerBtc.js +35 -0
  18. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts +12 -0
  19. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -0
  20. package/dist/signer/btc/signerBtcPublicKeyReadonly.js +16 -0
  21. package/dist/signer/ckb/index.d.ts +2 -0
  22. package/dist/signer/ckb/index.d.ts.map +1 -0
  23. package/dist/signer/ckb/index.js +1 -0
  24. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +12 -0
  25. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -0
  26. package/dist/signer/ckb/signerCkbScriptReadonly.js +16 -0
  27. package/dist/signer/evm/index.d.ts +3 -0
  28. package/dist/signer/evm/index.d.ts.map +1 -0
  29. package/dist/signer/evm/index.js +2 -0
  30. package/dist/signer/evm/signerEvm.d.ts +10 -0
  31. package/dist/signer/evm/signerEvm.d.ts.map +1 -0
  32. package/dist/signer/evm/signerEvm.js +34 -0
  33. package/dist/signer/evm/signerEvmAddressReadonly.d.ts +10 -0
  34. package/dist/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -0
  35. package/dist/signer/evm/signerEvmAddressReadonly.js +12 -0
  36. package/dist/signer/index.d.ts +3 -2
  37. package/dist/signer/index.d.ts.map +1 -1
  38. package/dist/signer/index.js +3 -2
  39. package/dist/signer/signer.d.ts +16 -2
  40. package/dist/signer/signer.d.ts.map +1 -1
  41. package/dist/signer/signer.js +17 -0
  42. package/package.json +3 -2
  43. package/src/bytes/advanced.ts +10 -0
  44. package/src/bytes/index.ts +11 -7
  45. package/src/ckb/transaction.ts +2 -2
  46. package/src/hasher/index.ts +2 -2
  47. package/src/hex/index.ts +2 -3
  48. package/src/signer/btc/index.ts +2 -0
  49. package/src/signer/btc/signerBtc.ts +65 -0
  50. package/src/signer/btc/signerBtcPublicKeyReadonly.ts +27 -0
  51. package/src/signer/ckb/index.ts +1 -0
  52. package/src/signer/ckb/signerCkbScriptReadonly.ts +24 -0
  53. package/src/signer/evm/index.ts +2 -0
  54. package/src/signer/evm/signerEvm.ts +58 -0
  55. package/src/signer/evm/signerEvmAddressReadonly.ts +19 -0
  56. package/src/signer/index.ts +3 -2
  57. package/src/signer/signer.ts +24 -2
  58. package/dist/signer/signerReadonly.d.ts +0 -8
  59. package/dist/signer/signerReadonly.d.ts.map +0 -1
  60. package/dist/signer/signerReadonly.js +0 -9
  61. package/dist/signer/signerReadonlyCkbScript.d.ts +0 -12
  62. package/dist/signer/signerReadonlyCkbScript.d.ts.map +0 -1
  63. package/dist/signer/signerReadonlyCkbScript.js +0 -17
  64. package/src/signer/signerReadonly.ts +0 -13
  65. package/src/signer/signerReadonlyCkbScript.ts +0 -25
@@ -0,0 +1,2 @@
1
+ export type BytesFromEncoding = "utf8" | "utf16le" | "latin1" | "base64" | "base64url" | "hex" | "ascii" | "binary" | "ucs2";
2
+ //# sourceMappingURL=advanced.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"advanced.d.ts","sourceRoot":"","sources":["../../src/bytes/advanced.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,iBAAiB,GACzB,MAAM,GACN,SAAS,GACT,QAAQ,GACR,QAAQ,GACR,WAAW,GACX,KAAK,GACL,OAAO,GACP,QAAQ,GACR,MAAM,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -1,7 +1,7 @@
1
+ import { BytesFromEncoding } from "./advanced";
1
2
  export type Bytes = Uint8Array;
2
3
  export type BytesLike = string | Uint8Array | ArrayBuffer | number[];
3
4
  export declare function bytesConcat(...args: BytesLike[]): Bytes;
4
- export declare function bytesFromUtf8(val: string): Bytes;
5
- export declare function bytesToUtf8(val: BytesLike): string;
6
- export declare function bytesFrom(bytes: BytesLike): Bytes;
5
+ export declare function bytesTo(val: BytesLike, encoding: BytesFromEncoding): string;
6
+ export declare function bytesFrom(bytes: BytesLike, encoding?: BytesFromEncoding): Bytes;
7
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/bytes/index.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,KAAK,GAAG,UAAU,CAAC;AAC/B,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,UAAU,GAAG,WAAW,GAAG,MAAM,EAAE,CAAC;AAErE,wBAAgB,WAAW,CAAC,GAAG,IAAI,EAAE,SAAS,EAAE,GAAG,KAAK,CAOvD;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,KAAK,CAEhD;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,SAAS,GAAG,MAAM,CAElD;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,SAAS,GAAG,KAAK,CAuBjD"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/bytes/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAE/C,MAAM,MAAM,KAAK,GAAG,UAAU,CAAC;AAC/B,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,UAAU,GAAG,WAAW,GAAG,MAAM,EAAE,CAAC;AAErE,wBAAgB,WAAW,CAAC,GAAG,IAAI,EAAE,SAAS,EAAE,GAAG,KAAK,CAOvD;AAED,wBAAgB,OAAO,CAAC,GAAG,EAAE,SAAS,EAAE,QAAQ,EAAE,iBAAiB,GAAG,MAAM,CAE3E;AAED,wBAAgB,SAAS,CACvB,KAAK,EAAE,SAAS,EAChB,QAAQ,CAAC,EAAE,iBAAiB,GAC3B,KAAK,CA2BP"}
@@ -5,13 +5,10 @@ export function bytesConcat(...args) {
5
5
  return acc;
6
6
  }, []));
7
7
  }
8
- export function bytesFromUtf8(val) {
9
- return Buffer.from(val, "utf-8");
8
+ export function bytesTo(val, encoding) {
9
+ return Buffer.from(bytesFrom(val)).toString(encoding);
10
10
  }
11
- export function bytesToUtf8(val) {
12
- return Buffer.from(bytesFrom(val)).toString("utf-8");
13
- }
14
- export function bytesFrom(bytes) {
11
+ export function bytesFrom(bytes, encoding) {
15
12
  if (bytes instanceof Uint8Array) {
16
13
  return bytes;
17
14
  }
@@ -24,6 +21,9 @@ export function bytesFrom(bytes) {
24
21
  }
25
22
  return new Uint8Array(bytes);
26
23
  }
24
+ if (encoding !== undefined) {
25
+ return Buffer.from(bytes, encoding);
26
+ }
27
27
  const str = bytes.startsWith("0x") ? bytes.slice(2) : bytes;
28
28
  const paddedStr = str.length % 2 === 0 ? str : `0${str}`;
29
29
  const data = Buffer.from(paddedStr, "hex");
@@ -210,10 +210,10 @@ export class Transaction {
210
210
  return bytesFrom(mol.SerializeRawTransaction({
211
211
  version: numToBytes(this.version, 4),
212
212
  cellDeps: this.cellDeps.map((d) => d._toMolData()),
213
- headerDeps: this.headerDeps.map(bytesFrom),
213
+ headerDeps: this.headerDeps.map((header) => bytesFrom(header)),
214
214
  inputs: this.inputs.map((i) => i._toMolData()),
215
215
  outputs: this.outputs.map((o) => o._toMolData()),
216
- outputsData: this.outputsData.map(bytesFrom),
216
+ outputsData: this.outputsData.map((header) => bytesFrom(header)),
217
217
  }));
218
218
  }
219
219
  hash() {
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hasher/index.ts"],"names":[],"mappings":"AAAA,OAAgB,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,EAAE,SAAS,EAA4B,MAAM,UAAU,CAAC;AAC/D,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAG7B,qBAAa,MAAM;IACjB,MAAM,EAAE,OAAO,CAAC;gBAEJ,SAAS,SAAK,EAAE,QAAQ,SAAuB;IAS3D,MAAM,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM;IAK/B,MAAM,IAAI,GAAG;CAGd;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,SAAS,GAAG,GAAG,CAE5C"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hasher/index.ts"],"names":[],"mappings":"AAAA,OAAgB,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAa,MAAM,UAAU,CAAC;AAChD,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAG7B,qBAAa,MAAM;IACjB,MAAM,EAAE,OAAO,CAAC;gBAEJ,SAAS,SAAK,EAAE,QAAQ,SAAuB;IAS3D,MAAM,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM;IAK/B,MAAM,IAAI,GAAG;CAGd;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,SAAS,GAAG,GAAG,CAE5C"}
@@ -1,9 +1,9 @@
1
1
  import blake2b from "blake2b";
2
- import { bytesFrom, bytesFromUtf8 } from "../bytes";
2
+ import { bytesFrom } from "../bytes";
3
3
  import { CKB_BLAKE2B_PERSONAL } from "./advanced";
4
4
  export class Hasher {
5
5
  constructor(outLength = 32, personal = CKB_BLAKE2B_PERSONAL) {
6
- this.hasher = blake2b(outLength, undefined, undefined, bytesFromUtf8(personal));
6
+ this.hasher = blake2b(outLength, undefined, undefined, bytesFrom(personal, "utf8"));
7
7
  }
8
8
  update(data) {
9
9
  this.hasher.update(bytesFrom(data));
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hex/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAa,SAAS,EAAE,MAAM,UAAU,CAAC;AAEhD,MAAM,MAAM,GAAG,GAAG,KAAK,MAAM,EAAE,CAAC;AAChC,MAAM,MAAM,OAAO,GAAG,SAAS,CAAC;AAEhC,wBAAgB,OAAO,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,CAEzC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hex/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,SAAS,EAAW,MAAM,UAAU,CAAC;AAEzD,MAAM,MAAM,GAAG,GAAG,KAAK,MAAM,EAAE,CAAC;AAChC,MAAM,MAAM,OAAO,GAAG,SAAS,CAAC;AAEhC,wBAAgB,OAAO,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,CAEzC"}
package/dist/hex/index.js CHANGED
@@ -1,5 +1,4 @@
1
- import { Buffer } from "buffer/";
2
- import { bytesFrom } from "../bytes";
1
+ import { bytesFrom, bytesTo } from "../bytes";
3
2
  export function hexFrom(hex) {
4
- return `0x${Buffer.from(bytesFrom(hex)).toString("hex")}`;
3
+ return `0x${bytesTo(bytesFrom(hex), "hex")}`;
5
4
  }
@@ -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<Hex>;
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,GAAG,CAAC;IAEzD,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,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckb-ccc/core",
3
- "version": "0.0.3-alpha.0",
3
+ "version": "0.0.4-alpha.0",
4
4
  "description": "Common Chains Connector Core",
5
5
  "author": "Hanssen0 <hanssen0@hanssen0.com>",
6
6
  "license": "MIT",
@@ -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": "4a995208e5135d11e9f72b9a9fd73174a3056fc7"
50
+ "gitHead": "e49045ed4b2778c92d6f49b79c17125245141a21"
50
51
  }
@@ -0,0 +1,10 @@
1
+ export type BytesFromEncoding =
2
+ | "utf8"
3
+ | "utf16le"
4
+ | "latin1"
5
+ | "base64"
6
+ | "base64url"
7
+ | "hex"
8
+ | "ascii"
9
+ | "binary"
10
+ | "ucs2";
@@ -1,4 +1,5 @@
1
1
  import { Buffer } from "buffer/";
2
+ import { BytesFromEncoding } from "./advanced";
2
3
 
3
4
  export type Bytes = Uint8Array;
4
5
  export type BytesLike = string | Uint8Array | ArrayBuffer | number[];
@@ -12,15 +13,14 @@ export function bytesConcat(...args: BytesLike[]): Bytes {
12
13
  );
13
14
  }
14
15
 
15
- export function bytesFromUtf8(val: string): Bytes {
16
- return Buffer.from(val, "utf-8");
16
+ export function bytesTo(val: BytesLike, encoding: BytesFromEncoding): string {
17
+ return Buffer.from(bytesFrom(val)).toString(encoding);
17
18
  }
18
19
 
19
- export function bytesToUtf8(val: BytesLike): string {
20
- return Buffer.from(bytesFrom(val)).toString("utf-8");
21
- }
22
-
23
- export function bytesFrom(bytes: BytesLike): Bytes {
20
+ export function bytesFrom(
21
+ bytes: BytesLike,
22
+ encoding?: BytesFromEncoding,
23
+ ): Bytes {
24
24
  if (bytes instanceof Uint8Array) {
25
25
  return bytes;
26
26
  }
@@ -36,6 +36,10 @@ export function bytesFrom(bytes: BytesLike): Bytes {
36
36
  return new Uint8Array(bytes);
37
37
  }
38
38
 
39
+ if (encoding !== undefined) {
40
+ return Buffer.from(bytes, encoding);
41
+ }
42
+
39
43
  const str = bytes.startsWith("0x") ? bytes.slice(2) : bytes;
40
44
  const paddedStr = str.length % 2 === 0 ? str : `0${str}`;
41
45
  const data = Buffer.from(paddedStr, "hex");
@@ -337,10 +337,10 @@ export class Transaction {
337
337
  mol.SerializeRawTransaction({
338
338
  version: numToBytes(this.version, 4),
339
339
  cellDeps: this.cellDeps.map((d) => d._toMolData()),
340
- headerDeps: this.headerDeps.map(bytesFrom),
340
+ headerDeps: this.headerDeps.map((header) => bytesFrom(header)),
341
341
  inputs: this.inputs.map((i) => i._toMolData()),
342
342
  outputs: this.outputs.map((o) => o._toMolData()),
343
- outputsData: this.outputsData.map(bytesFrom),
343
+ outputsData: this.outputsData.map((header) => bytesFrom(header)),
344
344
  }),
345
345
  );
346
346
  }
@@ -1,5 +1,5 @@
1
1
  import blake2b, { Blake2b } from "blake2b";
2
- import { BytesLike, bytesFrom, bytesFromUtf8 } from "../bytes";
2
+ import { BytesLike, bytesFrom } from "../bytes";
3
3
  import { Hex } from "../hex";
4
4
  import { CKB_BLAKE2B_PERSONAL } from "./advanced";
5
5
 
@@ -11,7 +11,7 @@ export class Hasher {
11
11
  outLength,
12
12
  undefined,
13
13
  undefined,
14
- bytesFromUtf8(personal),
14
+ bytesFrom(personal, "utf8"),
15
15
  );
16
16
  }
17
17
 
package/src/hex/index.ts CHANGED
@@ -1,9 +1,8 @@
1
- import { Buffer } from "buffer/";
2
- import { bytesFrom, BytesLike } from "../bytes";
1
+ import { bytesFrom, BytesLike, bytesTo } from "../bytes";
3
2
 
4
3
  export type Hex = `0x${string}`;
5
4
  export type HexLike = BytesLike;
6
5
 
7
6
  export function hexFrom(hex: HexLike): Hex {
8
- return `0x${Buffer.from(bytesFrom(hex)).toString("hex")}`;
7
+ return `0x${bytesTo(bytesFrom(hex), "hex")}`;
9
8
  }
@@ -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<Hex>;
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,8 +0,0 @@
1
- import { Signer } from ".";
2
- import { Transaction } from "../ckb";
3
- import { Hex } from "../hex";
4
- export declare abstract class SignerReadonly extends Signer {
5
- signMessage(): Promise<Hex>;
6
- signOnlyTransaction(): Promise<Transaction>;
7
- }
8
- //# 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;AACrC,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAE7B,8BAAsB,cAAe,SAAQ,MAAM;IACjD,WAAW,IAAI,OAAO,CAAC,GAAG,CAAC;IAI3B,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,13 +0,0 @@
1
- import { Signer } from ".";
2
- import { Transaction } from "../ckb";
3
- import { Hex } from "../hex";
4
-
5
- export abstract class SignerReadonly extends Signer {
6
- signMessage(): Promise<Hex> {
7
- throw new Error("Method not implemented.");
8
- }
9
-
10
- signOnlyTransaction(): Promise<Transaction> {
11
- throw new Error("Method not implemented.");
12
- }
13
- }
@@ -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
- }