From 6410c87d6ac178b80043f7cd3565c0f238259e8c Mon Sep 17 00:00:00 2001 From: Nishant Ghodke Date: Fri, 29 Sep 2023 15:02:19 +0530 Subject: [PATCH] feat: integrate Coin98 --- .../src/browser-wallets/coin98/addresses.ts | 41 ++++++++++++++++ .../sdk/src/browser-wallets/coin98/index.ts | 3 ++ .../src/browser-wallets/coin98/signatures.ts | 48 +++++++++++++++++++ .../sdk/src/browser-wallets/coin98/utils.ts | 9 ++++ 4 files changed, 101 insertions(+) create mode 100644 packages/sdk/src/browser-wallets/coin98/addresses.ts create mode 100644 packages/sdk/src/browser-wallets/coin98/index.ts create mode 100644 packages/sdk/src/browser-wallets/coin98/signatures.ts create mode 100644 packages/sdk/src/browser-wallets/coin98/utils.ts diff --git a/packages/sdk/src/browser-wallets/coin98/addresses.ts b/packages/sdk/src/browser-wallets/coin98/addresses.ts new file mode 100644 index 00000000..cb0b2e0d --- /dev/null +++ b/packages/sdk/src/browser-wallets/coin98/addresses.ts @@ -0,0 +1,41 @@ +import { getAddressFormat } from "../.." +import { Network } from "../../config/types" +import { isCoin98Installed, UnisatNetwork } from "./utils" + +export async function getAddresses(network: Network) { + if (!isCoin98Installed()) { + throw new Error("Coin98 not installed") + } + + if (!network) { + throw new Error("Invalid options provided") + } + + let targetNetwork: UnisatNetwork = "livenet" + const connectedNetwork = await window.coin98.getNetwork() + + if (network === "testnet") { + targetNetwork = network + } + + if (connectedNetwork !== targetNetwork) { + await window.coin98.switchNetwork(targetNetwork) + } + + const accounts = await window.coin98.requestAccounts() + const publicKey = await window.coin98.getPublicKey() + + if (!accounts[0]) { + return [] + } + + const formatObj = getAddressFormat(accounts[0], network) + + return [ + { + pub: publicKey, + address: formatObj.address, + format: formatObj.format + } + ] +} diff --git a/packages/sdk/src/browser-wallets/coin98/index.ts b/packages/sdk/src/browser-wallets/coin98/index.ts new file mode 100644 index 00000000..ebbce1e6 --- /dev/null +++ b/packages/sdk/src/browser-wallets/coin98/index.ts @@ -0,0 +1,3 @@ +export * from "./addresses" +export * from "./signatures" +export * from "./utils" diff --git a/packages/sdk/src/browser-wallets/coin98/signatures.ts b/packages/sdk/src/browser-wallets/coin98/signatures.ts new file mode 100644 index 00000000..1efb9df3 --- /dev/null +++ b/packages/sdk/src/browser-wallets/coin98/signatures.ts @@ -0,0 +1,48 @@ +import { Psbt } from "bitcoinjs-lib" + +import { BrowserWalletSignPSBTResponse } from "../types" +import { UnisatSignPSBTOptions } from "../unisat/types" +import { isCoin98Installed } from "./utils" + +export async function signPsbt( + psbt: Psbt, + { finalize = true, extractTx = true }: UnisatSignPSBTOptions = {} +): Promise { + if (!isCoin98Installed()) { + throw new Error("Coin98 not installed") + } + + const psbtHex = psbt.toHex() + const signedPsbtHex = await window.coin98.signPsbt(psbtHex, { autoFinalized: finalize }) + if (!signedPsbtHex) { + throw new Error("Failed to sign psbt hex using Coin98") + } + + if (psbtHex === signedPsbtHex) { + throw new Error("Psbt has already been signed.") + } + + const signedPsbt = Psbt.fromHex(signedPsbtHex) + + return { + hex: extractTx ? signedPsbt.extractTransaction().toHex() : signedPsbt.toHex(), + base64: !extractTx ? signedPsbt.toBase64() : null + } +} + +export async function signMessage(message: string) { + if (!isCoin98Installed()) { + throw new Error("Coin98 not installed.") + } + + const signature = await window.coin98.signMessage(message) + + if (!signature) { + throw new Error("Failed to sign message using Coin98") + } + + return { + base64: signature, + hex: Buffer.from(signature, "base64").toString("hex") + } +} diff --git a/packages/sdk/src/browser-wallets/coin98/utils.ts b/packages/sdk/src/browser-wallets/coin98/utils.ts new file mode 100644 index 00000000..6b5bd018 --- /dev/null +++ b/packages/sdk/src/browser-wallets/coin98/utils.ts @@ -0,0 +1,9 @@ +export function isCoin98Installed() { + if (typeof window.coin98?.bitcoin !== "undefined") { + return false + } + + return false +} + +export type UnisatNetwork = "livenet" | "testnet"