Skip to content

Commit

Permalink
allow overriding the chain used
Browse files Browse the repository at this point in the history
  • Loading branch information
antonilol committed May 12, 2024
1 parent ad883da commit bebde90
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
23 changes: 17 additions & 6 deletions btc.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 16 additions & 5 deletions btc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,22 @@ export const networks: { [name in Chain]: bitcoin.networks.Network } = {
let chain: Chain = 'testnet4';
export let network = networks[chain];

export function setChain(c: Chain): void {
chain = c;
network = networks[chain];
}

export const chainEnvVarKey = 'BTC_STUFF_CHAIN';

const chainEnvVarValue = process.env[chainEnvVarKey];
if (chainEnvVarValue) {
if (networks[chainEnvVarValue]) {
setChain(chainEnvVarValue as Chain);
} else {
console.error(`Invalid chain "${chainEnvVarValue}", leaving it unchanged (currently set to ${chain})`);
}
}

export async function btc(...args: (string | Buffer | number | {} | TransactionType | PsbtType)[]): Promise<string> {
return new Promise((r, e) => {
const cmdargs = [ `-chain=${chain}`, '-stdin' ];
Expand Down Expand Up @@ -739,11 +755,6 @@ export function createTaprootOutput(
};
}

export function setChain(c: Chain): void {
chain = c;
network = networks[chain];
}

// Utils

export function encodeVarUintLE(n: bigint | number): Buffer {
Expand Down

0 comments on commit bebde90

Please sign in to comment.