Skip to content

Commit

Permalink
Merge pull request #1046 from oraidex/feat/new-btc-sign
Browse files Browse the repository at this point in the history
Feat/new btc sign
  • Loading branch information
haunv3 authored Dec 3, 2024
2 parents 6d86bfc + 996627f commit d68e7b3
Showing 1 changed file with 53 additions and 9 deletions.
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

0 comments on commit d68e7b3

Please sign in to comment.