@ckb-ccc/uni-sat 0.0.8-alpha.4 → 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.
- package/dist/advancedBarrel.d.ts +26 -0
- package/dist/advancedBarrel.d.ts.map +1 -1
- package/dist/signer.d.ts +7 -1
- package/dist/signer.d.ts.map +1 -1
- package/dist/signer.js +46 -1
- package/dist/signersFactory.d.ts +1 -1
- package/dist/signersFactory.d.ts.map +1 -1
- package/dist/signersFactory.js +2 -2
- package/dist.commonjs/advancedBarrel.d.ts +26 -0
- package/dist.commonjs/advancedBarrel.d.ts.map +1 -1
- package/dist.commonjs/signer.d.ts +7 -1
- package/dist.commonjs/signer.d.ts.map +1 -1
- package/dist.commonjs/signer.js +46 -1
- package/dist.commonjs/signersFactory.d.ts +1 -1
- package/dist.commonjs/signersFactory.d.ts.map +1 -1
- package/dist.commonjs/signersFactory.js +2 -2
- package/package.json +3 -3
- package/src/advancedBarrel.ts +24 -0
- package/src/signer.ts +55 -0
- package/src/signersFactory.ts +5 -2
package/dist/advancedBarrel.d.ts
CHANGED
|
@@ -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,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","sourceRoot":"","sources":["../src/advancedBarrel.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,QAAQ;IACvB;;;OAGG;IACH,e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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.
|
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;
|
|
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"}
|
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.
|
package/dist/signersFactory.d.ts
CHANGED
|
@@ -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,
|
|
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"}
|
package/dist/signersFactory.js
CHANGED
|
@@ -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
|
}
|
|
@@ -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,CA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,MAAM,CAAA;KAAE,CAAC,CAAC;IAE5D;;;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"}
|
|
@@ -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;AAE5C;;;;GAIG;AACH,qBAAa,MAAO,SAAQ,GAAG,CAAC,SAAS;aAQrB,QAAQ,EAAE,QAAQ;
|
|
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"}
|
package/dist.commonjs/signer.js
CHANGED
|
@@ -13,9 +13,46 @@ class Signer extends core_1.ccc.SignerBtc {
|
|
|
13
13
|
* @param {ccc.Client} client - The client instance.
|
|
14
14
|
* @param {Provider} provider - The provider instance.
|
|
15
15
|
*/
|
|
16
|
-
constructor(client, provider) {
|
|
16
|
+
constructor(client, provider, preferredNetworks) {
|
|
17
17
|
super(client);
|
|
18
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}`);
|
|
19
56
|
}
|
|
20
57
|
/**
|
|
21
58
|
* Gets the Bitcoin account address.
|
|
@@ -37,6 +74,14 @@ class Signer extends core_1.ccc.SignerBtc {
|
|
|
37
74
|
*/
|
|
38
75
|
async connect() {
|
|
39
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;
|
|
40
85
|
}
|
|
41
86
|
/**
|
|
42
87
|
* 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,
|
|
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) {
|
|
10
|
+
function getUniSatSigner(client, preferredNetworks) {
|
|
11
11
|
const windowRef = window;
|
|
12
12
|
if (typeof windowRef.unisat === "undefined") {
|
|
13
13
|
return undefined;
|
|
14
14
|
}
|
|
15
|
-
return new signer_1.Signer(client, windowRef.unisat);
|
|
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.
|
|
3
|
+
"version": "0.0.9-alpha.0",
|
|
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.
|
|
53
|
+
"@ckb-ccc/core": "0.0.9-alpha.0"
|
|
54
54
|
},
|
|
55
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "ac4435006da9c9e48b3b66fc329e6f539156b252"
|
|
56
56
|
}
|
package/src/advancedBarrel.ts
CHANGED
|
@@ -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
|
/**
|
package/src/signersFactory.ts
CHANGED
|
@@ -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(
|
|
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
|
}
|