@ckb-ccc/okx 0.0.12-alpha.6 → 0.0.12-alpha.7

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/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # @ckb-ccc/okx
2
+
3
+ ## 0.0.12-alpha.7
4
+
5
+ ### Patch Changes
6
+
7
+ - fix(connector): catch connecting error
8
+ - Updated dependencies
9
+ - @ckb-ccc/nip07@0.0.12-alpha.2
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/nostr/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAErD;;;;GAIG;AACH,qBAAa,WAAY,SAAQ,GAAG,CAAC,WAAW;aAK5B,QAAQ,EAAE,aAAa;IAJzC,OAAO,CAAC,cAAc,CAAC,CAA8B;gBAGnD,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,QAAQ,EAAE,aAAa;IAKnC,iBAAiB,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAOrC,cAAc,CAClB,KAAK,EAAE,GAAG,CAAC,UAAU,GACpB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAO9B,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAI9B,UAAU,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI;IAetC,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;CAGtC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/nostr/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAErD;;;;GAIG;AACH,qBAAa,WAAY,SAAQ,GAAG,CAAC,WAAW;aAK5B,QAAQ,EAAE,aAAa;IAJzC,OAAO,CAAC,cAAc,CAAC,CAA8B;gBAGnD,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,QAAQ,EAAE,aAAa;IAKnC,iBAAiB,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAsBrC,cAAc,CAClB,KAAK,EAAE,GAAG,CAAC,UAAU,GACpB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAO9B,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAM9B,UAAU,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI;IAetC,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;CAGtC"}
@@ -12,7 +12,20 @@ export class NostrSigner extends ccc.SignerNostr {
12
12
  }
13
13
  async getNostrPublicKey() {
14
14
  if (!this.publicKeyCache) {
15
- this.publicKeyCache = this.provider.getPublicKey();
15
+ this.publicKeyCache = this.provider
16
+ .getPublicKey()
17
+ .then((v) => {
18
+ // For some account types of OKX Wallet, this might returns null
19
+ // e.g. Keyless accounts
20
+ if (v) {
21
+ return v;
22
+ }
23
+ throw Error("This OKX Wallet account does not support Nostr");
24
+ })
25
+ .catch((e) => {
26
+ this.publicKeyCache = undefined;
27
+ throw e;
28
+ });
16
29
  }
17
30
  return ccc.hexFrom(await this.publicKeyCache);
18
31
  }
@@ -24,6 +37,7 @@ export class NostrSigner extends ccc.SignerNostr {
24
37
  }
25
38
  async connect() {
26
39
  await this.provider.connect?.(); // Help extension to switch network
40
+ await this.getNostrPublicKey();
27
41
  }
28
42
  onReplaced(listener) {
29
43
  const stop = [];
@@ -1,2 +1,2 @@
1
1
  export * as OkxA from "./advancedBarrel.js";
2
- //# sourceMappingURL=advanced.d.ts.map
2
+ //# sourceMappingURL=advanced.d.ts.map
@@ -1,27 +1,48 @@
1
1
  "use strict";
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) {
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) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
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.OkxA = void 0;
27
48
  exports.OkxA = __importStar(require("./advancedBarrel.js"));
@@ -1,42 +1,47 @@
1
1
  import { Nip07A } from "@ckb-ccc/nip07/advanced";
2
2
  import { UniSatA } from "@ckb-ccc/uni-sat/advanced";
3
- export interface BitcoinProvider extends Pick<UniSatA.Provider, "on" | "removeListener" | "signMessage">, Partial<Omit<UniSatA.Provider, "on" | "removeListener" | "signMessage">> {
4
- connect?(): Promise<{
5
- address: string;
6
- publicKey: string;
7
- compressedPublicKey: string;
8
- }>;
9
- getSelectedAccount?(): Promise<{
10
- address: string;
11
- publicKey: string;
12
- compressedPublicKey: string;
13
- } | null>;
3
+ export interface BitcoinProvider
4
+ extends Pick<UniSatA.Provider, "on" | "removeListener" | "signMessage">,
5
+ Partial<Omit<UniSatA.Provider, "on" | "removeListener" | "signMessage">> {
6
+ connect?(): Promise<{
7
+ address: string;
8
+ publicKey: string;
9
+ compressedPublicKey: string;
10
+ }>;
11
+ getSelectedAccount?(): Promise<{
12
+ address: string;
13
+ publicKey: string;
14
+ compressedPublicKey: string;
15
+ } | null>;
14
16
  }
15
17
  export interface NostrProvider extends Nip07A.Provider {
16
- connect?(): Promise<void>;
17
- /**
18
- * Adds an event listener to the provider.
19
- */
20
- on: NostrOnMethod;
21
- /**
22
- * Removes an event listener from the provider.
23
- * @param eventName - The name of the event to remove the listener from.
24
- * @param listener - The listener function to remove.
25
- * @returns The provider instance.
26
- */
27
- removeListener(eventName: string, listener: (...args: unknown[]) => unknown): NostrProvider;
18
+ connect?(): Promise<void>;
19
+ /**
20
+ * Adds an event listener to the provider.
21
+ */
22
+ on: NostrOnMethod;
23
+ /**
24
+ * Removes an event listener from the provider.
25
+ * @param eventName - The name of the event to remove the listener from.
26
+ * @param listener - The listener function to remove.
27
+ * @returns The provider instance.
28
+ */
29
+ removeListener(
30
+ eventName: string,
31
+ listener: (...args: unknown[]) => unknown,
32
+ ): NostrProvider;
28
33
  }
29
34
  /**
30
35
  * Interface representing a method to add event listeners to the provider.
31
36
  * @interface
32
37
  */
33
38
  export interface NostrOnMethod {
34
- /**
35
- * Adds an event listener to the provider.
36
- * @param eventName - The name of the event.
37
- * @param listener - The listener function.
38
- * @returns The provider instance.
39
- */
40
- (eventName: string, listener: (...args: unknown[]) => unknown): Promise<void>;
39
+ /**
40
+ * Adds an event listener to the provider.
41
+ * @param eventName - The name of the event.
42
+ * @param listener - The listener function.
43
+ * @returns The provider instance.
44
+ */
45
+ (eventName: string, listener: (...args: unknown[]) => unknown): Promise<void>;
41
46
  }
42
- //# sourceMappingURL=advancedBarrel.d.ts.map
47
+ //# sourceMappingURL=advancedBarrel.d.ts.map
@@ -1,3 +1,3 @@
1
1
  export * from "./btc/index.js";
2
2
  export * from "./signersFactory.js";
3
- //# sourceMappingURL=barrel.d.ts.map
3
+ //# sourceMappingURL=barrel.d.ts.map
@@ -1,18 +1,34 @@
1
1
  "use strict";
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
- };
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("./btc/index.js"), exports);
18
34
  __exportStar(require("./signersFactory.js"), exports);
@@ -6,42 +6,46 @@ import { BitcoinProvider } from "../advancedBarrel.js";
6
6
  * @extends {ccc.SignerBtc}
7
7
  */
8
8
  export declare class BitcoinSigner extends ccc.SignerBtc {
9
- readonly providers: Record<string, BitcoinProvider>;
10
- private readonly preferredNetworks;
11
- private network;
12
- /**
13
- * Creates an instance of Signer.
14
- * @param {ccc.Client} client - The client instance.
15
- * @param {Provider} provider - The provider instance.
16
- */
17
- constructor(client: ccc.Client, providers: Record<string, BitcoinProvider>, preferredNetworks?: ccc.NetworkPreference[]);
18
- get provider(): BitcoinProvider;
19
- /**
20
- * Gets the Bitcoin account address.
21
- * @returns {Promise<string>} A promise that resolves to the Bitcoin account address.
22
- */
23
- getBtcAccount(): Promise<string>;
24
- /**
25
- * Gets the Bitcoin public key.
26
- * @returns {Promise<ccc.Hex>} A promise that resolves to the Bitcoin public key.
27
- */
28
- getBtcPublicKey(): Promise<ccc.Hex>;
29
- /**
30
- * Connects to the provider by requesting accounts.
31
- * @returns {Promise<void>} A promise that resolves when the connection is established.
32
- */
33
- connect(): Promise<void>;
34
- onReplaced(listener: () => void): () => void;
35
- /**
36
- * Checks if the signer is connected.
37
- * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
38
- */
39
- isConnected(): Promise<boolean>;
40
- /**
41
- * Signs a raw message with the Bitcoin account.
42
- * @param {string | ccc.BytesLike} message - The message to sign.
43
- * @returns {Promise<string>} A promise that resolves to the signed message.
44
- */
45
- signMessageRaw(message: string | ccc.BytesLike): Promise<string>;
9
+ readonly providers: Record<string, BitcoinProvider>;
10
+ private readonly preferredNetworks;
11
+ private network;
12
+ /**
13
+ * Creates an instance of Signer.
14
+ * @param {ccc.Client} client - The client instance.
15
+ * @param {Provider} provider - The provider instance.
16
+ */
17
+ constructor(
18
+ client: ccc.Client,
19
+ providers: Record<string, BitcoinProvider>,
20
+ preferredNetworks?: ccc.NetworkPreference[],
21
+ );
22
+ get provider(): BitcoinProvider;
23
+ /**
24
+ * Gets the Bitcoin account address.
25
+ * @returns {Promise<string>} A promise that resolves to the Bitcoin account address.
26
+ */
27
+ getBtcAccount(): Promise<string>;
28
+ /**
29
+ * Gets the Bitcoin public key.
30
+ * @returns {Promise<ccc.Hex>} A promise that resolves to the Bitcoin public key.
31
+ */
32
+ getBtcPublicKey(): Promise<ccc.Hex>;
33
+ /**
34
+ * Connects to the provider by requesting accounts.
35
+ * @returns {Promise<void>} A promise that resolves when the connection is established.
36
+ */
37
+ connect(): Promise<void>;
38
+ onReplaced(listener: () => void): () => void;
39
+ /**
40
+ * Checks if the signer is connected.
41
+ * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
42
+ */
43
+ isConnected(): Promise<boolean>;
44
+ /**
45
+ * Signs a raw message with the Bitcoin account.
46
+ * @param {string | ccc.BytesLike} message - The message to sign.
47
+ * @returns {Promise<string>} A promise that resolves to the signed message.
48
+ */
49
+ signMessageRaw(message: string | ccc.BytesLike): Promise<string>;
46
50
  }
47
- //# sourceMappingURL=index.d.ts.map
51
+ //# sourceMappingURL=index.d.ts.map
@@ -8,142 +8,154 @@ const core_1 = require("@ckb-ccc/core");
8
8
  * @extends {ccc.SignerBtc}
9
9
  */
10
10
  class BitcoinSigner 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, providers, preferredNetworks = [
17
- {
18
- addressPrefix: "ckb",
19
- signerType: core_1.ccc.SignerType.BTC,
20
- network: "btc",
21
- },
22
- {
23
- addressPrefix: "ckt",
24
- signerType: core_1.ccc.SignerType.BTC,
25
- network: "btcTestnet",
26
- },
27
- ]) {
28
- super(client);
29
- this.providers = providers;
30
- this.preferredNetworks = preferredNetworks;
31
- this.network = "btcTestnet";
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
+ providers,
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.providers = providers;
34
+ this.preferredNetworks = preferredNetworks;
35
+ this.network = "btcTestnet";
36
+ }
37
+ get provider() {
38
+ const { network } = this.matchNetworkPreference(
39
+ this.preferredNetworks,
40
+ this.network,
41
+ ) ?? { network: this.network };
42
+ this.network = network;
43
+ const chain = {
44
+ btc: "bitcoin",
45
+ btcTestnet: "bitcoinTestnet",
46
+ btcSignet: "bitcoinSignet",
47
+ }[network];
48
+ if (!chain) {
49
+ throw new Error(
50
+ `OKX wallet doesn't support the requested chain ${this.network}`,
51
+ );
32
52
  }
33
- get provider() {
34
- const { network } = this.matchNetworkPreference(this.preferredNetworks, this.network) ?? { network: this.network };
35
- this.network = network;
36
- const chain = {
37
- btc: "bitcoin",
38
- btcTestnet: "bitcoinTestnet",
39
- btcSignet: "bitcoinSignet",
40
- }[network];
41
- if (!chain) {
42
- throw new Error(`OKX wallet doesn't support the requested chain ${this.network}`);
43
- }
44
- const provider = this.providers[chain];
45
- if (!provider) {
46
- throw new Error(`OKX wallet doesn't support the requested chain ${this.network}`);
47
- }
48
- return provider;
53
+ const provider = this.providers[chain];
54
+ if (!provider) {
55
+ throw new Error(
56
+ `OKX wallet doesn't support the requested chain ${this.network}`,
57
+ );
49
58
  }
50
- /**
51
- * Gets the Bitcoin account address.
52
- * @returns {Promise<string>} A promise that resolves to the Bitcoin account address.
53
- */
54
- async getBtcAccount() {
55
- if (this.provider.getAccounts) {
56
- const address = (await this.provider.getAccounts())[0];
57
- if (!address) {
58
- throw Error("Not connected");
59
- }
60
- return address;
61
- }
62
- if (this.provider.getSelectedAccount) {
63
- const account = await this.provider.getSelectedAccount();
64
- if (!account) {
65
- throw Error("Not connected");
66
- }
67
- return account.address;
68
- }
69
- throw Error("Unsupported OKX provider");
59
+ return provider;
60
+ }
61
+ /**
62
+ * Gets the Bitcoin account address.
63
+ * @returns {Promise<string>} A promise that resolves to the Bitcoin account address.
64
+ */
65
+ async getBtcAccount() {
66
+ if (this.provider.getAccounts) {
67
+ const address = (await this.provider.getAccounts())[0];
68
+ if (!address) {
69
+ throw Error("Not connected");
70
+ }
71
+ return address;
70
72
  }
71
- /**
72
- * Gets the Bitcoin public key.
73
- * @returns {Promise<ccc.Hex>} A promise that resolves to the Bitcoin public key.
74
- */
75
- async getBtcPublicKey() {
76
- if (this.provider.getPublicKey) {
77
- return core_1.ccc.hexFrom(await this.provider.getPublicKey());
78
- }
79
- if (this.provider.getSelectedAccount) {
80
- const account = await this.provider.getSelectedAccount();
81
- if (!account) {
82
- throw Error("Not connected");
83
- }
84
- return core_1.ccc.hexFrom(account.compressedPublicKey);
85
- }
86
- throw Error("Unsupported OKX provider");
73
+ if (this.provider.getSelectedAccount) {
74
+ const account = await this.provider.getSelectedAccount();
75
+ if (!account) {
76
+ throw Error("Not connected");
77
+ }
78
+ return account.address;
87
79
  }
88
- /**
89
- * Connects to the provider by requesting accounts.
90
- * @returns {Promise<void>} A promise that resolves when the connection is established.
91
- */
92
- async connect() {
93
- if (this.provider.requestAccounts) {
94
- await this.provider.requestAccounts();
95
- return;
96
- }
97
- if (this.provider.connect) {
98
- await this.provider.connect();
99
- return;
100
- }
101
- throw Error("Unsupported OKX provider");
80
+ throw Error("Unsupported OKX provider");
81
+ }
82
+ /**
83
+ * Gets the Bitcoin public key.
84
+ * @returns {Promise<ccc.Hex>} A promise that resolves to the Bitcoin public key.
85
+ */
86
+ async getBtcPublicKey() {
87
+ if (this.provider.getPublicKey) {
88
+ return core_1.ccc.hexFrom(await this.provider.getPublicKey());
102
89
  }
103
- onReplaced(listener) {
104
- const stop = [];
105
- const replacer = async () => {
106
- listener();
107
- stop[0]?.();
108
- };
109
- stop.push(() => {
110
- this.provider.removeListener("accountChanged", replacer);
111
- });
112
- this.provider.on("accountChanged", replacer);
113
- return stop[0];
90
+ if (this.provider.getSelectedAccount) {
91
+ const account = await this.provider.getSelectedAccount();
92
+ if (!account) {
93
+ throw Error("Not connected");
94
+ }
95
+ return core_1.ccc.hexFrom(account.compressedPublicKey);
114
96
  }
115
- /**
116
- * Checks if the signer is connected.
117
- * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
118
- */
119
- async isConnected() {
120
- try {
121
- this.provider;
122
- }
123
- catch (_) {
124
- return false;
125
- }
126
- if (this.provider.getAccounts) {
127
- if ((await this.provider.getAccounts()).length === 0) {
128
- return false;
129
- }
130
- }
131
- else if (this.provider.getSelectedAccount) {
132
- if ((await this.provider.getSelectedAccount()) === null) {
133
- return false;
134
- }
135
- }
136
- await this.connect();
137
- return true;
97
+ throw Error("Unsupported OKX provider");
98
+ }
99
+ /**
100
+ * Connects to the provider by requesting accounts.
101
+ * @returns {Promise<void>} A promise that resolves when the connection is established.
102
+ */
103
+ async connect() {
104
+ if (this.provider.requestAccounts) {
105
+ await this.provider.requestAccounts();
106
+ return;
138
107
  }
139
- /**
140
- * Signs a raw message with the Bitcoin account.
141
- * @param {string | ccc.BytesLike} message - The message to sign.
142
- * @returns {Promise<string>} A promise that resolves to the signed message.
143
- */
144
- async signMessageRaw(message) {
145
- const challenge = typeof message === "string" ? message : core_1.ccc.hexFrom(message).slice(2);
146
- return this.provider.signMessage(challenge, "ecdsa");
108
+ if (this.provider.connect) {
109
+ await this.provider.connect();
110
+ return;
147
111
  }
112
+ throw Error("Unsupported OKX provider");
113
+ }
114
+ onReplaced(listener) {
115
+ const stop = [];
116
+ const replacer = async () => {
117
+ listener();
118
+ stop[0]?.();
119
+ };
120
+ stop.push(() => {
121
+ this.provider.removeListener("accountChanged", replacer);
122
+ });
123
+ this.provider.on("accountChanged", replacer);
124
+ return stop[0];
125
+ }
126
+ /**
127
+ * Checks if the signer is connected.
128
+ * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
129
+ */
130
+ async isConnected() {
131
+ try {
132
+ this.provider;
133
+ } catch (_) {
134
+ return false;
135
+ }
136
+ if (this.provider.getAccounts) {
137
+ if ((await this.provider.getAccounts()).length === 0) {
138
+ return false;
139
+ }
140
+ } else if (this.provider.getSelectedAccount) {
141
+ if ((await this.provider.getSelectedAccount()) === null) {
142
+ return false;
143
+ }
144
+ }
145
+ await this.connect();
146
+ return true;
147
+ }
148
+ /**
149
+ * Signs a raw message with the Bitcoin account.
150
+ * @param {string | ccc.BytesLike} message - The message to sign.
151
+ * @returns {Promise<string>} A promise that resolves to the signed message.
152
+ */
153
+ async signMessageRaw(message) {
154
+ const challenge =
155
+ typeof message === "string"
156
+ ? message
157
+ : core_1.ccc.hexFrom(message).slice(2);
158
+ return this.provider.signMessage(challenge, "ecdsa");
159
+ }
148
160
  }
149
161
  exports.BitcoinSigner = BitcoinSigner;
@@ -1,2 +1,2 @@
1
1
  export * as Okx from "./barrel.js";
2
- //# sourceMappingURL=index.d.ts.map
2
+ //# sourceMappingURL=index.d.ts.map
@@ -1,27 +1,48 @@
1
1
  "use strict";
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) {
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) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
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.Okx = void 0;
27
48
  exports.Okx = __importStar(require("./barrel.js"));
@@ -6,13 +6,13 @@ import { NostrProvider } from "../advancedBarrel.js";
6
6
  * @extends {ccc.SignerBtc}
7
7
  */
8
8
  export declare class NostrSigner extends ccc.SignerNostr {
9
- readonly provider: NostrProvider;
10
- private publicKeyCache?;
11
- constructor(client: ccc.Client, provider: NostrProvider);
12
- getNostrPublicKey(): Promise<ccc.Hex>;
13
- signNostrEvent(event: ccc.NostrEvent): Promise<Required<ccc.NostrEvent>>;
14
- connect(): Promise<void>;
15
- onReplaced(listener: () => void): () => void;
16
- isConnected(): Promise<boolean>;
9
+ readonly provider: NostrProvider;
10
+ private publicKeyCache?;
11
+ constructor(client: ccc.Client, provider: NostrProvider);
12
+ getNostrPublicKey(): Promise<ccc.Hex>;
13
+ signNostrEvent(event: ccc.NostrEvent): Promise<Required<ccc.NostrEvent>>;
14
+ connect(): Promise<void>;
15
+ onReplaced(listener: () => void): () => void;
16
+ isConnected(): Promise<boolean>;
17
17
  }
18
- //# sourceMappingURL=index.d.ts.map
18
+ //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/nostr/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAErD;;;;GAIG;AACH,qBAAa,WAAY,SAAQ,GAAG,CAAC,WAAW;aAK5B,QAAQ,EAAE,aAAa;IAJzC,OAAO,CAAC,cAAc,CAAC,CAA8B;gBAGnD,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,QAAQ,EAAE,aAAa;IAKnC,iBAAiB,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAOrC,cAAc,CAClB,KAAK,EAAE,GAAG,CAAC,UAAU,GACpB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAO9B,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAI9B,UAAU,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI;IAetC,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;CAGtC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/nostr/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAErD;;;;GAIG;AACH,qBAAa,WAAY,SAAQ,GAAG,CAAC,WAAW;aAK5B,QAAQ,EAAE,aAAa;IAJzC,OAAO,CAAC,cAAc,CAAC,CAA8B;gBAGnD,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,QAAQ,EAAE,aAAa;IAKnC,iBAAiB,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAsBrC,cAAc,CAClB,KAAK,EAAE,GAAG,CAAC,UAAU,GACpB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAO9B,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAM9B,UAAU,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI;IAetC,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;CAGtC"}
@@ -8,40 +8,54 @@ const core_1 = require("@ckb-ccc/core");
8
8
  * @extends {ccc.SignerBtc}
9
9
  */
10
10
  class NostrSigner extends core_1.ccc.SignerNostr {
11
- constructor(client, provider) {
12
- super(client);
13
- this.provider = provider;
14
- this.publicKeyCache = undefined;
15
- }
16
- async getNostrPublicKey() {
17
- if (!this.publicKeyCache) {
18
- this.publicKeyCache = this.provider.getPublicKey();
19
- }
20
- return core_1.ccc.hexFrom(await this.publicKeyCache);
21
- }
22
- async signNostrEvent(event) {
23
- return this.provider.signEvent({
24
- ...event,
25
- pubkey: await this.publicKeyCache,
26
- });
27
- }
28
- async connect() {
29
- await this.provider.connect?.(); // Help extension to switch network
30
- }
31
- onReplaced(listener) {
32
- const stop = [];
33
- const replacer = async () => {
34
- listener();
35
- stop[0]?.();
36
- };
37
- stop.push(() => {
38
- this.provider.removeListener("accountChanged", replacer);
11
+ constructor(client, provider) {
12
+ super(client);
13
+ this.provider = provider;
14
+ this.publicKeyCache = undefined;
15
+ }
16
+ async getNostrPublicKey() {
17
+ if (!this.publicKeyCache) {
18
+ this.publicKeyCache = this.provider
19
+ .getPublicKey()
20
+ .then((v) => {
21
+ // For some account types of OKX Wallet, this might returns null
22
+ // e.g. Keyless accounts
23
+ if (v) {
24
+ return v;
25
+ }
26
+ throw Error("This OKX Wallet account does not support Nostr");
27
+ })
28
+ .catch((e) => {
29
+ this.publicKeyCache = undefined;
30
+ throw e;
39
31
  });
40
- this.provider.on("accountChanged", replacer);
41
- return stop[0];
42
- }
43
- async isConnected() {
44
- return true;
45
32
  }
33
+ return core_1.ccc.hexFrom(await this.publicKeyCache);
34
+ }
35
+ async signNostrEvent(event) {
36
+ return this.provider.signEvent({
37
+ ...event,
38
+ pubkey: await this.publicKeyCache,
39
+ });
40
+ }
41
+ async connect() {
42
+ await this.provider.connect?.(); // Help extension to switch network
43
+ await this.getNostrPublicKey();
44
+ }
45
+ onReplaced(listener) {
46
+ const stop = [];
47
+ const replacer = async () => {
48
+ listener();
49
+ stop[0]?.();
50
+ };
51
+ stop.push(() => {
52
+ this.provider.removeListener("accountChanged", replacer);
53
+ });
54
+ this.provider.on("accountChanged", replacer);
55
+ return stop[0];
56
+ }
57
+ async isConnected() {
58
+ return true;
59
+ }
46
60
  }
47
61
  exports.NostrSigner = NostrSigner;
@@ -4,5 +4,8 @@ import { ccc } from "@ckb-ccc/core";
4
4
  * @param {ccc.Client} client - The client instance.
5
5
  * @returns {BitcoinSigner | undefined} The BitcoinSigner instance if the OKX wallet is available, otherwise undefined.
6
6
  */
7
- export declare function getOKXSigners(client: ccc.Client, preferredNetworks?: ccc.NetworkPreference[]): ccc.SignerInfo[];
8
- //# sourceMappingURL=signersFactory.d.ts.map
7
+ export declare function getOKXSigners(
8
+ client: ccc.Client,
9
+ preferredNetworks?: ccc.NetworkPreference[],
10
+ ): ccc.SignerInfo[];
11
+ //# sourceMappingURL=signersFactory.d.ts.map
@@ -9,19 +9,23 @@ const index_js_2 = require("./nostr/index.js");
9
9
  * @returns {BitcoinSigner | undefined} The BitcoinSigner instance if the OKX wallet is available, otherwise undefined.
10
10
  */
11
11
  function getOKXSigners(client, preferredNetworks) {
12
- const windowRef = window;
13
- if (typeof windowRef.okxwallet === "undefined") {
14
- return [];
15
- }
16
- return [
17
- {
18
- signer: new index_js_1.BitcoinSigner(client, windowRef.okxwallet, preferredNetworks),
19
- name: "BTC",
20
- },
21
- {
22
- signer: new index_js_2.NostrSigner(client, windowRef.okxwallet.nostr),
23
- name: "Nostr",
24
- },
25
- ];
12
+ const windowRef = window;
13
+ if (typeof windowRef.okxwallet === "undefined") {
14
+ return [];
15
+ }
16
+ return [
17
+ {
18
+ signer: new index_js_1.BitcoinSigner(
19
+ client,
20
+ windowRef.okxwallet,
21
+ preferredNetworks,
22
+ ),
23
+ name: "BTC",
24
+ },
25
+ {
26
+ signer: new index_js_2.NostrSigner(client, windowRef.okxwallet.nostr),
27
+ name: "Nostr",
28
+ },
29
+ ];
26
30
  }
27
31
  exports.getOKXSigners = getOKXSigners;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckb-ccc/okx",
3
- "version": "0.0.12-alpha.6",
3
+ "version": "0.0.12-alpha.7",
4
4
  "description": "CCC - CKBer's Codebase. Common Chains Connector's support for OKX",
5
5
  "author": "Hanssen0 <hanssen0@hanssen0.com>",
6
6
  "license": "MIT",
@@ -22,11 +22,6 @@
22
22
  "default": "./dist.commonjs/advanced.js"
23
23
  }
24
24
  },
25
- "scripts": {
26
- "build": "rimraf ./dist && rimraf ./dist.commonjs && tsc && tsc --project tsconfig.commonjs.json && copyfiles -u 2 misc/basedirs/**/* .",
27
- "lint": "eslint",
28
- "format": "prettier --write . && eslint --fix"
29
- },
30
25
  "devDependencies": {
31
26
  "@eslint/js": "^9.1.1",
32
27
  "copyfiles": "^2.4.1",
@@ -44,8 +39,12 @@
44
39
  },
45
40
  "dependencies": {
46
41
  "@ckb-ccc/core": "0.0.12-alpha.1",
47
- "@ckb-ccc/nip07": "0.0.12-alpha.1",
42
+ "@ckb-ccc/nip07": "0.0.12-alpha.2",
48
43
  "@ckb-ccc/uni-sat": "0.0.12-alpha.1"
49
44
  },
50
- "gitHead": "3679eba15c5ec5397c514444594a09b777c3ffe1"
51
- }
45
+ "scripts": {
46
+ "build": "rimraf ./dist && rimraf ./dist.commonjs && tsc && tsc --project tsconfig.commonjs.json && copyfiles -u 2 misc/basedirs/**/* .",
47
+ "lint": "eslint",
48
+ "format": "prettier --write . && eslint --fix"
49
+ }
50
+ }
@@ -18,8 +18,23 @@ export class NostrSigner extends ccc.SignerNostr {
18
18
 
19
19
  async getNostrPublicKey(): Promise<ccc.Hex> {
20
20
  if (!this.publicKeyCache) {
21
- this.publicKeyCache = this.provider.getPublicKey();
21
+ this.publicKeyCache = this.provider
22
+ .getPublicKey()
23
+ .then((v) => {
24
+ // For some account types of OKX Wallet, this might returns null
25
+ // e.g. Keyless accounts
26
+ if (v) {
27
+ return v;
28
+ }
29
+
30
+ throw Error("This OKX Wallet account does not support Nostr");
31
+ })
32
+ .catch((e) => {
33
+ this.publicKeyCache = undefined;
34
+ throw e;
35
+ });
22
36
  }
37
+
23
38
  return ccc.hexFrom(await this.publicKeyCache);
24
39
  }
25
40
 
@@ -34,6 +49,8 @@ export class NostrSigner extends ccc.SignerNostr {
34
49
 
35
50
  async connect(): Promise<void> {
36
51
  await this.provider.connect?.(); // Help extension to switch network
52
+
53
+ await this.getNostrPublicKey();
37
54
  }
38
55
 
39
56
  onReplaced(listener: () => void): () => void {