Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk): add support for Coin98 Bitcoin wallet extension #95

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.bitcoin.getNetwork()

if (network === "testnet") {
targetNetwork = network
}

if (connectedNetwork !== targetNetwork) {
await window.coin98.bitcoin.switchNetwork(targetNetwork)
}

const accounts = await window.coin98.bitcoin.requestAccounts()
const publicKey = await window.coin98.bitcoin.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.bitcoin.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.bitcoin.signMessage(message)

if (!signature) {
throw new Error("Failed to sign message using Coin98")
}

return {
base64: signature,
hex: Buffer.from(signature, "base64").toString("hex")
}
}
25 changes: 25 additions & 0 deletions packages/sdk/src/browser-wallets/coin98/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { RequireAtLeastOne } from "../../utils/types"
import { UnisatNetwork } from "../unisat"

interface Coin98SignPSBTOptions {
autoFinalized?: boolean
toSignInputs?: Array<
RequireAtLeastOne<{
address?: string
publicKey?: string
}> & {
index: number
sigHashTypes?: number[]
}
>
}

export interface Coin98 {
requestAccounts: () => Promise<string[]>
getAccounts: () => Promise<string>
getNetwork: () => Promise<UnisatNetwork>
getPublicKey: () => Promise<string>
signMessage: (message: string) => Promise<string>
signPsbt: (hex: string, { autoFinalized = true, toSignInputs }: Coin98SignPSBTOptions) => Promise<string>
switchNetwork: (network: UnisatNetwork) => Promise<void>
}
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"
15 changes: 11 additions & 4 deletions packages/sdk/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
declare interface Window {
unisat: Unisat
satsConnect: any
ethereum: MetaMask
import { BitcoinProvider } from "sats-connect"

import { Coin98 } from "./browser-wallets/coin98/types"

declare global {
interface Window {
unisat: Unisat
coin98: { bitcoin: Coin98 }
BitcoinProvider: BitcoinProvider
ethereum: MetaMask
}
}

type Unisat = {
Expand Down