Skip to content

Commit

Permalink
feat: refactor ledger and psbt related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed May 8, 2024
1 parent 4eb1bfd commit af87613
Show file tree
Hide file tree
Showing 13 changed files with 994 additions and 919 deletions.
360 changes: 53 additions & 307 deletions src/bitcoin-functions.ts

Large diffs are not rendered by default.

213 changes: 213 additions & 0 deletions src/bitgo-functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/** @format */

import { CoinConstructor, EnvironmentName, Wallet } from '@bitgo/sdk-core';
import { BitGoAddress } from './models/bitgo-models.js';
import { bitcoinToSats } from './utilities.js';
import { Network } from 'bitcoinjs-lib';
import { Btc, Tbtc } from '@bitgo/sdk-coin-btc';
import { bitcoin, testnet } from 'bitcoinjs-lib/src/networks.js';
import { BitGoAPI } from '@bitgo/sdk-api';

function findBitGoAddress(bitGoAddresses: BitGoAddress[], targetAddress: string): BitGoAddress {

Check failure on line 11 in src/bitgo-functions.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

'findBitGoAddress' is defined but never used
const bitGoAddress = bitGoAddresses.find((address) => address.address === targetAddress);
if (!bitGoAddress) {
throw new Error(`Address ${targetAddress} not found.`);
}
return bitGoAddress;
}

async function createTaprootAddress(bitGoWallet: Wallet, label: string) {

Check failure on line 19 in src/bitgo-functions.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

'createTaprootAddress' is defined but never used
try {
const taprootAddress = await bitGoWallet.createAddress({
chain: 30,
label,
});
console.log(`Created Taproot Address: ${JSON.stringify(taprootAddress, null, 2)}`);
} catch (error) {
throw new Error(`Error while creating Taproot address: ${error}`);
}
}

async function createNativeSegwitAddress(bitGoWallet: Wallet, label: string) {

Check failure on line 31 in src/bitgo-functions.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

'createNativeSegwitAddress' is defined but never used
try {
const nativeSegwitAddress = await bitGoWallet.createAddress({
chain: 20,
label,
});
console.log(`Created Native Segwit Address: ${JSON.stringify(nativeSegwitAddress, null, 2)}`);
} catch (error) {
throw new Error(`Error while creating Native Segwit address: ${error}`);
}
}

async function createMultisigWallet() {

Check failure on line 43 in src/bitgo-functions.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

'createMultisigWallet' is defined but never used
const {
BITCOIN_NETWORK,
BITGO_ACCESS_TOKEN,
BITGO_WALLET_PASSPHRASE,
BITGO_WALLET_ID,
BITGO_NATIVE_SEGWIT_ADDRESS,
BITGO_TAPROOT_ADDRESS,
USER_XPUB,
BACKUP_XPUB,
BITGO_XPUB,
} = process.env;

if (
!BITCOIN_NETWORK ||
!BITGO_ACCESS_TOKEN ||
!BITGO_WALLET_PASSPHRASE ||
!BITGO_WALLET_ID ||
!BITGO_NATIVE_SEGWIT_ADDRESS ||
!BITGO_TAPROOT_ADDRESS ||
!USER_XPUB ||
!BACKUP_XPUB ||
!BITGO_XPUB
) {
throw new Error('Please provide all the required Environment Variables.');
}

let environmentName: EnvironmentName;
let coinType: string;
let coinInstance: CoinConstructor;
let bitcoinNetwork: Network;

switch (BITCOIN_NETWORK) {
case 'bitcoin':
environmentName = 'prod';
coinType = 'btc';
coinInstance = Btc.createInstance;
bitcoinNetwork = bitcoin;
break;
case 'testnet':
environmentName = 'test';
coinType = 'tbtc';
coinInstance = Tbtc.createInstance;
bitcoinNetwork = testnet;

Check failure on line 86 in src/bitgo-functions.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

'bitcoinNetwork' is assigned a value but never used
break;
default:
throw new Error('Invalid BITCOIN_NETWORK Value. Please provide either "bitcoin" or "testnet".');
}

const attestorGroupXPublicKey = 'xpub43f9a14c790c0b86ce78bec919e96725e56aee8e0a0fdd8138aa7b351930b3c1';

Check failure on line 92 in src/bitgo-functions.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

'attestorGroupXPublicKey' is assigned a value but never used

let bitGoAPI: BitGoAPI;
try {
bitGoAPI = new BitGoAPI({ accessToken: BITGO_ACCESS_TOKEN, env: environmentName });
} catch (error) {
throw new Error(`Error while initializing BitGo API: ${error}`);
}

bitGoAPI.coin(coinType).wallets().generateWallet({ label: 'Test Wallet' });

bitGoAPI.register(coinType, coinInstance);
}

async function getBitGoDetails() {

Check failure on line 106 in src/bitgo-functions.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

'getBitGoDetails' is defined but never used
const {
BITCOIN_NETWORK,
BITGO_ACCESS_TOKEN,
BITGO_WALLET_PASSPHRASE,
BITGO_WALLET_ID,
BITGO_NATIVE_SEGWIT_ADDRESS,
BITGO_TAPROOT_ADDRESS,
USER_XPUB,
BACKUP_XPUB,
BITGO_XPUB,
} = process.env;

if (
!BITCOIN_NETWORK ||
!BITGO_ACCESS_TOKEN ||
!BITGO_WALLET_PASSPHRASE ||
!BITGO_WALLET_ID ||
!BITGO_NATIVE_SEGWIT_ADDRESS ||
!BITGO_TAPROOT_ADDRESS ||
!USER_XPUB ||
!BACKUP_XPUB ||
!BITGO_XPUB
) {
throw new Error('Please provide all the required Environment Variables.');
}

let environmentName: EnvironmentName;
let coinType: string;
let coinInstance: CoinConstructor;
let bitcoinNetwork: Network;

switch (BITCOIN_NETWORK) {
case 'bitcoin':
environmentName = 'prod';
coinType = 'btc';
coinInstance = Btc.createInstance;
bitcoinNetwork = bitcoin;
break;
case 'testnet':
environmentName = 'test';
coinType = 'tbtc';
coinInstance = Tbtc.createInstance;
bitcoinNetwork = testnet;
break;
default:
throw new Error('Invalid BITCOIN_NETWORK Value. Please provide either "bitcoin" or "testnet".');
}

let bitGoAPI: BitGoAPI;
try {
bitGoAPI = new BitGoAPI({ accessToken: BITGO_ACCESS_TOKEN, env: environmentName });
} catch (error) {
throw new Error(`Error while initializing BitGo API: ${error}`);
}

bitGoAPI.coin(coinType).wallets().generateWallet({ label: 'Test Wallet' });

bitGoAPI.register(coinType, coinInstance);

let bitGoWallet: Wallet;
try {
bitGoWallet = await bitGoAPI.coin(coinType).wallets().getWallet({ id: BITGO_WALLET_ID });
} catch (error) {
throw new Error(`Error while retrieving BitGo wallet: ${error}`);
}

const bitGoKeyChain = await bitGoAPI.coin(coinType).keychains().getKeysForSigning({ wallet: bitGoWallet });

return {
bitGoAPI,
bitGoWallet,
bitGoKeyChain,
bitGoWalletPassphrase: BITGO_WALLET_PASSPHRASE,
nativeSegwitAddress: BITGO_NATIVE_SEGWIT_ADDRESS,
taprootAddress: BITGO_TAPROOT_ADDRESS,
userXPUB: USER_XPUB,
backupXPUB: BACKUP_XPUB,
bitGoXPUB: BITGO_XPUB,
bitcoinNetwork,
};
}

/**
* Creates a Funding Transaction to fund the Multisig Transaction.
*
* @param bitcoinAmount - The amount of Bitcoin to fund the Transaction with.
* @param multisigAddress - The Multisig Address.
* @param feeRecipientAddress - The Fee Recipient's Address.
* @param feeBasisPoints - The Fee Basis Points.
* @returns The Funding Transaction Info.
*/
export function getFundingTransactionRecipients(
bitcoinAmount: number,
multisigAddress: string,
feeRecipientAddress: string,
feeBasisPoints: number
) {
const recipients = [
{ amount: bitcoinToSats(bitcoinAmount), address: multisigAddress },
{
amount: bitcoinToSats(bitcoinAmount) * feeBasisPoints,
address: feeRecipientAddress,
},
];

return recipients;
}
17 changes: 17 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,20 @@ export const DERIVATION_PATH_NATIVE_SEGWIT_FROM_MASTER = `m/0/0/20/`;
export const DERIVATION_PATH_NATIVE_SEGWIT_FROM_CHILD = `0/0/20/`;
export const DERIVATION_PATH_TAPROOT_FROM_MASTER = `m/0/0/30/`;
export const DERIVATION_PATH_TAPROOT_FROM_CHILD = `0/0/30/`;

export const TEST_EXTENDED_PRIVATE_KEY_1 =
'tprv8ZgxMBicQKsPdUfw7LM946yzMWhPrDtmBpB3R5Czx3u98TB2bXgUnkGQbPrNaQ8VQsbjNYseSsggRETuFExqhHoAoqCbrcpVj8pWShR5eQy';
export const TEST_EXTENDED_PUBLIC_KEY_1 =
'tpubD6NzVbkrYhZ4Wwhizz1jTWe6vYDL1Z5fm7mphbFJNKhXxwRoDvW4yEtGmWJ6n9JE86wpvQsDpzn5t49uenYStgAqwgmKNjDe1D71TdAjy8o';
export const TEST_MASTER_FINGERPRINT_1 = '8400dc04';

export const TEST_EXTENDED_PRIVATE_KEY_2 =
'tprv8ZgxMBicQKsPfJ6T1H5ErNLa1fZyj2fxCR7vRqVokCLvWg9JypYJoGVdvU6UNkj59o6qDdB97QFk7CQa2XnKZGSzQGhfoc4hCGXrviFuxwP';
export const TEST_EXTENDED_PUBLIC_KEY_2 =
'tpubD6NzVbkrYhZ4Ym8EtvjqFmzgah5utMrrmiihiMY7AU9KMAQ5cDMtym7W6ccSUinTVbDqK1Vno96HNhaqhS1DuVCrjHoFG9bFa3DKUUMErCv';
export const TEST_MASTER_FINGERPRINT_2 = 'b2cd3e18';

export const TAPROOT_UNSPENDABLE_KEY_STRING = '50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0';

export const TAPROOT_DERIVATION_PATH = "86'";
export const NATIVE_SEGWIT_DERIVATION_PATH = "84'";
Loading

0 comments on commit af87613

Please sign in to comment.