Skip to content

Commit

Permalink
feat: add doge signer to utxo global
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and Hanssen0 committed Dec 15, 2024
1 parent 3ecf5e0 commit e63a06e
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .changeset/pre.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"mode": "pre",
"mode": "exit",
"tag": "alpha",
"initialVersions": {
"@ckb-ccc/ccc": "0.0.15",
Expand Down
8 changes: 8 additions & 0 deletions .changeset/pretty-coins-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@ckb-ccc/utxo-global": patch
"@ckb-ccc/connector": patch
"@ckb-ccc/core": patch
"@ckb-ccc/ccc": patch
---

feat: support doge signer
10 changes: 10 additions & 0 deletions packages/ccc/src/signersController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ export class SignersController {
signerType: ccc.SignerType.BTC,
network: "btcTestnet",
},
{
addressPrefix: "ckb",
signerType: ccc.SignerType.Doge,
network: "doge",
},
{
addressPrefix: "ckt",
signerType: ccc.SignerType.Doge,
network: "dogeTestnet",
},
];

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ export abstract class Client {
async waitTransaction(
txHash: HexLike,
confirmations: number = 0,
timeout: number = 30000,
timeout: number = 60000,
interval: number = 2000,
): Promise<ClientTransactionResponse | undefined> {
const startTime = Date.now();
Expand Down
114 changes: 114 additions & 0 deletions packages/utxo-global/src/doge/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { ccc } from "@ckb-ccc/core";
import { Provider } from "../advancedBarrel.js";

/**
* @public
*/
export class SignerDoge extends ccc.SignerDoge {
private accountCache: string | undefined;

constructor(
client: ccc.Client,
public readonly provider: Provider,
private readonly preferredNetworks: ccc.NetworkPreference[] = [
{
addressPrefix: "ckb",
signerType: ccc.SignerType.Doge,
network: "doge",
},
{
addressPrefix: "ckt",
signerType: ccc.SignerType.Doge,
network: "dogeTestnet",
},
],
) {
super(client);
}

async getDogeAddress(): Promise<string> {
const accounts = await this.provider.getAccount();
this.accountCache = accounts[0];
return this.accountCache;
}

/**
* Ensure the BTC network is the same as CKB network.
*/
async ensureNetwork(): Promise<void> {
const network = await this._getNetworkToChange();
if (!network) {
return;
}

const chain = {
doge: "dogecoin",
dogeTestnet: "dogecoin_testnet",
}[network];

if (chain) {
await this.provider.switchNetwork(chain);
return;
}

throw new Error(
`UTXO Global Doge wallet doesn't support the requested chain ${network}`,
);
}

async _getNetworkToChange(): Promise<string | undefined> {
const currentNetwork = {
dogecoin: "doge",
dogecoin_testnet: "dogeTestnet",
}[await this.provider.getNetwork()];

const { network } = this.matchNetworkPreference(
this.preferredNetworks,
currentNetwork,
) ?? { network: currentNetwork };
if (network === currentNetwork) {
return;
}

return network;
}

onReplaced(listener: () => void): () => void {
const stop: (() => void)[] = [];
const replacer = async () => {
listener();
stop[0]?.();
};
stop.push(() => {
this.provider.removeListener("accountsChanged", replacer);
this.provider.removeListener("networkChanged", replacer);
});

this.provider.on("accountsChanged", replacer);
this.provider.on("networkChanged", replacer);

return stop[0];
}

async connect(): Promise<void> {
await this.provider.connect();
await this.ensureNetwork();
}

async isConnected(): Promise<boolean> {
if ((await this._getNetworkToChange()) !== undefined) {
return false;
}

return await this.provider.isConnected();
}

async signMessageRaw(message: string | ccc.BytesLike): Promise<string> {
const challenge =
typeof message === "string" ? message : ccc.hexFrom(message).slice(2);
return this.provider.signMessage(
challenge,
this.accountCache ?? (await this.getDogeAddress()),
);
}
}
10 changes: 10 additions & 0 deletions packages/utxo-global/src/signersFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ccc } from "@ckb-ccc/core";
import { Provider } from "./advancedBarrel.js";
import { SignerBtc } from "./btc/index.js";
import { SignerCkb } from "./ckb/index.js";
import { SignerDoge } from "./doge/index.js";

/**
* @public
Expand All @@ -14,6 +15,7 @@ export function getUtxoGlobalSigners(
utxoGlobal?: {
bitcoinSigner: Provider;
ckbSigner: Provider;
dogeSigner: Provider;
};
};

Expand All @@ -34,5 +36,13 @@ export function getUtxoGlobalSigners(
preferredNetworks,
),
},
{
name: "DOGE",
signer: new SignerDoge(
client,
windowRef.utxoGlobal.dogeSigner,
preferredNetworks,
),
},
];
}

0 comments on commit e63a06e

Please sign in to comment.