@ckb-ccc/uni-sat 0.0.10-alpha.2 → 0.0.10-alpha.4
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.
- package/dist/signer.d.ts +1 -0
- package/dist/signer.d.ts.map +1 -1
- package/dist/signer.js +15 -4
- package/dist.commonjs/advanced.d.ts +1 -1
- package/dist.commonjs/advanced.js +40 -19
- package/dist.commonjs/advancedBarrel.d.ts +71 -68
- package/dist.commonjs/barrel.d.ts +1 -1
- package/dist.commonjs/barrel.js +30 -14
- package/dist.commonjs/index.d.ts +1 -1
- package/dist.commonjs/index.js +40 -19
- package/dist.commonjs/signer.d.ts +45 -40
- package/dist.commonjs/signer.d.ts.map +1 -1
- package/dist.commonjs/signer.js +131 -105
- package/dist.commonjs/signersFactory.d.ts +5 -2
- package/dist.commonjs/signersFactory.js +5 -5
- package/package.json +3 -3
- package/src/signer.ts +18 -4
package/dist/signer.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ export declare class Signer extends ccc.SignerBtc {
|
|
|
14
14
|
* @param {Provider} provider - The provider instance.
|
|
15
15
|
*/
|
|
16
16
|
constructor(client: ccc.Client, provider: Provider, preferredNetworks?: ccc.NetworkPreference[]);
|
|
17
|
+
_getNetworkToChange(): Promise<string | undefined>;
|
|
17
18
|
/**
|
|
18
19
|
* Ensure the BTC network is the same as CKB network.
|
|
19
20
|
*/
|
package/dist/signer.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C;;;;GAIG;AACH,qBAAa,MAAO,SAAQ,GAAG,CAAC,SAAS;aAQrB,QAAQ,EAAE,QAAQ;IAClC,OAAO,CAAC,QAAQ,CAAC,iBAAiB;IARpC;;;;OAIG;gBAED,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,QAAQ,EAAE,QAAQ,EACjB,iBAAiB,GAAE,GAAG,CAAC,iBAAiB,EAWxD;
|
|
1
|
+
{"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C;;;;GAIG;AACH,qBAAa,MAAO,SAAQ,GAAG,CAAC,SAAS;aAQrB,QAAQ,EAAE,QAAQ;IAClC,OAAO,CAAC,QAAQ,CAAC,iBAAiB;IARpC;;;;OAIG;gBAED,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,QAAQ,EAAE,QAAQ,EACjB,iBAAiB,GAAE,GAAG,CAAC,iBAAiB,EAWxD;IAKG,mBAAmB,IAAI,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IA0BxD;;OAEG;IACG,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IA2BpC;;;OAGG;IACG,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;IAItC;;;OAGG;IACG,eAAe,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAIzC;;;OAGG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAK9B,UAAU,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI;IAiB5C;;;OAGG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAOrC;;;;OAIG;IACG,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;CAMvE"}
|
package/dist/signer.js
CHANGED
|
@@ -26,10 +26,7 @@ export class Signer extends ccc.SignerBtc {
|
|
|
26
26
|
this.provider = provider;
|
|
27
27
|
this.preferredNetworks = preferredNetworks;
|
|
28
28
|
}
|
|
29
|
-
|
|
30
|
-
* Ensure the BTC network is the same as CKB network.
|
|
31
|
-
*/
|
|
32
|
-
async ensureNetwork() {
|
|
29
|
+
async _getNetworkToChange() {
|
|
33
30
|
const currentNetwork = await (async () => {
|
|
34
31
|
if (this.provider.getChain) {
|
|
35
32
|
return ({
|
|
@@ -46,6 +43,16 @@ export class Signer extends ccc.SignerBtc {
|
|
|
46
43
|
if (network === currentNetwork) {
|
|
47
44
|
return;
|
|
48
45
|
}
|
|
46
|
+
return network;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Ensure the BTC network is the same as CKB network.
|
|
50
|
+
*/
|
|
51
|
+
async ensureNetwork() {
|
|
52
|
+
const network = await this._getNetworkToChange();
|
|
53
|
+
if (!network) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
49
56
|
if (this.provider.switchChain) {
|
|
50
57
|
const chain = {
|
|
51
58
|
btc: "BITCOIN_MAINNET",
|
|
@@ -59,6 +66,7 @@ export class Signer extends ccc.SignerBtc {
|
|
|
59
66
|
}
|
|
60
67
|
else if (network === "btc" || network === "btcTestnet") {
|
|
61
68
|
await this.provider.switchNetwork(network === "btc" ? "livenet" : "testnet");
|
|
69
|
+
return;
|
|
62
70
|
}
|
|
63
71
|
throw new Error(`UniSat wallet doesn't support the requested chain ${network}`);
|
|
64
72
|
}
|
|
@@ -103,6 +111,9 @@ export class Signer extends ccc.SignerBtc {
|
|
|
103
111
|
* @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
|
|
104
112
|
*/
|
|
105
113
|
async isConnected() {
|
|
114
|
+
if (await this._getNetworkToChange()) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
106
117
|
return (await this.provider.getAccounts()).length !== 0;
|
|
107
118
|
}
|
|
108
119
|
/**
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * as UniSatA from "./advancedBarrel";
|
|
2
|
-
//# sourceMappingURL=advanced.d.ts.map
|
|
2
|
+
//# sourceMappingURL=advanced.d.ts.map
|
|
@@ -1,27 +1,48 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding =
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
2
|
+
var __createBinding =
|
|
3
|
+
(this && this.__createBinding) ||
|
|
4
|
+
(Object.create
|
|
5
|
+
? function (o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (
|
|
9
|
+
!desc ||
|
|
10
|
+
("get" in desc ? !m.__esModule : desc.writable || desc.configurable)
|
|
11
|
+
) {
|
|
12
|
+
desc = {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return m[k];
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(o, k2, desc);
|
|
20
|
+
}
|
|
21
|
+
: function (o, m, k, k2) {
|
|
22
|
+
if (k2 === undefined) k2 = k;
|
|
23
|
+
o[k2] = m[k];
|
|
24
|
+
});
|
|
25
|
+
var __setModuleDefault =
|
|
26
|
+
(this && this.__setModuleDefault) ||
|
|
27
|
+
(Object.create
|
|
28
|
+
? function (o, v) {
|
|
29
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
30
|
+
}
|
|
31
|
+
: function (o, v) {
|
|
32
|
+
o["default"] = v;
|
|
33
|
+
});
|
|
34
|
+
var __importStar =
|
|
35
|
+
(this && this.__importStar) ||
|
|
36
|
+
function (mod) {
|
|
19
37
|
if (mod && mod.__esModule) return mod;
|
|
20
38
|
var result = {};
|
|
21
|
-
if (mod != null)
|
|
39
|
+
if (mod != null)
|
|
40
|
+
for (var k in mod)
|
|
41
|
+
if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
|
|
42
|
+
__createBinding(result, mod, k);
|
|
22
43
|
__setModuleDefault(result, mod);
|
|
23
44
|
return result;
|
|
24
|
-
};
|
|
45
|
+
};
|
|
25
46
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
47
|
exports.UniSatA = void 0;
|
|
27
48
|
exports.UniSatA = __importStar(require("./advancedBarrel"));
|
|
@@ -3,78 +3,81 @@
|
|
|
3
3
|
* @interface
|
|
4
4
|
*/
|
|
5
5
|
export interface Provider {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Requests user accounts.
|
|
8
|
+
* @returns {Promise<string[]>} A promise that resolves to an array of account addresses.
|
|
9
|
+
*/
|
|
10
|
+
requestAccounts(): Promise<string[]>;
|
|
11
|
+
/**
|
|
12
|
+
* Gets the current network.
|
|
13
|
+
* @returns current network.
|
|
14
|
+
*/
|
|
15
|
+
getNetwork(): Promise<"livenet" | "testnet">;
|
|
16
|
+
/**
|
|
17
|
+
* Switch the current network.
|
|
18
|
+
*/
|
|
19
|
+
switchNetwork(chain: "livenet" | "testnet"): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* Gets the current chain.
|
|
22
|
+
* @returns current chain.
|
|
23
|
+
*/
|
|
24
|
+
getChain?(): Promise<{
|
|
25
|
+
enum: string;
|
|
26
|
+
name: string;
|
|
27
|
+
network: string;
|
|
28
|
+
}>;
|
|
29
|
+
/**
|
|
30
|
+
* Switch the current chain.
|
|
31
|
+
*/
|
|
32
|
+
switchChain?(chain: string): Promise<{
|
|
33
|
+
enum: string;
|
|
34
|
+
name: string;
|
|
35
|
+
network: string;
|
|
36
|
+
}>;
|
|
37
|
+
/**
|
|
38
|
+
* Gets the current accounts.
|
|
39
|
+
* @returns {Promise<string[]>} A promise that resolves to an array of account addresses.
|
|
40
|
+
*/
|
|
41
|
+
getAccounts(): Promise<string[]>;
|
|
42
|
+
/**
|
|
43
|
+
* Gets the public key of the account.
|
|
44
|
+
* @returns {Promise<string>} A promise that resolves to the public key.
|
|
45
|
+
*/
|
|
46
|
+
getPublicKey(): Promise<string>;
|
|
47
|
+
/**
|
|
48
|
+
* Signs a message with the specified type.
|
|
49
|
+
* @param {string} msg - The message to sign.
|
|
50
|
+
* @param {"ecdsa" | "bip322-simple"} type - The type of signature.
|
|
51
|
+
* @returns {Promise<string>} A promise that resolves to the signed message.
|
|
52
|
+
*/
|
|
53
|
+
signMessage(msg: string, type: "ecdsa" | "bip322-simple"): Promise<string>;
|
|
54
|
+
/**
|
|
55
|
+
* Adds an event listener to the provider.
|
|
56
|
+
* @type {OnMethod}
|
|
57
|
+
*/
|
|
58
|
+
on: OnMethod;
|
|
59
|
+
/**
|
|
60
|
+
* Removes an event listener from the provider.
|
|
61
|
+
* @param {string} eventName - The name of the event to remove the listener from.
|
|
62
|
+
* @param {(...args: unknown[]) => unknown} listener - The listener function to remove.
|
|
63
|
+
* @returns {Provider} The provider instance.
|
|
64
|
+
*/
|
|
65
|
+
removeListener(
|
|
66
|
+
eventName: string,
|
|
67
|
+
listener: (...args: unknown[]) => unknown,
|
|
68
|
+
): Provider;
|
|
66
69
|
}
|
|
67
70
|
/**
|
|
68
71
|
* Interface representing a method to add event listeners to the provider.
|
|
69
72
|
* @interface
|
|
70
73
|
*/
|
|
71
74
|
export interface OnMethod {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Adds an event listener to the provider.
|
|
77
|
+
* @param {string} eventName - The name of the event.
|
|
78
|
+
* @param {(...args: unknown[]) => unknown} listener - The listener function.
|
|
79
|
+
* @returns {Provider} The provider instance.
|
|
80
|
+
*/
|
|
81
|
+
(eventName: string, listener: (...args: unknown[]) => unknown): Provider;
|
|
79
82
|
}
|
|
80
|
-
//# sourceMappingURL=advancedBarrel.d.ts.map
|
|
83
|
+
//# sourceMappingURL=advancedBarrel.d.ts.map
|
package/dist.commonjs/barrel.js
CHANGED
|
@@ -1,18 +1,34 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding =
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
2
|
+
var __createBinding =
|
|
3
|
+
(this && this.__createBinding) ||
|
|
4
|
+
(Object.create
|
|
5
|
+
? function (o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (
|
|
9
|
+
!desc ||
|
|
10
|
+
("get" in desc ? !m.__esModule : desc.writable || desc.configurable)
|
|
11
|
+
) {
|
|
12
|
+
desc = {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return m[k];
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(o, k2, desc);
|
|
20
|
+
}
|
|
21
|
+
: function (o, m, k, k2) {
|
|
22
|
+
if (k2 === undefined) k2 = k;
|
|
23
|
+
o[k2] = m[k];
|
|
24
|
+
});
|
|
25
|
+
var __exportStar =
|
|
26
|
+
(this && this.__exportStar) ||
|
|
27
|
+
function (m, exports) {
|
|
28
|
+
for (var p in m)
|
|
29
|
+
if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p))
|
|
30
|
+
__createBinding(exports, m, p);
|
|
31
|
+
};
|
|
16
32
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
33
|
__exportStar(require("./signer"), exports);
|
|
18
34
|
__exportStar(require("./signersFactory"), exports);
|
package/dist.commonjs/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * as UniSat from "./barrel";
|
|
2
|
-
//# sourceMappingURL=index.d.ts.map
|
|
2
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist.commonjs/index.js
CHANGED
|
@@ -1,27 +1,48 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding =
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
2
|
+
var __createBinding =
|
|
3
|
+
(this && this.__createBinding) ||
|
|
4
|
+
(Object.create
|
|
5
|
+
? function (o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (
|
|
9
|
+
!desc ||
|
|
10
|
+
("get" in desc ? !m.__esModule : desc.writable || desc.configurable)
|
|
11
|
+
) {
|
|
12
|
+
desc = {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return m[k];
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(o, k2, desc);
|
|
20
|
+
}
|
|
21
|
+
: function (o, m, k, k2) {
|
|
22
|
+
if (k2 === undefined) k2 = k;
|
|
23
|
+
o[k2] = m[k];
|
|
24
|
+
});
|
|
25
|
+
var __setModuleDefault =
|
|
26
|
+
(this && this.__setModuleDefault) ||
|
|
27
|
+
(Object.create
|
|
28
|
+
? function (o, v) {
|
|
29
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
30
|
+
}
|
|
31
|
+
: function (o, v) {
|
|
32
|
+
o["default"] = v;
|
|
33
|
+
});
|
|
34
|
+
var __importStar =
|
|
35
|
+
(this && this.__importStar) ||
|
|
36
|
+
function (mod) {
|
|
19
37
|
if (mod && mod.__esModule) return mod;
|
|
20
38
|
var result = {};
|
|
21
|
-
if (mod != null)
|
|
39
|
+
if (mod != null)
|
|
40
|
+
for (var k in mod)
|
|
41
|
+
if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
|
|
42
|
+
__createBinding(result, mod, k);
|
|
22
43
|
__setModuleDefault(result, mod);
|
|
23
44
|
return result;
|
|
24
|
-
};
|
|
45
|
+
};
|
|
25
46
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
47
|
exports.UniSat = void 0;
|
|
27
48
|
exports.UniSat = __importStar(require("./barrel"));
|
|
@@ -6,44 +6,49 @@ import { Provider } from "./advancedBarrel";
|
|
|
6
6
|
* @extends {ccc.SignerBtc}
|
|
7
7
|
*/
|
|
8
8
|
export declare class Signer extends ccc.SignerBtc {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
9
|
+
readonly provider: Provider;
|
|
10
|
+
private readonly preferredNetworks;
|
|
11
|
+
/**
|
|
12
|
+
* Creates an instance of Signer.
|
|
13
|
+
* @param {ccc.Client} client - The client instance.
|
|
14
|
+
* @param {Provider} provider - The provider instance.
|
|
15
|
+
*/
|
|
16
|
+
constructor(
|
|
17
|
+
client: ccc.Client,
|
|
18
|
+
provider: Provider,
|
|
19
|
+
preferredNetworks?: ccc.NetworkPreference[],
|
|
20
|
+
);
|
|
21
|
+
_getNetworkToChange(): Promise<string | undefined>;
|
|
22
|
+
/**
|
|
23
|
+
* Ensure the BTC network is the same as CKB network.
|
|
24
|
+
*/
|
|
25
|
+
ensureNetwork(): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Gets the Bitcoin account address.
|
|
28
|
+
* @returns {Promise<string>} A promise that resolves to the Bitcoin account address.
|
|
29
|
+
*/
|
|
30
|
+
getBtcAccount(): Promise<string>;
|
|
31
|
+
/**
|
|
32
|
+
* Gets the Bitcoin public key.
|
|
33
|
+
* @returns {Promise<ccc.Hex>} A promise that resolves to the Bitcoin public key.
|
|
34
|
+
*/
|
|
35
|
+
getBtcPublicKey(): Promise<ccc.Hex>;
|
|
36
|
+
/**
|
|
37
|
+
* Connects to the provider by requesting accounts.
|
|
38
|
+
* @returns {Promise<void>} A promise that resolves when the connection is established.
|
|
39
|
+
*/
|
|
40
|
+
connect(): Promise<void>;
|
|
41
|
+
onReplaced(listener: () => void): () => void;
|
|
42
|
+
/**
|
|
43
|
+
* Checks if the signer is connected.
|
|
44
|
+
* @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
|
|
45
|
+
*/
|
|
46
|
+
isConnected(): Promise<boolean>;
|
|
47
|
+
/**
|
|
48
|
+
* Signs a raw message with the Bitcoin account.
|
|
49
|
+
* @param {string | ccc.BytesLike} message - The message to sign.
|
|
50
|
+
* @returns {Promise<string>} A promise that resolves to the signed message.
|
|
51
|
+
*/
|
|
52
|
+
signMessageRaw(message: string | ccc.BytesLike): Promise<string>;
|
|
48
53
|
}
|
|
49
|
-
//# sourceMappingURL=signer.d.ts.map
|
|
54
|
+
//# sourceMappingURL=signer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C;;;;GAIG;AACH,qBAAa,MAAO,SAAQ,GAAG,CAAC,SAAS;aAQrB,QAAQ,EAAE,QAAQ;IAClC,OAAO,CAAC,QAAQ,CAAC,iBAAiB;IARpC;;;;OAIG;gBAED,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,QAAQ,EAAE,QAAQ,EACjB,iBAAiB,GAAE,GAAG,CAAC,iBAAiB,EAWxD;
|
|
1
|
+
{"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C;;;;GAIG;AACH,qBAAa,MAAO,SAAQ,GAAG,CAAC,SAAS;aAQrB,QAAQ,EAAE,QAAQ;IAClC,OAAO,CAAC,QAAQ,CAAC,iBAAiB;IARpC;;;;OAIG;gBAED,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,QAAQ,EAAE,QAAQ,EACjB,iBAAiB,GAAE,GAAG,CAAC,iBAAiB,EAWxD;IAKG,mBAAmB,IAAI,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IA0BxD;;OAEG;IACG,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IA2BpC;;;OAGG;IACG,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;IAItC;;;OAGG;IACG,eAAe,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAIzC;;;OAGG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAK9B,UAAU,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI;IAiB5C;;;OAGG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAOrC;;;;OAIG;IACG,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;CAMvE"}
|
package/dist.commonjs/signer.js
CHANGED
|
@@ -8,114 +8,140 @@ const core_1 = require("@ckb-ccc/core");
|
|
|
8
8
|
* @extends {ccc.SignerBtc}
|
|
9
9
|
*/
|
|
10
10
|
class Signer extends core_1.ccc.SignerBtc {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Creates an instance of Signer.
|
|
13
|
+
* @param {ccc.Client} client - The client instance.
|
|
14
|
+
* @param {Provider} provider - The provider instance.
|
|
15
|
+
*/
|
|
16
|
+
constructor(
|
|
17
|
+
client,
|
|
18
|
+
provider,
|
|
19
|
+
preferredNetworks = [
|
|
20
|
+
{
|
|
21
|
+
addressPrefix: "ckb",
|
|
22
|
+
signerType: core_1.ccc.SignerType.BTC,
|
|
23
|
+
network: "btc",
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
addressPrefix: "ckt",
|
|
27
|
+
signerType: core_1.ccc.SignerType.BTC,
|
|
28
|
+
network: "btcTestnet",
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
) {
|
|
32
|
+
super(client);
|
|
33
|
+
this.provider = provider;
|
|
34
|
+
this.preferredNetworks = preferredNetworks;
|
|
35
|
+
}
|
|
36
|
+
async _getNetworkToChange() {
|
|
37
|
+
const currentNetwork = await (async () => {
|
|
38
|
+
if (this.provider.getChain) {
|
|
39
|
+
return (
|
|
40
|
+
{
|
|
41
|
+
BITCOIN_MAINNET: "btc",
|
|
42
|
+
BITCOIN_TESTNET: "btcTestnet",
|
|
43
|
+
FRACTAL_BITCOIN_MAINNET: "fractalBtc",
|
|
44
|
+
}[(await this.provider.getChain()).enum] ?? ""
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
return (await this.provider.getNetwork()) === "livenet"
|
|
48
|
+
? "btc"
|
|
49
|
+
: "btcTestnet";
|
|
50
|
+
})();
|
|
51
|
+
const { network } = this.matchNetworkPreference(
|
|
52
|
+
this.preferredNetworks,
|
|
53
|
+
currentNetwork,
|
|
54
|
+
) ?? { network: currentNetwork };
|
|
55
|
+
if (network === currentNetwork) {
|
|
56
|
+
return;
|
|
31
57
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
FRACTAL_BITCOIN_MAINNET: "fractalBtc",
|
|
42
|
-
}[(await this.provider.getChain()).enum] ?? "");
|
|
43
|
-
}
|
|
44
|
-
return (await this.provider.getNetwork()) === "livenet"
|
|
45
|
-
? "btc"
|
|
46
|
-
: "btcTestnet";
|
|
47
|
-
})();
|
|
48
|
-
const { network } = this.matchNetworkPreference(this.preferredNetworks, currentNetwork) ?? { network: currentNetwork };
|
|
49
|
-
if (network === currentNetwork) {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
if (this.provider.switchChain) {
|
|
53
|
-
const chain = {
|
|
54
|
-
btc: "BITCOIN_MAINNET",
|
|
55
|
-
btcTestnet: "BITCOIN_TESTNET",
|
|
56
|
-
fractalBtc: "FRACTAL_BITCOIN_MAINNET",
|
|
57
|
-
}[network];
|
|
58
|
-
if (chain) {
|
|
59
|
-
await this.provider.switchChain(chain);
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
else if (network === "btc" || network === "btcTestnet") {
|
|
64
|
-
await this.provider.switchNetwork(network === "btc" ? "livenet" : "testnet");
|
|
65
|
-
}
|
|
66
|
-
throw new Error(`UniSat wallet doesn't support the requested chain ${network}`);
|
|
58
|
+
return network;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Ensure the BTC network is the same as CKB network.
|
|
62
|
+
*/
|
|
63
|
+
async ensureNetwork() {
|
|
64
|
+
const network = await this._getNetworkToChange();
|
|
65
|
+
if (!network) {
|
|
66
|
+
return;
|
|
67
67
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
68
|
+
if (this.provider.switchChain) {
|
|
69
|
+
const chain = {
|
|
70
|
+
btc: "BITCOIN_MAINNET",
|
|
71
|
+
btcTestnet: "BITCOIN_TESTNET",
|
|
72
|
+
fractalBtc: "FRACTAL_BITCOIN_MAINNET",
|
|
73
|
+
}[network];
|
|
74
|
+
if (chain) {
|
|
75
|
+
await this.provider.switchChain(chain);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
} else if (network === "btc" || network === "btcTestnet") {
|
|
79
|
+
await this.provider.switchNetwork(
|
|
80
|
+
network === "btc" ? "livenet" : "testnet",
|
|
81
|
+
);
|
|
82
|
+
return;
|
|
74
83
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
84
|
+
throw new Error(
|
|
85
|
+
`UniSat wallet doesn't support the requested chain ${network}`,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Gets the Bitcoin account address.
|
|
90
|
+
* @returns {Promise<string>} A promise that resolves to the Bitcoin account address.
|
|
91
|
+
*/
|
|
92
|
+
async getBtcAccount() {
|
|
93
|
+
return (await this.provider.getAccounts())[0];
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Gets the Bitcoin public key.
|
|
97
|
+
* @returns {Promise<ccc.Hex>} A promise that resolves to the Bitcoin public key.
|
|
98
|
+
*/
|
|
99
|
+
async getBtcPublicKey() {
|
|
100
|
+
return core_1.ccc.hexFrom(await this.provider.getPublicKey());
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Connects to the provider by requesting accounts.
|
|
104
|
+
* @returns {Promise<void>} A promise that resolves when the connection is established.
|
|
105
|
+
*/
|
|
106
|
+
async connect() {
|
|
107
|
+
await this.provider.requestAccounts();
|
|
108
|
+
await this.ensureNetwork();
|
|
109
|
+
}
|
|
110
|
+
onReplaced(listener) {
|
|
111
|
+
const stop = [];
|
|
112
|
+
const replacer = async () => {
|
|
113
|
+
listener();
|
|
114
|
+
stop[0]?.();
|
|
115
|
+
};
|
|
116
|
+
stop.push(() => {
|
|
117
|
+
this.provider.removeListener("accountsChanged", replacer);
|
|
118
|
+
this.provider.removeListener("networkChanged", replacer);
|
|
119
|
+
});
|
|
120
|
+
this.provider.on("accountsChanged", replacer);
|
|
121
|
+
this.provider.on("networkChanged", replacer);
|
|
122
|
+
return stop[0];
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Checks if the signer is connected.
|
|
126
|
+
* @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
|
|
127
|
+
*/
|
|
128
|
+
async isConnected() {
|
|
129
|
+
if (await this._getNetworkToChange()) {
|
|
130
|
+
return false;
|
|
119
131
|
}
|
|
132
|
+
return (await this.provider.getAccounts()).length !== 0;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Signs a raw message with the Bitcoin account.
|
|
136
|
+
* @param {string | ccc.BytesLike} message - The message to sign.
|
|
137
|
+
* @returns {Promise<string>} A promise that resolves to the signed message.
|
|
138
|
+
*/
|
|
139
|
+
async signMessageRaw(message) {
|
|
140
|
+
const challenge =
|
|
141
|
+
typeof message === "string"
|
|
142
|
+
? message
|
|
143
|
+
: core_1.ccc.hexFrom(message).slice(2);
|
|
144
|
+
return this.provider.signMessage(challenge, "ecdsa");
|
|
145
|
+
}
|
|
120
146
|
}
|
|
121
147
|
exports.Signer = Signer;
|
|
@@ -5,5 +5,8 @@ import { Signer } from "./signer";
|
|
|
5
5
|
* @param {ccc.Client} client - The client instance.
|
|
6
6
|
* @returns {Signer | undefined} The Signer instance if the UniSat provider is available, otherwise undefined.
|
|
7
7
|
*/
|
|
8
|
-
export declare function getUniSatSigner(
|
|
9
|
-
|
|
8
|
+
export declare function getUniSatSigner(
|
|
9
|
+
client: ccc.Client,
|
|
10
|
+
preferredNetworks: ccc.NetworkPreference[],
|
|
11
|
+
): Signer | undefined;
|
|
12
|
+
//# sourceMappingURL=signersFactory.d.ts.map
|
|
@@ -8,10 +8,10 @@ const signer_1 = require("./signer");
|
|
|
8
8
|
* @returns {Signer | undefined} The Signer instance if the UniSat provider is available, otherwise undefined.
|
|
9
9
|
*/
|
|
10
10
|
function getUniSatSigner(client, preferredNetworks) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
const windowRef = window;
|
|
12
|
+
if (typeof windowRef.unisat === "undefined") {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
return new signer_1.Signer(client, windowRef.unisat, preferredNetworks);
|
|
16
16
|
}
|
|
17
17
|
exports.getUniSatSigner = getUniSatSigner;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ckb-ccc/uni-sat",
|
|
3
|
-
"version": "0.0.10-alpha.
|
|
3
|
+
"version": "0.0.10-alpha.4",
|
|
4
4
|
"description": "CCC - CKBer's Codebase. Common Chains Connector's support for UniSat",
|
|
5
5
|
"author": "Hanssen0 <hanssen0@hanssen0.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"access": "public"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@ckb-ccc/core": "0.0.10-alpha.
|
|
53
|
+
"@ckb-ccc/core": "0.0.10-alpha.4"
|
|
54
54
|
},
|
|
55
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "61228337bbf4bdc0bc082e3c9530f843c2182106"
|
|
56
56
|
}
|
package/src/signer.ts
CHANGED
|
@@ -31,10 +31,7 @@ export class Signer extends ccc.SignerBtc {
|
|
|
31
31
|
super(client);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
* Ensure the BTC network is the same as CKB network.
|
|
36
|
-
*/
|
|
37
|
-
async ensureNetwork(): Promise<void> {
|
|
34
|
+
async _getNetworkToChange(): Promise<string | undefined> {
|
|
38
35
|
const currentNetwork = await (async () => {
|
|
39
36
|
if (this.provider.getChain) {
|
|
40
37
|
return (
|
|
@@ -56,6 +53,18 @@ export class Signer extends ccc.SignerBtc {
|
|
|
56
53
|
if (network === currentNetwork) {
|
|
57
54
|
return;
|
|
58
55
|
}
|
|
56
|
+
|
|
57
|
+
return network;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Ensure the BTC network is the same as CKB network.
|
|
62
|
+
*/
|
|
63
|
+
async ensureNetwork(): Promise<void> {
|
|
64
|
+
const network = await this._getNetworkToChange();
|
|
65
|
+
if (!network) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
59
68
|
if (this.provider.switchChain) {
|
|
60
69
|
const chain = {
|
|
61
70
|
btc: "BITCOIN_MAINNET",
|
|
@@ -70,7 +79,9 @@ export class Signer extends ccc.SignerBtc {
|
|
|
70
79
|
await this.provider.switchNetwork(
|
|
71
80
|
network === "btc" ? "livenet" : "testnet",
|
|
72
81
|
);
|
|
82
|
+
return;
|
|
73
83
|
}
|
|
84
|
+
|
|
74
85
|
throw new Error(
|
|
75
86
|
`UniSat wallet doesn't support the requested chain ${network}`,
|
|
76
87
|
);
|
|
@@ -123,6 +134,9 @@ export class Signer extends ccc.SignerBtc {
|
|
|
123
134
|
* @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
|
|
124
135
|
*/
|
|
125
136
|
async isConnected(): Promise<boolean> {
|
|
137
|
+
if (await this._getNetworkToChange()) {
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
126
140
|
return (await this.provider.getAccounts()).length !== 0;
|
|
127
141
|
}
|
|
128
142
|
|