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/new btc sign #1046

Merged
merged 4 commits into from
Dec 3, 2024
Merged
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
62 changes: 53 additions & 9 deletions src/libs/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,24 @@ import { Key } from '@keplr-wallet/types';
import { ChainIdEnum } from '@oraichain/oraidex-common';
import { network } from 'config/networks';
import { bitcoinChainId } from 'helper/constants';
import {BitcoinUnit} from "bitcoin-units";
export type BitcoinMode = 'core' | 'extension' | 'mobile-web' | 'walletconnect';
// import { CosmosChainId, BitcoinWallet } from '@oraichain/oraidex-common';
type BitcoinChainId = 'bitcoin' | 'bitcoinTestnet';
export enum TransactionBtcType {
Legacy = "legacy",
Bech32 = "bech32",
TapRoot = "tap-root",
Segwit = "segwit",
}
export interface UnsignedBtcTransaction {
amount: string;
to: string;
sender: string;
memo: string;
coinMinimalDenom: string;
chainId: string;
}
export interface IBitcoin {
readonly version: string;
/**
Expand All @@ -21,6 +36,13 @@ export interface IBitcoin {
rawTxHex: string;
}>;
getKey(chainId: string): Promise<Key>;
sendTx(chainId: string, signedTx: string): Promise<string>;
sign(
chainId: string,
signer: string,
data: string | Uint8Array,
type: TransactionBtcType
): Promise<string>;
}

export default class Bitcoin {
Expand All @@ -31,13 +53,14 @@ export default class Bitcoin {
async getBitcoinKey(chainId?: string): Promise<Key | undefined> {
try {
chainId = chainId ?? network.chainId;
const bitcoin = window.bitcoin;
if (!chainId) return undefined;

if (!window.owallet) {
console.error('OWallet not found.');
return undefined;
if (!bitcoin || !window.owallet) {
throw new Error('Bitcoin wallet not found.');
}

//GetKey for new keyring
if (bitcoin.getKey && bitcoin.sign) return bitcoin.getKey(chainId);
//TODO: Default for get key by legacy
return window.owallet.getKey(chainId);
} catch (error) {
console.error('Error while getting Bitcoin key:', error);
Expand All @@ -55,14 +78,35 @@ export default class Bitcoin {
}
}

async signAndBroadCast(chainId: BitcoinChainId = bitcoinChainId, data: object): Promise<{ rawTxHex: string }> {
async signAndBroadCast(chainId: BitcoinChainId = bitcoinChainId, data): Promise<{ rawTxHex: string }> {
try {
if (!window.bitcoin) {
const bitcoin = window.bitcoin;
if (!bitcoin) {
throw new Error('Bitcoin wallet not found.');
}
//Sign for new keyring
if(bitcoin.sign && bitcoin.sendTx){
const amount = new BitcoinUnit(data.msgs.amount, 'satoshi').to('BTC').toString();
const unsignedTx:UnsignedBtcTransaction = {
chainId: chainId,
to:data.msgs.address,
amount:amount,
coinMinimalDenom: "segwit:btc",
memo:data.memo || '',
sender:data.address,
}
const signedTx = await bitcoin.sign(
chainId,
data.address,
JSON.stringify(unsignedTx),
TransactionBtcType.Bech32
);
const txHash = await bitcoin.sendTx(chainId, signedTx);
return {rawTxHex:txHash};
}

const rs = await window.bitcoin.signAndBroadcast(chainId, data);
return rs;
//TODO: Default sign by legacy
return await window.bitcoin.signAndBroadcast(chainId, data);
} catch (error) {
console.error('Error while signing and broadcasting Bitcoin transaction:', error);
throw new Error(`Error while signing and broadcasting Bitcoin transaction: ${JSON.stringify(error)}`);
Expand Down
Loading