@ckb-ccc/uni-sat 0.0.8-alpha.3 → 0.0.9-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.
@@ -8,6 +8,32 @@ export interface Provider {
8
8
  * @returns {Promise<string[]>} A promise that resolves to an array of account addresses.
9
9
  */
10
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
+ }>;
11
37
  /**
12
38
  * Gets the current accounts.
13
39
  * @returns {Promise<string[]>} A promise that resolves to an array of account addresses.
@@ -1 +1 @@
1
- {"version":3,"file":"advancedBarrel.d.ts","sourceRoot":"","sources":["../src/advancedBarrel.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,QAAQ;IACvB;;;OAGG;IACH,eAAe,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAErC;;;OAGG;IACH,WAAW,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAEjC;;;OAGG;IACH,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAEhC;;;;;OAKG;IACH,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAE3E;;;OAGG;IACH,EAAE,EAAE,QAAQ,CAAC;IAEb;;;;;OAKG;IACH,cAAc,CACZ,SAAS,EAAE,MAAM,EACjB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G;IACH,EAAE,EAAE,QAAQ,CAAC;IAEb;;;;;OAKG;IACH,cAAc,CACZ,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,GACxC,QAAQ,CAAC;CACb;AAED;;;GAGG;AACH,MAAM,WAAW,QAAQ;IACvB;;;;;OAKG;IACH,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,GAAG,QAAQ,CAAC;CAC1E"}
package/dist/signer.d.ts CHANGED
@@ -7,12 +7,17 @@ import { Provider } from "./advancedBarrel";
7
7
  */
8
8
  export declare class Signer extends ccc.SignerBtc {
9
9
  readonly provider: Provider;
10
+ private readonly preferredNetworks;
10
11
  /**
11
12
  * Creates an instance of Signer.
12
13
  * @param {ccc.Client} client - The client instance.
13
14
  * @param {Provider} provider - The provider instance.
14
15
  */
15
- constructor(client: ccc.Client, provider: Provider);
16
+ constructor(client: ccc.Client, provider: Provider, preferredNetworks: ccc.NetworkPreference[]);
17
+ /**
18
+ * Ensure the BTC network is the same as CKB network.
19
+ */
20
+ ensureNetwork(): Promise<void>;
16
21
  /**
17
22
  * Gets the Bitcoin account address.
18
23
  * @returns {Promise<string>} A promise that resolves to the Bitcoin account address.
@@ -28,6 +33,7 @@ export declare class Signer extends ccc.SignerBtc {
28
33
  * @returns {Promise<void>} A promise that resolves when the connection is established.
29
34
  */
30
35
  connect(): Promise<void>;
36
+ replaceClient(client: ccc.Client): Promise<boolean>;
31
37
  /**
32
38
  * Checks if the signer is connected.
33
39
  * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
@@ -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;A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O,CAAC,OAAO,CAAC;IAQzD;;;OAGG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAIrC;;;;OAIG;IACG,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;CAMvE"}
package/dist/signer.js CHANGED
@@ -10,9 +10,46 @@ export class Signer extends ccc.SignerBtc {
10
10
  * @param {ccc.Client} client - The client instance.
11
11
  * @param {Provider} provider - The provider instance.
12
12
  */
13
- constructor(client, provider) {
13
+ constructor(client, provider, preferredNetworks) {
14
14
  super(client);
15
15
  this.provider = provider;
16
+ this.preferredNetworks = preferredNetworks;
17
+ }
18
+ /**
19
+ * Ensure the BTC network is the same as CKB network.
20
+ */
21
+ async ensureNetwork() {
22
+ const currentNetwork = await (async () => {
23
+ if (this.provider.getChain) {
24
+ return ({
25
+ BITCOIN_MAINNET: "btc",
26
+ BITCOIN_TESTNET: "btcTestnet",
27
+ FRACTAL_BITCOIN_MAINNET: "fractalBtc",
28
+ }[(await this.provider.getChain()).enum] ?? "");
29
+ }
30
+ return (await this.provider.getNetwork()) === "livenet"
31
+ ? "btc"
32
+ : "btcTestnet";
33
+ })();
34
+ const { network } = this.matchNetworkPreference(this.preferredNetworks, currentNetwork) ?? { network: currentNetwork };
35
+ if (network === currentNetwork) {
36
+ return;
37
+ }
38
+ if (this.provider.switchChain) {
39
+ const chain = {
40
+ btc: "BITCOIN_MAINNET",
41
+ btcTestnet: "BITCOIN_TESTNET",
42
+ fractalBtc: "FRACTAL_BITCOIN_MAINNET",
43
+ }[network];
44
+ if (chain) {
45
+ await this.provider.switchChain(chain);
46
+ return;
47
+ }
48
+ }
49
+ else if (network === "btc" || network === "btcTestnet") {
50
+ await this.provider.switchNetwork(network === "btc" ? "livenet" : "testnet");
51
+ }
52
+ throw new Error(`UniSat wallet doesn't support the requested chain ${network}`);
16
53
  }
17
54
  /**
18
55
  * Gets the Bitcoin account address.
@@ -34,6 +71,14 @@ export class Signer extends ccc.SignerBtc {
34
71
  */
35
72
  async connect() {
36
73
  await this.provider.requestAccounts();
74
+ await this.ensureNetwork();
75
+ }
76
+ async replaceClient(client) {
77
+ if (await super.replaceClient(client)) {
78
+ await this.ensureNetwork();
79
+ return true;
80
+ }
81
+ return false;
37
82
  }
38
83
  /**
39
84
  * Checks if the signer is connected.
@@ -5,5 +5,5 @@ 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(client: ccc.Client): Signer | undefined;
8
+ export declare function getUniSatSigner(client: ccc.Client, preferredNetworks: ccc.NetworkPreference[]): Signer | undefined;
9
9
  //# sourceMappingURL=signersFactory.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"signersFactory.d.ts","sourceRoot":"","sources":["../src/signersFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAEpC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,GAAG,MAAM,GAAG,SAAS,CAQtE"}
1
+ {"version":3,"file":"signersFactory.d.ts","sourceRoot":"","sources":["../src/signersFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAEpC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC;;;;GAIG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,GAAG,CAAC,MAAM,EAClB,iBAAiB,EAAE,GAAG,CAAC,iBAAiB,EAAE,GACzC,MAAM,GAAG,SAAS,CAQpB"}
@@ -4,10 +4,10 @@ import { Signer } from "./signer";
4
4
  * @param {ccc.Client} client - The client instance.
5
5
  * @returns {Signer | undefined} The Signer instance if the UniSat provider is available, otherwise undefined.
6
6
  */
7
- export function getUniSatSigner(client) {
7
+ export function getUniSatSigner(client, preferredNetworks) {
8
8
  const windowRef = window;
9
9
  if (typeof windowRef.unisat === "undefined") {
10
10
  return undefined;
11
11
  }
12
- return new Signer(client, windowRef.unisat);
12
+ return new Signer(client, windowRef.unisat, preferredNetworks);
13
13
  }
@@ -1,2 +1,2 @@
1
1
  export * as UniSatA from "./advancedBarrel";
2
- //# sourceMappingURL=advanced.d.ts.map
2
+ //# sourceMappingURL=advanced.d.ts.map
@@ -1,48 +1,27 @@
1
1
  "use strict";
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) {
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
37
19
  if (mod && mod.__esModule) return mod;
38
20
  var result = {};
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);
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
43
22
  __setModuleDefault(result, mod);
44
23
  return result;
45
- };
24
+ };
46
25
  Object.defineProperty(exports, "__esModule", { value: true });
47
26
  exports.UniSatA = void 0;
48
27
  exports.UniSatA = __importStar(require("./advancedBarrel"));
@@ -3,55 +3,78 @@
3
3
  * @interface
4
4
  */
5
5
  export interface Provider {
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 accounts.
13
- * @returns {Promise<string[]>} A promise that resolves to an array of account addresses.
14
- */
15
- getAccounts(): Promise<string[]>;
16
- /**
17
- * Gets the public key of the account.
18
- * @returns {Promise<string>} A promise that resolves to the public key.
19
- */
20
- getPublicKey(): Promise<string>;
21
- /**
22
- * Signs a message with the specified type.
23
- * @param {string} msg - The message to sign.
24
- * @param {"ecdsa" | "bip322-simple"} type - The type of signature.
25
- * @returns {Promise<string>} A promise that resolves to the signed message.
26
- */
27
- signMessage(msg: string, type: "ecdsa" | "bip322-simple"): Promise<string>;
28
- /**
29
- * Adds an event listener to the provider.
30
- * @type {OnMethod}
31
- */
32
- on: OnMethod;
33
- /**
34
- * Removes an event listener from the provider.
35
- * @param {string} eventName - The name of the event to remove the listener from.
36
- * @param {(...args: unknown[]) => unknown} listener - The listener function to remove.
37
- * @returns {Provider} The provider instance.
38
- */
39
- removeListener(
40
- eventName: string,
41
- listener: (...args: unknown[]) => unknown,
42
- ): Provider;
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(eventName: string, listener: (...args: unknown[]) => unknown): Provider;
43
66
  }
44
67
  /**
45
68
  * Interface representing a method to add event listeners to the provider.
46
69
  * @interface
47
70
  */
48
71
  export interface OnMethod {
49
- /**
50
- * Adds an event listener to the provider.
51
- * @param {string} eventName - The name of the event.
52
- * @param {(...args: unknown[]) => unknown} listener - The listener function.
53
- * @returns {Provider} The provider instance.
54
- */
55
- (eventName: string, listener: (...args: unknown[]) => unknown): Provider;
72
+ /**
73
+ * Adds an event listener to the provider.
74
+ * @param {string} eventName - The name of the event.
75
+ * @param {(...args: unknown[]) => unknown} listener - The listener function.
76
+ * @returns {Provider} The provider instance.
77
+ */
78
+ (eventName: string, listener: (...args: unknown[]) => unknown): Provider;
56
79
  }
57
- //# sourceMappingURL=advancedBarrel.d.ts.map
80
+ //# sourceMappingURL=advancedBarrel.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"advancedBarrel.d.ts","sourceRoot":"","sources":["../src/advancedBarrel.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,QAAQ;IACvB;;;OAGG;IACH,eAAe,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAErC;;;OAGG;IACH,WAAW,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAEjC;;;OAGG;IACH,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAEhC;;;;;OAKG;IACH,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAE3E;;;OAGG;IACH,EAAE,EAAE,QAAQ,CAAC;IAEb;;;;;OAKG;IACH,cAAc,CACZ,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,GACxC,QAAQ,CAAC;CACb;AAED;;;GAGG;AACH,MAAM,WAAW,QAAQ;IACvB;;;;;OAKG;IACH,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,GAAG,QAAQ,CAAC;CAC1E"}
1
+ {"version":3,"file":"advancedBarrel.d.ts","source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OAAO,EAAE,KAAK,OAAO,GAAG,QAAQ,CAAC;CAC1E"}
@@ -1,3 +1,3 @@
1
1
  export * from "./signer";
2
2
  export * from "./signersFactory";
3
- //# sourceMappingURL=barrel.d.ts.map
3
+ //# sourceMappingURL=barrel.d.ts.map
@@ -1,34 +1,18 @@
1
1
  "use strict";
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
- };
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
32
16
  Object.defineProperty(exports, "__esModule", { value: true });
33
17
  __exportStar(require("./signer"), exports);
34
18
  __exportStar(require("./signersFactory"), exports);
@@ -1,2 +1,2 @@
1
1
  export * as UniSat from "./barrel";
2
- //# sourceMappingURL=index.d.ts.map
2
+ //# sourceMappingURL=index.d.ts.map
@@ -1,48 +1,27 @@
1
1
  "use strict";
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) {
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
37
19
  if (mod && mod.__esModule) return mod;
38
20
  var result = {};
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);
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
43
22
  __setModuleDefault(result, mod);
44
23
  return result;
45
- };
24
+ };
46
25
  Object.defineProperty(exports, "__esModule", { value: true });
47
26
  exports.UniSat = void 0;
48
27
  exports.UniSat = __importStar(require("./barrel"));
@@ -6,38 +6,44 @@ import { Provider } from "./advancedBarrel";
6
6
  * @extends {ccc.SignerBtc}
7
7
  */
8
8
  export declare class Signer extends ccc.SignerBtc {
9
- readonly provider: Provider;
10
- /**
11
- * Creates an instance of Signer.
12
- * @param {ccc.Client} client - The client instance.
13
- * @param {Provider} provider - The provider instance.
14
- */
15
- constructor(client: ccc.Client, provider: Provider);
16
- /**
17
- * Gets the Bitcoin account address.
18
- * @returns {Promise<string>} A promise that resolves to the Bitcoin account address.
19
- */
20
- getBtcAccount(): Promise<string>;
21
- /**
22
- * Gets the Bitcoin public key.
23
- * @returns {Promise<ccc.Hex>} A promise that resolves to the Bitcoin public key.
24
- */
25
- getBtcPublicKey(): Promise<ccc.Hex>;
26
- /**
27
- * Connects to the provider by requesting accounts.
28
- * @returns {Promise<void>} A promise that resolves when the connection is established.
29
- */
30
- connect(): Promise<void>;
31
- /**
32
- * Checks if the signer is connected.
33
- * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
34
- */
35
- isConnected(): Promise<boolean>;
36
- /**
37
- * Signs a raw message with the Bitcoin account.
38
- * @param {string | ccc.BytesLike} message - The message to sign.
39
- * @returns {Promise<string>} A promise that resolves to the signed message.
40
- */
41
- signMessageRaw(message: string | ccc.BytesLike): Promise<string>;
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(client: ccc.Client, provider: Provider, preferredNetworks: ccc.NetworkPreference[]);
17
+ /**
18
+ * Ensure the BTC network is the same as CKB network.
19
+ */
20
+ ensureNetwork(): Promise<void>;
21
+ /**
22
+ * Gets the Bitcoin account address.
23
+ * @returns {Promise<string>} A promise that resolves to the Bitcoin account address.
24
+ */
25
+ getBtcAccount(): Promise<string>;
26
+ /**
27
+ * Gets the Bitcoin public key.
28
+ * @returns {Promise<ccc.Hex>} A promise that resolves to the Bitcoin public key.
29
+ */
30
+ getBtcPublicKey(): Promise<ccc.Hex>;
31
+ /**
32
+ * Connects to the provider by requesting accounts.
33
+ * @returns {Promise<void>} A promise that resolves when the connection is established.
34
+ */
35
+ connect(): Promise<void>;
36
+ replaceClient(client: ccc.Client): Promise<boolean>;
37
+ /**
38
+ * Checks if the signer is connected.
39
+ * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
40
+ */
41
+ isConnected(): Promise<boolean>;
42
+ /**
43
+ * Signs a raw message with the Bitcoin account.
44
+ * @param {string | ccc.BytesLike} message - The message to sign.
45
+ * @returns {Promise<string>} A promise that resolves to the signed message.
46
+ */
47
+ signMessageRaw(message: string | ccc.BytesLike): Promise<string>;
42
48
  }
43
- //# sourceMappingURL=signer.d.ts.map
49
+ //# 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;IAPpC;;;;OAIG;gBAED,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,QAAQ,EAAE,QAAQ;IAKpC;;;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;IAI9B;;;OAGG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAIrC;;;;OAIG;IACG,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;CAMvE"}
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,EAAE,GAAG,CAAC,iBAAiB,EAAE;IAK7D;;OAEG;IACG,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IA0CpC;;;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;IAKxB,aAAa,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAQzD;;;OAGG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAIrC;;;;OAIG;IACG,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;CAMvE"}
@@ -8,54 +8,96 @@ 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
- * Creates an instance of Signer.
13
- * @param {ccc.Client} client - The client instance.
14
- * @param {Provider} provider - The provider instance.
15
- */
16
- constructor(client, provider) {
17
- super(client);
18
- this.provider = provider;
19
- }
20
- /**
21
- * Gets the Bitcoin account address.
22
- * @returns {Promise<string>} A promise that resolves to the Bitcoin account address.
23
- */
24
- async getBtcAccount() {
25
- return (await this.provider.getAccounts())[0];
26
- }
27
- /**
28
- * Gets the Bitcoin public key.
29
- * @returns {Promise<ccc.Hex>} A promise that resolves to the Bitcoin public key.
30
- */
31
- async getBtcPublicKey() {
32
- return core_1.ccc.hexFrom(await this.provider.getPublicKey());
33
- }
34
- /**
35
- * Connects to the provider by requesting accounts.
36
- * @returns {Promise<void>} A promise that resolves when the connection is established.
37
- */
38
- async connect() {
39
- await this.provider.requestAccounts();
40
- }
41
- /**
42
- * Checks if the signer is connected.
43
- * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
44
- */
45
- async isConnected() {
46
- return (await this.provider.getAccounts()).length !== 0;
47
- }
48
- /**
49
- * Signs a raw message with the Bitcoin account.
50
- * @param {string | ccc.BytesLike} message - The message to sign.
51
- * @returns {Promise<string>} A promise that resolves to the signed message.
52
- */
53
- async signMessageRaw(message) {
54
- const challenge =
55
- typeof message === "string"
56
- ? message
57
- : core_1.ccc.hexFrom(message).slice(2);
58
- return this.provider.signMessage(challenge, "ecdsa");
59
- }
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(client, provider, preferredNetworks) {
17
+ super(client);
18
+ this.provider = provider;
19
+ this.preferredNetworks = preferredNetworks;
20
+ }
21
+ /**
22
+ * Ensure the BTC network is the same as CKB network.
23
+ */
24
+ async ensureNetwork() {
25
+ const currentNetwork = await (async () => {
26
+ if (this.provider.getChain) {
27
+ return ({
28
+ BITCOIN_MAINNET: "btc",
29
+ BITCOIN_TESTNET: "btcTestnet",
30
+ FRACTAL_BITCOIN_MAINNET: "fractalBtc",
31
+ }[(await this.provider.getChain()).enum] ?? "");
32
+ }
33
+ return (await this.provider.getNetwork()) === "livenet"
34
+ ? "btc"
35
+ : "btcTestnet";
36
+ })();
37
+ const { network } = this.matchNetworkPreference(this.preferredNetworks, currentNetwork) ?? { network: currentNetwork };
38
+ if (network === currentNetwork) {
39
+ return;
40
+ }
41
+ if (this.provider.switchChain) {
42
+ const chain = {
43
+ btc: "BITCOIN_MAINNET",
44
+ btcTestnet: "BITCOIN_TESTNET",
45
+ fractalBtc: "FRACTAL_BITCOIN_MAINNET",
46
+ }[network];
47
+ if (chain) {
48
+ await this.provider.switchChain(chain);
49
+ return;
50
+ }
51
+ }
52
+ else if (network === "btc" || network === "btcTestnet") {
53
+ await this.provider.switchNetwork(network === "btc" ? "livenet" : "testnet");
54
+ }
55
+ throw new Error(`UniSat wallet doesn't support the requested chain ${network}`);
56
+ }
57
+ /**
58
+ * Gets the Bitcoin account address.
59
+ * @returns {Promise<string>} A promise that resolves to the Bitcoin account address.
60
+ */
61
+ async getBtcAccount() {
62
+ return (await this.provider.getAccounts())[0];
63
+ }
64
+ /**
65
+ * Gets the Bitcoin public key.
66
+ * @returns {Promise<ccc.Hex>} A promise that resolves to the Bitcoin public key.
67
+ */
68
+ async getBtcPublicKey() {
69
+ return core_1.ccc.hexFrom(await this.provider.getPublicKey());
70
+ }
71
+ /**
72
+ * Connects to the provider by requesting accounts.
73
+ * @returns {Promise<void>} A promise that resolves when the connection is established.
74
+ */
75
+ async connect() {
76
+ await this.provider.requestAccounts();
77
+ await this.ensureNetwork();
78
+ }
79
+ async replaceClient(client) {
80
+ if (await super.replaceClient(client)) {
81
+ await this.ensureNetwork();
82
+ return true;
83
+ }
84
+ return false;
85
+ }
86
+ /**
87
+ * Checks if the signer is connected.
88
+ * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
89
+ */
90
+ async isConnected() {
91
+ return (await this.provider.getAccounts()).length !== 0;
92
+ }
93
+ /**
94
+ * Signs a raw message with the Bitcoin account.
95
+ * @param {string | ccc.BytesLike} message - The message to sign.
96
+ * @returns {Promise<string>} A promise that resolves to the signed message.
97
+ */
98
+ async signMessageRaw(message) {
99
+ const challenge = typeof message === "string" ? message : core_1.ccc.hexFrom(message).slice(2);
100
+ return this.provider.signMessage(challenge, "ecdsa");
101
+ }
60
102
  }
61
103
  exports.Signer = Signer;
@@ -5,5 +5,5 @@ 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(client: ccc.Client): Signer | undefined;
9
- //# sourceMappingURL=signersFactory.d.ts.map
8
+ export declare function getUniSatSigner(client: ccc.Client, preferredNetworks: ccc.NetworkPreference[]): Signer | undefined;
9
+ //# sourceMappingURL=signersFactory.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"signersFactory.d.ts","sourceRoot":"","sources":["../src/signersFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAEpC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,GAAG,MAAM,GAAG,SAAS,CAQtE"}
1
+ {"version":3,"file":"signersFactory.d.ts","sourceRoot":"","sources":["../src/signersFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAEpC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC;;;;GAIG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,GAAG,CAAC,MAAM,EAClB,iBAAiB,EAAE,GAAG,CAAC,iBAAiB,EAAE,GACzC,MAAM,GAAG,SAAS,CAQpB"}
@@ -7,11 +7,11 @@ const signer_1 = require("./signer");
7
7
  * @param {ccc.Client} client - The client instance.
8
8
  * @returns {Signer | undefined} The Signer instance if the UniSat provider is available, otherwise undefined.
9
9
  */
10
- function getUniSatSigner(client) {
11
- const windowRef = window;
12
- if (typeof windowRef.unisat === "undefined") {
13
- return undefined;
14
- }
15
- return new signer_1.Signer(client, windowRef.unisat);
10
+ function getUniSatSigner(client, preferredNetworks) {
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,7 +1,7 @@
1
1
  {
2
2
  "name": "@ckb-ccc/uni-sat",
3
- "version": "0.0.8-alpha.3",
4
- "description": "Common Chains Connector's support for UniSat",
3
+ "version": "0.0.9-alpha.0",
4
+ "description": "CCC - CKBer's Codebase. Common Chains Connector's support for UniSat",
5
5
  "author": "Hanssen0 <hanssen0@hanssen0.com>",
6
6
  "license": "MIT",
7
7
  "private": false,
@@ -50,7 +50,7 @@
50
50
  "access": "public"
51
51
  },
52
52
  "dependencies": {
53
- "@ckb-ccc/core": "0.0.8-alpha.3"
53
+ "@ckb-ccc/core": "0.0.9-alpha.0"
54
54
  },
55
- "gitHead": "bddfa3e5631252194dfde32b79ee589703f18afb"
55
+ "gitHead": "ac4435006da9c9e48b3b66fc329e6f539156b252"
56
56
  }
@@ -9,6 +9,30 @@ export interface Provider {
9
9
  */
10
10
  requestAccounts(): Promise<string[]>;
11
11
 
12
+ /**
13
+ * Gets the current network.
14
+ * @returns current network.
15
+ */
16
+ getNetwork(): Promise<"livenet" | "testnet">;
17
+
18
+ /**
19
+ * Switch the current network.
20
+ */
21
+ switchNetwork(chain: "livenet" | "testnet"): Promise<void>;
22
+
23
+ /**
24
+ * Gets the current chain.
25
+ * @returns current chain.
26
+ */
27
+ getChain?(): Promise<{ enum: string; name: string; network: string }>;
28
+
29
+ /**
30
+ * Switch the current chain.
31
+ */
32
+ switchChain?(
33
+ chain: string,
34
+ ): Promise<{ enum: string; name: string; network: string }>;
35
+
12
36
  /**
13
37
  * Gets the current accounts.
14
38
  * @returns {Promise<string[]>} A promise that resolves to an array of account addresses.
package/src/signer.ts CHANGED
@@ -15,10 +15,56 @@ export class Signer extends ccc.SignerBtc {
15
15
  constructor(
16
16
  client: ccc.Client,
17
17
  public readonly provider: Provider,
18
+ private readonly preferredNetworks: ccc.NetworkPreference[],
18
19
  ) {
19
20
  super(client);
20
21
  }
21
22
 
23
+ /**
24
+ * Ensure the BTC network is the same as CKB network.
25
+ */
26
+ async ensureNetwork(): Promise<void> {
27
+ const currentNetwork = await (async () => {
28
+ if (this.provider.getChain) {
29
+ return (
30
+ {
31
+ BITCOIN_MAINNET: "btc",
32
+ BITCOIN_TESTNET: "btcTestnet",
33
+ FRACTAL_BITCOIN_MAINNET: "fractalBtc",
34
+ }[(await this.provider.getChain()).enum] ?? ""
35
+ );
36
+ }
37
+ return (await this.provider.getNetwork()) === "livenet"
38
+ ? "btc"
39
+ : "btcTestnet";
40
+ })();
41
+ const { network } = this.matchNetworkPreference(
42
+ this.preferredNetworks,
43
+ currentNetwork,
44
+ ) ?? { network: currentNetwork };
45
+ if (network === currentNetwork) {
46
+ return;
47
+ }
48
+ if (this.provider.switchChain) {
49
+ const chain = {
50
+ btc: "BITCOIN_MAINNET",
51
+ btcTestnet: "BITCOIN_TESTNET",
52
+ fractalBtc: "FRACTAL_BITCOIN_MAINNET",
53
+ }[network];
54
+ if (chain) {
55
+ await this.provider.switchChain(chain);
56
+ return;
57
+ }
58
+ } else if (network === "btc" || network === "btcTestnet") {
59
+ await this.provider.switchNetwork(
60
+ network === "btc" ? "livenet" : "testnet",
61
+ );
62
+ }
63
+ throw new Error(
64
+ `UniSat wallet doesn't support the requested chain ${network}`,
65
+ );
66
+ }
67
+
22
68
  /**
23
69
  * Gets the Bitcoin account address.
24
70
  * @returns {Promise<string>} A promise that resolves to the Bitcoin account address.
@@ -41,6 +87,15 @@ export class Signer extends ccc.SignerBtc {
41
87
  */
42
88
  async connect(): Promise<void> {
43
89
  await this.provider.requestAccounts();
90
+ await this.ensureNetwork();
91
+ }
92
+
93
+ async replaceClient(client: ccc.Client): Promise<boolean> {
94
+ if (await super.replaceClient(client)) {
95
+ await this.ensureNetwork();
96
+ return true;
97
+ }
98
+ return false;
44
99
  }
45
100
 
46
101
  /**
@@ -7,12 +7,15 @@ import { Signer } from "./signer";
7
7
  * @param {ccc.Client} client - The client instance.
8
8
  * @returns {Signer | undefined} The Signer instance if the UniSat provider is available, otherwise undefined.
9
9
  */
10
- export function getUniSatSigner(client: ccc.Client): Signer | undefined {
10
+ export function getUniSatSigner(
11
+ client: ccc.Client,
12
+ preferredNetworks: ccc.NetworkPreference[],
13
+ ): Signer | undefined {
11
14
  const windowRef = window as { unisat?: Provider };
12
15
 
13
16
  if (typeof windowRef.unisat === "undefined") {
14
17
  return undefined;
15
18
  }
16
19
 
17
- return new Signer(client, windowRef.unisat);
20
+ return new Signer(client, windowRef.unisat, preferredNetworks);
18
21
  }