-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3cc2c13
commit 37f73fc
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./addresses" | ||
export * from "./signatures" | ||
export * from "./utils" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |