@ckb-ccc/uni-sat 0.0.3

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.
@@ -0,0 +1,2 @@
1
+ export * as UniSat from "./uni-sat.advanced";
2
+ //# sourceMappingURL=advanced.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"advanced.d.ts","sourceRoot":"","sources":["../src/advanced.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1 @@
1
+ export * as UniSat from "./uni-sat.advanced";
@@ -0,0 +1,3 @@
1
+ export * from "./signer";
2
+ export * from "./signersFactory";
3
+ //# sourceMappingURL=barrel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"barrel.d.ts","sourceRoot":"","sources":["../src/barrel.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,kBAAkB,CAAC"}
package/dist/barrel.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./signer";
2
+ export * from "./signersFactory";
@@ -0,0 +1,2 @@
1
+ export * as UniSat from "./barrel";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export * as UniSat from "./barrel";
@@ -0,0 +1,14 @@
1
+ import { ccc } from "@ckb-ccc/core";
2
+ import { Provider } from "./uni-sat.advanced";
3
+ export declare class Signer extends ccc.Signer {
4
+ readonly provider: Provider;
5
+ constructor(client: ccc.Client, provider: Provider);
6
+ getBTCAccount(): Promise<string>;
7
+ getBTCPublicKey(): Promise<ccc.Hex>;
8
+ getInternalAddress(): Promise<string>;
9
+ getAddressObjs(): Promise<ccc.Address[]>;
10
+ connect(): Promise<void>;
11
+ signMessage(message: string | ccc.BytesLike): Promise<string>;
12
+ signOnlyTransaction(tx: ccc.Transaction): Promise<ccc.Transaction>;
13
+ }
14
+ //# sourceMappingURL=signer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAGpC,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAE9C,qBAAa,MAAO,SAAQ,GAAG,CAAC,MAAM;aAGlB,QAAQ,EAAE,QAAQ;gBADlC,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,QAAQ,EAAE,QAAQ;IAK9B,aAAa;IAIb,eAAe,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAInC,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAIrC,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;IAaxC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAIxB,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;IAO7D,mBAAmB,CAAC,EAAE,EAAE,GAAG,CAAC,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC;CAiCzE"}
package/dist/signer.js ADDED
@@ -0,0 +1,46 @@
1
+ import { ccc } from "@ckb-ccc/core";
2
+ import { ripemd160 } from "@noble/hashes/ripemd160";
3
+ import { sha256 } from "@noble/hashes/sha256";
4
+ export class Signer extends ccc.Signer {
5
+ constructor(client, provider) {
6
+ super(client);
7
+ this.provider = provider;
8
+ }
9
+ async getBTCAccount() {
10
+ return (await this.provider.getAccounts())[0];
11
+ }
12
+ async getBTCPublicKey() {
13
+ return ccc.hexFrom(await this.provider.getPublicKey());
14
+ }
15
+ async getInternalAddress() {
16
+ return this.getBTCAccount();
17
+ }
18
+ async getAddressObjs() {
19
+ const publicKey = await this.getBTCPublicKey();
20
+ const hash = ripemd160(sha256(ccc.bytesFrom(publicKey)));
21
+ return [
22
+ await ccc.Address.fromKnownScript(ccc.KnownScript.OmniLock, ccc.hexFrom([0x04, ...hash, 0x00]), this.client),
23
+ ];
24
+ }
25
+ async connect() {
26
+ await this.provider.requestAccounts();
27
+ }
28
+ async signMessage(message) {
29
+ const challenge = typeof message === "string" ? message : ccc.hexFrom(message).slice(2);
30
+ return this.provider.signMessage(challenge, "ecdsa");
31
+ }
32
+ async signOnlyTransaction(tx) {
33
+ const { script } = await this.getRecommendedAddressObj();
34
+ const info = await ccc.getSignHashInfo(tx, script);
35
+ if (!info) {
36
+ return tx;
37
+ }
38
+ const signature = ccc.bytesFrom(await this.signMessage(`CKB (Bitcoin Layer) transaction: ${info.message}`), "base64");
39
+ signature[0] = 31 + ((signature[0] - 27) % 4);
40
+ console.log(ccc.hexFrom(signature));
41
+ const witness = ccc.WitnessArgs.fromBytes(tx.witnesses[info.position]);
42
+ witness.lock = ccc.hexFrom(ccc.bytesConcat(ccc.numToBytes(5 * 4 + signature.length, 4), ccc.numToBytes(4 * 4, 4), ccc.numToBytes(5 * 4 + signature.length, 4), ccc.numToBytes(5 * 4 + signature.length, 4), ccc.numToBytes(signature.length, 4), signature));
43
+ tx.witnesses[info.position] = ccc.hexFrom(witness.toBytes());
44
+ return tx;
45
+ }
46
+ }
@@ -0,0 +1,4 @@
1
+ import { ccc } from "@ckb-ccc/core";
2
+ import { Signer } from "./signer";
3
+ export declare function getUniSatSigner(client: ccc.Client): Signer | undefined;
4
+ //# sourceMappingURL=signersFactory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signersFactory.d.ts","sourceRoot":"","sources":["../src/signersFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAGlC,wBAAgB,eAAe,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,GAAG,MAAM,GAAG,SAAS,CAQtE"}
@@ -0,0 +1,8 @@
1
+ import { Signer } from "./signer";
2
+ export function getUniSatSigner(client) {
3
+ const windowRef = window;
4
+ if (typeof windowRef.unisat === "undefined") {
5
+ return;
6
+ }
7
+ return new Signer(client, windowRef.unisat);
8
+ }
@@ -0,0 +1,12 @@
1
+ export interface Provider {
2
+ requestAccounts(): Promise<string[]>;
3
+ getAccounts(): Promise<string[]>;
4
+ getPublicKey(): Promise<string>;
5
+ signMessage(msg: string, type: "ecdsa" | "bip322-simple"): Promise<string>;
6
+ on: OnMethod;
7
+ removeListener(eventName: string, listener: (...args: unknown[]) => unknown): Provider;
8
+ }
9
+ export interface OnMethod {
10
+ (eventName: string, listener: (...args: unknown[]) => unknown): Provider;
11
+ }
12
+ //# sourceMappingURL=uni-sat.advanced.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uni-sat.advanced.d.ts","sourceRoot":"","sources":["../src/uni-sat.advanced.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,QAAQ;IACvB,eAAe,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IACrC,WAAW,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IACjC,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAChC,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAE3E,EAAE,EAAE,QAAQ,CAAC;IACb,cAAc,CACZ,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,GACxC,QAAQ,CAAC;CACb;AAED,MAAM,WAAW,QAAQ;IACvB,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,GAAG,QAAQ,CAAC;CAC1E"}
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@ckb-ccc/uni-sat",
3
+ "version": "0.0.3",
4
+ "description": "Common Chains Connector's support for UniSat",
5
+ "author": "Hanssen0 <hanssen0@hanssen0.com>",
6
+ "license": "MIT",
7
+ "private": false,
8
+ "homepage": "https://github.com/Hanssen0/ccc",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git://github.com/Hanssen0/ccc.git"
12
+ },
13
+ "main": "dist/index.js",
14
+ "exports": {
15
+ ".": "./dist/index.js",
16
+ "./advanced": "./dist/advanced.js"
17
+ },
18
+ "scripts": {
19
+ "build": "rimraf ./dist && tsc",
20
+ "lint": "eslint",
21
+ "format": "prettier --write . && eslint --fix"
22
+ },
23
+ "devDependencies": {
24
+ "@eslint/js": "^9.1.1",
25
+ "eslint": "^9.1.0",
26
+ "eslint-config-prettier": "^9.1.0",
27
+ "eslint-plugin-prettier": "^5.1.3",
28
+ "prettier": "^3.2.5",
29
+ "prettier-plugin-organize-imports": "^3.2.4",
30
+ "rimraf": "^5.0.5",
31
+ "typescript": "^5.4.5",
32
+ "typescript-eslint": "^7.7.0"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "dependencies": {
38
+ "@ckb-ccc/core": "0.0.3",
39
+ "@noble/hashes": "^1.4.0"
40
+ },
41
+ "gitHead": "9df1bbe0007136568914d8a73fe39f898d636a12"
42
+ }
@@ -0,0 +1 @@
1
+ export * as UniSat from "./uni-sat.advanced";
package/src/barrel.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./signer";
2
+ export * from "./signersFactory";
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * as UniSat from "./barrel";
package/src/signer.ts ADDED
@@ -0,0 +1,83 @@
1
+ import { ccc } from "@ckb-ccc/core";
2
+ import { ripemd160 } from "@noble/hashes/ripemd160";
3
+ import { sha256 } from "@noble/hashes/sha256";
4
+ import { Provider } from "./uni-sat.advanced";
5
+
6
+ export class Signer extends ccc.Signer {
7
+ constructor(
8
+ client: ccc.Client,
9
+ public readonly provider: Provider,
10
+ ) {
11
+ super(client);
12
+ }
13
+
14
+ async getBTCAccount() {
15
+ return (await this.provider.getAccounts())[0];
16
+ }
17
+
18
+ async getBTCPublicKey(): Promise<ccc.Hex> {
19
+ return ccc.hexFrom(await this.provider.getPublicKey());
20
+ }
21
+
22
+ async getInternalAddress(): Promise<string> {
23
+ return this.getBTCAccount();
24
+ }
25
+
26
+ async getAddressObjs(): Promise<ccc.Address[]> {
27
+ const publicKey = await this.getBTCPublicKey();
28
+ const hash = ripemd160(sha256(ccc.bytesFrom(publicKey)));
29
+
30
+ return [
31
+ await ccc.Address.fromKnownScript(
32
+ ccc.KnownScript.OmniLock,
33
+ ccc.hexFrom([0x04, ...hash, 0x00]),
34
+ this.client,
35
+ ),
36
+ ];
37
+ }
38
+
39
+ async connect(): Promise<void> {
40
+ await this.provider.requestAccounts();
41
+ }
42
+
43
+ async signMessage(message: string | ccc.BytesLike): Promise<string> {
44
+ const challenge =
45
+ typeof message === "string" ? message : ccc.hexFrom(message).slice(2);
46
+
47
+ return this.provider.signMessage(challenge, "ecdsa");
48
+ }
49
+
50
+ async signOnlyTransaction(tx: ccc.Transaction): Promise<ccc.Transaction> {
51
+ const { script } = await this.getRecommendedAddressObj();
52
+ const info = await ccc.getSignHashInfo(tx, script);
53
+ if (!info) {
54
+ return tx;
55
+ }
56
+
57
+ const signature = ccc.bytesFrom(
58
+ await this.signMessage(
59
+ `CKB (Bitcoin Layer) transaction: ${info.message}`,
60
+ ),
61
+ "base64",
62
+ );
63
+ signature[0] = 31 + ((signature[0] - 27) % 4);
64
+
65
+ console.log(ccc.hexFrom(signature));
66
+
67
+ const witness = ccc.WitnessArgs.fromBytes(tx.witnesses[info.position]);
68
+ witness.lock = ccc.hexFrom(
69
+ ccc.bytesConcat(
70
+ ccc.numToBytes(5 * 4 + signature.length, 4),
71
+ ccc.numToBytes(4 * 4, 4),
72
+ ccc.numToBytes(5 * 4 + signature.length, 4),
73
+ ccc.numToBytes(5 * 4 + signature.length, 4),
74
+ ccc.numToBytes(signature.length, 4),
75
+ signature,
76
+ ),
77
+ );
78
+
79
+ tx.witnesses[info.position] = ccc.hexFrom(witness.toBytes());
80
+
81
+ return tx;
82
+ }
83
+ }
@@ -0,0 +1,13 @@
1
+ import { ccc } from "@ckb-ccc/core";
2
+ import { Signer } from "./signer";
3
+ import { Provider } from "./uni-sat.advanced";
4
+
5
+ export function getUniSatSigner(client: ccc.Client): Signer | undefined {
6
+ const windowRef = window as { unisat?: Provider };
7
+
8
+ if (typeof windowRef.unisat === "undefined") {
9
+ return;
10
+ }
11
+
12
+ return new Signer(client, windowRef.unisat);
13
+ }
@@ -0,0 +1,16 @@
1
+ export interface Provider {
2
+ requestAccounts(): Promise<string[]>;
3
+ getAccounts(): Promise<string[]>;
4
+ getPublicKey(): Promise<string>;
5
+ signMessage(msg: string, type: "ecdsa" | "bip322-simple"): Promise<string>;
6
+
7
+ on: OnMethod;
8
+ removeListener(
9
+ eventName: string,
10
+ listener: (...args: unknown[]) => unknown,
11
+ ): Provider;
12
+ }
13
+
14
+ export interface OnMethod {
15
+ (eventName: string, listener: (...args: unknown[]) => unknown): Provider;
16
+ }