Skip to content

Commit

Permalink
feat: integrate Coin98
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcrazycoder committed Oct 10, 2023
1 parent 0421577 commit 6410c87
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
41 changes: 41 additions & 0 deletions packages/sdk/src/browser-wallets/coin98/addresses.ts
Original file line number Diff line number Diff line change
@@ -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
}
]
}
3 changes: 3 additions & 0 deletions packages/sdk/src/browser-wallets/coin98/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./addresses"
export * from "./signatures"
export * from "./utils"
48 changes: 48 additions & 0 deletions packages/sdk/src/browser-wallets/coin98/signatures.ts
Original file line number Diff line number Diff line change
@@ -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<BrowserWalletSignPSBTResponse> {
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")
}
}
9 changes: 9 additions & 0 deletions packages/sdk/src/browser-wallets/coin98/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function isCoin98Installed() {
if (typeof window.coin98?.bitcoin !== "undefined") {
return false
}

return false
}

export type UnisatNetwork = "livenet" | "testnet"

0 comments on commit 6410c87

Please sign in to comment.