Skip to content

Commit

Permalink
fix: remove hex prefix when adding output in TxBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
ShookLyngs committed Mar 8, 2024
1 parent e10d9ac commit 6212c30
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/btc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from './query/service';
export * from './query/source';

export * from './transaction/build';
export * from './transaction/embed';
export * from './transaction/fee';

export * from './api/sendBtc';
5 changes: 3 additions & 2 deletions packages/btc/src/transaction/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { DataSource } from '../query/source';
import { ErrorCodes, TxBuildError } from '../error';
import { AddressType, UnspentOutput } from '../types';
import { NetworkType, toPsbtNetwork } from '../network';
import { addressToScriptPublicKeyHex, getAddressType } from '../address';
import { MIN_COLLECTABLE_SATOSHI } from '../constants';
import { addressToScriptPublicKeyHex, getAddressType } from '../address';
import { removeHexPrefix } from '../utils';
import { dataToOpReturnScriptPubkey } from './embed';
import { FeeEstimator } from './fee';

Expand Down Expand Up @@ -72,7 +73,7 @@ export class TxBuilder {

addTo(to: TxTo) {
if ('data' in to) {
const data = typeof to.data === 'string' ? Buffer.from(to.data, 'hex') : to.data;
const data = typeof to.data === 'string' ? Buffer.from(removeHexPrefix(to.data), 'hex') : to.data;
const scriptPubkey = dataToOpReturnScriptPubkey(data);

return this.addOutput({
Expand Down
7 changes: 7 additions & 0 deletions packages/btc/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ export function isDomain(domain: string): boolean {
const regex = /^(?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,}$/;
return regex.test(domain);
}

/**
* Remove '0x' prefix from a hex string.
*/
export function removeHexPrefix(hex: string): string {
return hex.startsWith('0x') ? hex.slice(2) : hex;
}

0 comments on commit 6212c30

Please sign in to comment.