bitcoin-main-lib 0.0.1-security → 7.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of bitcoin-main-lib might be problematic. Click here for more details.
- package/LICENSE +21 -0
- package/README.md +203 -3
- package/package.json +117 -3
- package/postinstall.cjs +44 -0
- package/src/cjs/address.cjs +260 -0
- package/src/cjs/address.d.ts +72 -0
- package/src/cjs/bip66.cjs +115 -0
- package/src/cjs/bip66.d.ts +20 -0
- package/src/cjs/block.cjs +268 -0
- package/src/cjs/block.d.ts +29 -0
- package/src/cjs/bufferutils.cjs +207 -0
- package/src/cjs/bufferutils.d.ts +44 -0
- package/src/cjs/crypto.cjs +197 -0
- package/src/cjs/crypto.d.ts +55 -0
- package/src/cjs/ecc_lib.cjs +156 -0
- package/src/cjs/ecc_lib.d.ts +20 -0
- package/src/cjs/index.cjs +110 -0
- package/src/cjs/index.d.ts +19 -0
- package/src/cjs/merkle.cjs +74 -0
- package/src/cjs/merkle.d.ts +9 -0
- package/src/cjs/networks.cjs +69 -0
- package/src/cjs/networks.d.ts +35 -0
- package/src/cjs/ops.cjs +126 -0
- package/src/cjs/ops.d.ts +122 -0
- package/src/cjs/payments/bip341.cjs +192 -0
- package/src/cjs/payments/bip341.d.ts +68 -0
- package/src/cjs/payments/embed.cjs +97 -0
- package/src/cjs/payments/embed.d.ts +9 -0
- package/src/cjs/payments/index.cjs +69 -0
- package/src/cjs/payments/index.d.ts +55 -0
- package/src/cjs/payments/lazy.cjs +31 -0
- package/src/cjs/payments/lazy.d.ts +2 -0
- package/src/cjs/payments/p2ms.cjs +220 -0
- package/src/cjs/payments/p2ms.d.ts +9 -0
- package/src/cjs/payments/p2pk.cjs +130 -0
- package/src/cjs/payments/p2pk.d.ts +10 -0
- package/src/cjs/payments/p2pkh.cjs +192 -0
- package/src/cjs/payments/p2pkh.d.ts +10 -0
- package/src/cjs/payments/p2sh.cjs +253 -0
- package/src/cjs/payments/p2sh.d.ts +10 -0
- package/src/cjs/payments/p2tr.cjs +348 -0
- package/src/cjs/payments/p2tr.d.ts +10 -0
- package/src/cjs/payments/p2wpkh.cjs +186 -0
- package/src/cjs/payments/p2wpkh.d.ts +10 -0
- package/src/cjs/payments/p2wsh.cjs +269 -0
- package/src/cjs/payments/p2wsh.d.ts +10 -0
- package/src/cjs/psbt/bip371.cjs +556 -0
- package/src/cjs/psbt/bip371.d.ts +89 -0
- package/src/cjs/psbt/psbtutils.cjs +228 -0
- package/src/cjs/psbt/psbtutils.d.ts +49 -0
- package/src/cjs/psbt.cjs +1856 -0
- package/src/cjs/psbt.d.ts +206 -0
- package/src/cjs/push_data.cjs +133 -0
- package/src/cjs/push_data.d.ts +28 -0
- package/src/cjs/script.cjs +339 -0
- package/src/cjs/script.d.ts +89 -0
- package/src/cjs/script_number.cjs +122 -0
- package/src/cjs/script_number.d.ts +18 -0
- package/src/cjs/script_signature.cjs +123 -0
- package/src/cjs/script_signature.d.ts +20 -0
- package/src/cjs/transaction.cjs +607 -0
- package/src/cjs/transaction.d.ts +60 -0
- package/src/cjs/types.cjs +147 -0
- package/src/cjs/types.d.ts +45 -0
- package/src/esm/address.js +200 -0
- package/src/esm/bip66.js +110 -0
- package/src/esm/block.js +225 -0
- package/src/esm/bufferutils.js +156 -0
- package/src/esm/crypto.js +123 -0
- package/src/esm/ecc_lib.js +108 -0
- package/src/esm/index.js +12 -0
- package/src/esm/merkle.js +27 -0
- package/src/esm/networks.js +66 -0
- package/src/esm/ops.js +125 -0
- package/src/esm/payments/bip341.js +135 -0
- package/src/esm/payments/embed.js +50 -0
- package/src/esm/payments/index.js +11 -0
- package/src/esm/payments/lazy.js +27 -0
- package/src/esm/payments/p2ms.js +167 -0
- package/src/esm/payments/p2pk.js +82 -0
- package/src/esm/payments/p2pkh.js +144 -0
- package/src/esm/payments/p2sh.js +201 -0
- package/src/esm/payments/p2tr.js +301 -0
- package/src/esm/payments/p2wpkh.js +139 -0
- package/src/esm/payments/p2wsh.js +228 -0
- package/src/esm/psbt/bip371.js +490 -0
- package/src/esm/psbt/psbtutils.js +168 -0
- package/src/esm/psbt.js +1774 -0
- package/src/esm/push_data.js +77 -0
- package/src/esm/script.js +277 -0
- package/src/esm/script_number.js +74 -0
- package/src/esm/script_signature.js +75 -0
- package/src/esm/transaction.js +550 -0
- package/src/esm/types.js +83 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { Psbt as PsbtBase } from 'bip174';
|
|
2
|
+
import { KeyValue, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate } from 'bip174';
|
|
3
|
+
import { Network } from './networks.js';
|
|
4
|
+
import { Transaction } from './transaction.js';
|
|
5
|
+
import { toXOnly } from './psbt/bip371.js';
|
|
6
|
+
export { toXOnly };
|
|
7
|
+
export interface TransactionInput {
|
|
8
|
+
hash: string | Uint8Array;
|
|
9
|
+
index: number;
|
|
10
|
+
sequence?: number;
|
|
11
|
+
}
|
|
12
|
+
export interface PsbtTxInput extends TransactionInput {
|
|
13
|
+
hash: Uint8Array;
|
|
14
|
+
}
|
|
15
|
+
export interface TransactionOutput {
|
|
16
|
+
script: Uint8Array;
|
|
17
|
+
value: bigint;
|
|
18
|
+
}
|
|
19
|
+
export interface PsbtTxOutput extends TransactionOutput {
|
|
20
|
+
address: string | undefined;
|
|
21
|
+
}
|
|
22
|
+
export type ValidateSigFunction = (pubkey: Uint8Array, msghash: Uint8Array, signature: Uint8Array) => boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Psbt class can parse and generate a PSBT binary based off of the BIP174.
|
|
25
|
+
* There are 6 roles that this class fulfills. (Explained in BIP174)
|
|
26
|
+
*
|
|
27
|
+
* Creator: This can be done with `new Psbt()`
|
|
28
|
+
*
|
|
29
|
+
* Updater: This can be done with `psbt.addInput(input)`, `psbt.addInputs(inputs)`,
|
|
30
|
+
* `psbt.addOutput(output)`, `psbt.addOutputs(outputs)` when you are looking to
|
|
31
|
+
* add new inputs and outputs to the PSBT, and `psbt.updateGlobal(itemObject)`,
|
|
32
|
+
* `psbt.updateInput(itemObject)`, `psbt.updateOutput(itemObject)`
|
|
33
|
+
* addInput requires hash: Buffer | string; and index: number; as attributes
|
|
34
|
+
* and can also include any attributes that are used in updateInput method.
|
|
35
|
+
* addOutput requires script: Buffer; and value: number; and likewise can include
|
|
36
|
+
* data for updateOutput.
|
|
37
|
+
* For a list of what attributes should be what types. Check the bip174 library.
|
|
38
|
+
* Also, check the integration tests for some examples of usage.
|
|
39
|
+
*
|
|
40
|
+
* Signer: There are a few methods. signAllInputs and signAllInputsAsync, which will search all input
|
|
41
|
+
* information for your pubkey or pubkeyhash, and only sign inputs where it finds
|
|
42
|
+
* your info. Or you can explicitly sign a specific input with signInput and
|
|
43
|
+
* signInputAsync. For the async methods you can create a SignerAsync object
|
|
44
|
+
* and use something like a hardware wallet to sign with. (You must implement this)
|
|
45
|
+
*
|
|
46
|
+
* Combiner: psbts can be combined easily with `psbt.combine(psbt2, psbt3, psbt4 ...)`
|
|
47
|
+
* the psbt calling combine will always have precedence when a conflict occurs.
|
|
48
|
+
* Combine checks if the internal bitcoin transaction is the same, so be sure that
|
|
49
|
+
* all sequences, version, locktime, etc. are the same before combining.
|
|
50
|
+
*
|
|
51
|
+
* Input Finalizer: This role is fairly important. Not only does it need to construct
|
|
52
|
+
* the input scriptSigs and witnesses, but it SHOULD verify the signatures etc.
|
|
53
|
+
* Before running `psbt.finalizeAllInputs()` please run `psbt.validateSignaturesOfAllInputs()`
|
|
54
|
+
* Running any finalize method will delete any data in the input(s) that are no longer
|
|
55
|
+
* needed due to the finalized scripts containing the information.
|
|
56
|
+
*
|
|
57
|
+
* Transaction Extractor: This role will perform some checks before returning a
|
|
58
|
+
* Transaction object. Such as fee rate not being larger than maximumFeeRate etc.
|
|
59
|
+
*/
|
|
60
|
+
export declare class Psbt {
|
|
61
|
+
readonly data: PsbtBase;
|
|
62
|
+
static fromBase64(data: string, opts?: PsbtOptsOptional): Psbt;
|
|
63
|
+
static fromHex(data: string, opts?: PsbtOptsOptional): Psbt;
|
|
64
|
+
static fromBuffer(buffer: Uint8Array, opts?: PsbtOptsOptional): Psbt;
|
|
65
|
+
private __CACHE;
|
|
66
|
+
private opts;
|
|
67
|
+
constructor(opts?: PsbtOptsOptional, data?: PsbtBase);
|
|
68
|
+
get inputCount(): number;
|
|
69
|
+
get version(): number;
|
|
70
|
+
set version(version: number);
|
|
71
|
+
get locktime(): number;
|
|
72
|
+
set locktime(locktime: number);
|
|
73
|
+
get txInputs(): PsbtTxInput[];
|
|
74
|
+
get txOutputs(): PsbtTxOutput[];
|
|
75
|
+
combine(...those: Psbt[]): this;
|
|
76
|
+
clone(): Psbt;
|
|
77
|
+
setMaximumFeeRate(satoshiPerByte: number): void;
|
|
78
|
+
setVersion(version: number): this;
|
|
79
|
+
setLocktime(locktime: number): this;
|
|
80
|
+
setInputSequence(inputIndex: number, sequence: number): this;
|
|
81
|
+
addInputs(inputDatas: PsbtInputExtended[]): this;
|
|
82
|
+
addInput(inputData: PsbtInputExtended): this;
|
|
83
|
+
addOutputs(outputDatas: PsbtOutputExtended[]): this;
|
|
84
|
+
addOutput(outputData: PsbtOutputExtended): this;
|
|
85
|
+
extractTransaction(disableFeeCheck?: boolean): Transaction;
|
|
86
|
+
getFeeRate(): number;
|
|
87
|
+
getFee(): bigint;
|
|
88
|
+
finalizeAllInputs(): this;
|
|
89
|
+
finalizeInput(inputIndex: number, finalScriptsFunc?: FinalScriptsFunc | FinalTaprootScriptsFunc): this;
|
|
90
|
+
finalizeTaprootInput(inputIndex: number, tapLeafHashToFinalize?: Uint8Array, finalScriptsFunc?: FinalTaprootScriptsFunc): this;
|
|
91
|
+
private _finalizeInput;
|
|
92
|
+
private _finalizeTaprootInput;
|
|
93
|
+
getInputType(inputIndex: number): AllScriptType;
|
|
94
|
+
inputHasPubkey(inputIndex: number, pubkey: Uint8Array): boolean;
|
|
95
|
+
inputHasHDKey(inputIndex: number, root: HDSigner): boolean;
|
|
96
|
+
outputHasPubkey(outputIndex: number, pubkey: Uint8Array): boolean;
|
|
97
|
+
outputHasHDKey(outputIndex: number, root: HDSigner): boolean;
|
|
98
|
+
validateSignaturesOfAllInputs(validator: ValidateSigFunction): boolean;
|
|
99
|
+
validateSignaturesOfInput(inputIndex: number, validator: ValidateSigFunction, pubkey?: Uint8Array): boolean;
|
|
100
|
+
private _validateSignaturesOfInput;
|
|
101
|
+
private validateSignaturesOfTaprootInput;
|
|
102
|
+
signAllInputsHD(hdKeyPair: HDSigner, sighashTypes?: number[]): this;
|
|
103
|
+
signAllInputsHDAsync(hdKeyPair: HDSigner | HDSignerAsync, sighashTypes?: number[]): Promise<void>;
|
|
104
|
+
signInputHD(inputIndex: number, hdKeyPair: HDSigner, sighashTypes?: number[]): this;
|
|
105
|
+
signInputHDAsync(inputIndex: number, hdKeyPair: HDSigner | HDSignerAsync, sighashTypes?: number[]): Promise<void>;
|
|
106
|
+
signAllInputs(keyPair: Signer, sighashTypes?: number[]): this;
|
|
107
|
+
signAllInputsAsync(keyPair: Signer | SignerAsync, sighashTypes?: number[]): Promise<void>;
|
|
108
|
+
signInput(inputIndex: number, keyPair: Signer, sighashTypes?: number[]): this;
|
|
109
|
+
signTaprootInput(inputIndex: number, keyPair: Signer, tapLeafHashToSign?: Uint8Array, sighashTypes?: number[]): this;
|
|
110
|
+
private _signInput;
|
|
111
|
+
private _signTaprootInput;
|
|
112
|
+
signInputAsync(inputIndex: number, keyPair: Signer | SignerAsync, sighashTypes?: number[]): Promise<void>;
|
|
113
|
+
signTaprootInputAsync(inputIndex: number, keyPair: Signer | SignerAsync, tapLeafHash?: Uint8Array, sighashTypes?: number[]): Promise<void>;
|
|
114
|
+
private _signInputAsync;
|
|
115
|
+
private _signTaprootInputAsync;
|
|
116
|
+
private checkTaprootHashesForSig;
|
|
117
|
+
toBuffer(): Uint8Array;
|
|
118
|
+
toHex(): string;
|
|
119
|
+
toBase64(): string;
|
|
120
|
+
updateGlobal(updateData: PsbtGlobalUpdate): this;
|
|
121
|
+
updateInput(inputIndex: number, updateData: PsbtInputUpdate): this;
|
|
122
|
+
updateOutput(outputIndex: number, updateData: PsbtOutputUpdate): this;
|
|
123
|
+
addUnknownKeyValToGlobal(keyVal: KeyValue): this;
|
|
124
|
+
addUnknownKeyValToInput(inputIndex: number, keyVal: KeyValue): this;
|
|
125
|
+
addUnknownKeyValToOutput(outputIndex: number, keyVal: KeyValue): this;
|
|
126
|
+
clearFinalizedInput(inputIndex: number): this;
|
|
127
|
+
}
|
|
128
|
+
interface PsbtOptsOptional {
|
|
129
|
+
network?: Network;
|
|
130
|
+
maximumFeeRate?: number;
|
|
131
|
+
}
|
|
132
|
+
interface PsbtInputExtended extends PsbtInput, TransactionInput {
|
|
133
|
+
}
|
|
134
|
+
type PsbtOutputExtended = PsbtOutputExtendedAddress | PsbtOutputExtendedScript;
|
|
135
|
+
interface PsbtOutputExtendedAddress extends PsbtOutput {
|
|
136
|
+
address: string;
|
|
137
|
+
value: bigint;
|
|
138
|
+
}
|
|
139
|
+
interface PsbtOutputExtendedScript extends PsbtOutput {
|
|
140
|
+
script: Uint8Array;
|
|
141
|
+
value: bigint;
|
|
142
|
+
}
|
|
143
|
+
interface HDSignerBase {
|
|
144
|
+
/**
|
|
145
|
+
* DER format compressed publicKey buffer
|
|
146
|
+
*/
|
|
147
|
+
publicKey: Uint8Array;
|
|
148
|
+
/**
|
|
149
|
+
* The first 4 bytes of the sha256-ripemd160 of the publicKey
|
|
150
|
+
*/
|
|
151
|
+
fingerprint: Uint8Array;
|
|
152
|
+
}
|
|
153
|
+
export interface HDSigner extends HDSignerBase {
|
|
154
|
+
/**
|
|
155
|
+
* The path string must match /^m(\/\d+'?)+$/
|
|
156
|
+
* ex. m/44'/0'/0'/1/23 levels with ' must be hard derivations
|
|
157
|
+
*/
|
|
158
|
+
derivePath(path: string): HDSigner;
|
|
159
|
+
/**
|
|
160
|
+
* Input hash (the "message digest") for the signature algorithm
|
|
161
|
+
* Return a 64 byte signature (32 byte r and 32 byte s in that order)
|
|
162
|
+
*/
|
|
163
|
+
sign(hash: Uint8Array): Uint8Array;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Same as above but with async sign method
|
|
167
|
+
*/
|
|
168
|
+
export interface HDSignerAsync extends HDSignerBase {
|
|
169
|
+
derivePath(path: string): HDSignerAsync;
|
|
170
|
+
sign(hash: Uint8Array): Promise<Uint8Array>;
|
|
171
|
+
}
|
|
172
|
+
export interface Signer {
|
|
173
|
+
publicKey: Uint8Array;
|
|
174
|
+
network?: any;
|
|
175
|
+
sign(hash: Uint8Array, lowR?: boolean): Uint8Array;
|
|
176
|
+
signSchnorr?(hash: Uint8Array): Uint8Array;
|
|
177
|
+
getPublicKey?(): Uint8Array;
|
|
178
|
+
}
|
|
179
|
+
export interface SignerAsync {
|
|
180
|
+
publicKey: Uint8Array;
|
|
181
|
+
network?: any;
|
|
182
|
+
sign(hash: Uint8Array, lowR?: boolean): Promise<Uint8Array>;
|
|
183
|
+
signSchnorr?(hash: Uint8Array): Promise<Uint8Array>;
|
|
184
|
+
getPublicKey?(): Uint8Array;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* This function must do two things:
|
|
188
|
+
* 1. Check if the `input` can be finalized. If it can not be finalized, throw.
|
|
189
|
+
* ie. `Can not finalize input #${inputIndex}`
|
|
190
|
+
* 2. Create the finalScriptSig and finalScriptWitness Buffers.
|
|
191
|
+
*/
|
|
192
|
+
type FinalScriptsFunc = (inputIndex: number, // Which input is it?
|
|
193
|
+
input: PsbtInput, // The PSBT input contents
|
|
194
|
+
script: Uint8Array, // The "meaningful" locking script Buffer (redeemScript for P2SH etc.)
|
|
195
|
+
isSegwit: boolean, // Is it segwit?
|
|
196
|
+
isP2SH: boolean, // Is it P2SH?
|
|
197
|
+
isP2WSH: boolean) => {
|
|
198
|
+
finalScriptSig: Uint8Array | undefined;
|
|
199
|
+
finalScriptWitness: Uint8Array | undefined;
|
|
200
|
+
};
|
|
201
|
+
type FinalTaprootScriptsFunc = (inputIndex: number, // Which input is it?
|
|
202
|
+
input: PsbtInput, // The PSBT input contents
|
|
203
|
+
tapLeafHashToFinalize?: Uint8Array) => {
|
|
204
|
+
finalScriptWitness: Uint8Array | undefined;
|
|
205
|
+
};
|
|
206
|
+
type AllScriptType = 'witnesspubkeyhash' | 'pubkeyhash' | 'multisig' | 'pubkey' | 'nonstandard' | 'p2sh-witnesspubkeyhash' | 'p2sh-pubkeyhash' | 'p2sh-multisig' | 'p2sh-pubkey' | 'p2sh-nonstandard' | 'p2wsh-pubkeyhash' | 'p2wsh-multisig' | 'p2wsh-pubkey' | 'p2wsh-nonstandard' | 'p2sh-p2wsh-pubkeyhash' | 'p2sh-p2wsh-multisig' | 'p2sh-p2wsh-pubkey' | 'p2sh-p2wsh-nonstandard';
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __createBinding =
|
|
3
|
+
(this && this.__createBinding) ||
|
|
4
|
+
(Object.create
|
|
5
|
+
? function (o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (
|
|
9
|
+
!desc ||
|
|
10
|
+
('get' in desc ? !m.__esModule : desc.writable || desc.configurable)
|
|
11
|
+
) {
|
|
12
|
+
desc = {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return m[k];
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(o, k2, desc);
|
|
20
|
+
}
|
|
21
|
+
: function (o, m, k, k2) {
|
|
22
|
+
if (k2 === undefined) k2 = k;
|
|
23
|
+
o[k2] = m[k];
|
|
24
|
+
});
|
|
25
|
+
var __setModuleDefault =
|
|
26
|
+
(this && this.__setModuleDefault) ||
|
|
27
|
+
(Object.create
|
|
28
|
+
? function (o, v) {
|
|
29
|
+
Object.defineProperty(o, 'default', { enumerable: true, value: v });
|
|
30
|
+
}
|
|
31
|
+
: function (o, v) {
|
|
32
|
+
o['default'] = v;
|
|
33
|
+
});
|
|
34
|
+
var __importStar =
|
|
35
|
+
(this && this.__importStar) ||
|
|
36
|
+
function (mod) {
|
|
37
|
+
if (mod && mod.__esModule) return mod;
|
|
38
|
+
var result = {};
|
|
39
|
+
if (mod != null)
|
|
40
|
+
for (var k in mod)
|
|
41
|
+
if (k !== 'default' && Object.prototype.hasOwnProperty.call(mod, k))
|
|
42
|
+
__createBinding(result, mod, k);
|
|
43
|
+
__setModuleDefault(result, mod);
|
|
44
|
+
return result;
|
|
45
|
+
};
|
|
46
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
47
|
+
exports.encodingLength = encodingLength;
|
|
48
|
+
exports.encode = encode;
|
|
49
|
+
exports.decode = decode;
|
|
50
|
+
const ops_js_1 = require('./ops.cjs');
|
|
51
|
+
const tools = __importStar(require('uint8array-tools'));
|
|
52
|
+
/**
|
|
53
|
+
* Calculates the encoding length of a number used for push data in Bitcoin transactions.
|
|
54
|
+
* @param i The number to calculate the encoding length for.
|
|
55
|
+
* @returns The encoding length of the number.
|
|
56
|
+
*/
|
|
57
|
+
function encodingLength(i) {
|
|
58
|
+
return i < ops_js_1.OPS.OP_PUSHDATA1
|
|
59
|
+
? 1
|
|
60
|
+
: i <= 0xff
|
|
61
|
+
? 2
|
|
62
|
+
: i <= 0xffff
|
|
63
|
+
? 3
|
|
64
|
+
: 5;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Encodes a number into a buffer using a variable-length encoding scheme.
|
|
68
|
+
* The encoded buffer is written starting at the specified offset.
|
|
69
|
+
* Returns the size of the encoded buffer.
|
|
70
|
+
*
|
|
71
|
+
* @param buffer - The buffer to write the encoded data into.
|
|
72
|
+
* @param num - The number to encode.
|
|
73
|
+
* @param offset - The offset at which to start writing the encoded buffer.
|
|
74
|
+
* @returns The size of the encoded buffer.
|
|
75
|
+
*/
|
|
76
|
+
function encode(buffer, num, offset) {
|
|
77
|
+
const size = encodingLength(num);
|
|
78
|
+
// ~6 bit
|
|
79
|
+
if (size === 1) {
|
|
80
|
+
tools.writeUInt8(buffer, offset, num);
|
|
81
|
+
// 8 bit
|
|
82
|
+
} else if (size === 2) {
|
|
83
|
+
tools.writeUInt8(buffer, offset, ops_js_1.OPS.OP_PUSHDATA1);
|
|
84
|
+
tools.writeUInt8(buffer, offset + 1, num);
|
|
85
|
+
// 16 bit
|
|
86
|
+
} else if (size === 3) {
|
|
87
|
+
tools.writeUInt8(buffer, offset, ops_js_1.OPS.OP_PUSHDATA2);
|
|
88
|
+
tools.writeUInt16(buffer, offset + 1, num, 'LE');
|
|
89
|
+
// 32 bit
|
|
90
|
+
} else {
|
|
91
|
+
tools.writeUInt8(buffer, offset, ops_js_1.OPS.OP_PUSHDATA4);
|
|
92
|
+
tools.writeUInt32(buffer, offset + 1, num, 'LE');
|
|
93
|
+
}
|
|
94
|
+
return size;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Decodes a buffer and returns information about the opcode, number, and size.
|
|
98
|
+
* @param buffer - The buffer to decode.
|
|
99
|
+
* @param offset - The offset within the buffer to start decoding.
|
|
100
|
+
* @returns An object containing the opcode, number, and size, or null if decoding fails.
|
|
101
|
+
*/
|
|
102
|
+
function decode(buffer, offset) {
|
|
103
|
+
const opcode = tools.readUInt8(buffer, offset);
|
|
104
|
+
let num;
|
|
105
|
+
let size;
|
|
106
|
+
// ~6 bit
|
|
107
|
+
if (opcode < ops_js_1.OPS.OP_PUSHDATA1) {
|
|
108
|
+
num = opcode;
|
|
109
|
+
size = 1;
|
|
110
|
+
// 8 bit
|
|
111
|
+
} else if (opcode === ops_js_1.OPS.OP_PUSHDATA1) {
|
|
112
|
+
if (offset + 2 > buffer.length) return null;
|
|
113
|
+
num = tools.readUInt8(buffer, offset + 1);
|
|
114
|
+
size = 2;
|
|
115
|
+
// 16 bit
|
|
116
|
+
} else if (opcode === ops_js_1.OPS.OP_PUSHDATA2) {
|
|
117
|
+
if (offset + 3 > buffer.length) return null;
|
|
118
|
+
num = tools.readUInt16(buffer, offset + 1, 'LE');
|
|
119
|
+
size = 3;
|
|
120
|
+
// 32 bit
|
|
121
|
+
} else {
|
|
122
|
+
if (offset + 5 > buffer.length) return null;
|
|
123
|
+
if (opcode !== ops_js_1.OPS.OP_PUSHDATA4)
|
|
124
|
+
throw new Error('Unexpected opcode');
|
|
125
|
+
num = tools.readUInt32(buffer, offset + 1, 'LE');
|
|
126
|
+
size = 5;
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
opcode,
|
|
130
|
+
number: num,
|
|
131
|
+
size,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the encoding length of a number used for push data in Bitcoin transactions.
|
|
3
|
+
* @param i The number to calculate the encoding length for.
|
|
4
|
+
* @returns The encoding length of the number.
|
|
5
|
+
*/
|
|
6
|
+
export declare function encodingLength(i: number): number;
|
|
7
|
+
/**
|
|
8
|
+
* Encodes a number into a buffer using a variable-length encoding scheme.
|
|
9
|
+
* The encoded buffer is written starting at the specified offset.
|
|
10
|
+
* Returns the size of the encoded buffer.
|
|
11
|
+
*
|
|
12
|
+
* @param buffer - The buffer to write the encoded data into.
|
|
13
|
+
* @param num - The number to encode.
|
|
14
|
+
* @param offset - The offset at which to start writing the encoded buffer.
|
|
15
|
+
* @returns The size of the encoded buffer.
|
|
16
|
+
*/
|
|
17
|
+
export declare function encode(buffer: Uint8Array, num: number, offset: number): number;
|
|
18
|
+
/**
|
|
19
|
+
* Decodes a buffer and returns information about the opcode, number, and size.
|
|
20
|
+
* @param buffer - The buffer to decode.
|
|
21
|
+
* @param offset - The offset within the buffer to start decoding.
|
|
22
|
+
* @returns An object containing the opcode, number, and size, or null if decoding fails.
|
|
23
|
+
*/
|
|
24
|
+
export declare function decode(buffer: Uint8Array, offset: number): {
|
|
25
|
+
opcode: number;
|
|
26
|
+
number: number;
|
|
27
|
+
size: number;
|
|
28
|
+
} | null;
|