@caravan/psbt 1.2.0 → 1.3.1

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.
@@ -1,111 +0,0 @@
1
- import { BufferReader, BufferWriter } from "bufio";
2
-
3
- import { bufferize, readAndSetKeyPairs, serializeMap } from "./functions";
4
- import { Key, KeyType, Value } from "./types";
5
- import { PSBT_MAGIC_BYTES } from "../psbt";
6
- import { PsbtV2 } from "./psbtv2";
7
-
8
- /**
9
- * This abstract class is provided for utility to allow for mapping, map
10
- * copying, and serialization operations for psbts. This does almost no
11
- * validation, so do not rely on it for ensuring a valid psbt.
12
- */
13
- export abstract class PsbtV2Maps {
14
- // These maps directly correspond to the maps defined in BIP0174 and extended
15
- // in BIP0370
16
- protected globalMap: Map<Key, Value> = new Map();
17
- protected inputMaps: Map<Key, Value>[] = [];
18
- protected outputMaps: Map<Key, Value>[] = [];
19
-
20
- constructor(psbt?: Buffer | string) {
21
- if (!psbt) {
22
- return;
23
- }
24
-
25
- const buf = bufferize(psbt);
26
- const br = new BufferReader(buf);
27
- if (!br.readBytes(PSBT_MAGIC_BYTES.length, true).equals(PSBT_MAGIC_BYTES)) {
28
- throw Error("PsbtV2 magic bytes are incorrect.");
29
- }
30
- // Build globalMap
31
- readAndSetKeyPairs(this.globalMap, br);
32
- if (
33
- // Assuming that psbt being passed in is a valid psbtv2
34
- !this.globalMap.has(KeyType.PSBT_GLOBAL_VERSION) ||
35
- !this.globalMap.has(KeyType.PSBT_GLOBAL_TX_VERSION) ||
36
- !this.globalMap.has(KeyType.PSBT_GLOBAL_INPUT_COUNT) ||
37
- !this.globalMap.has(KeyType.PSBT_GLOBAL_OUTPUT_COUNT) ||
38
- this.globalMap.has("00") // PsbtV2 must exclude key 0x00
39
- ) {
40
- throw Error("Provided PsbtV2 not valid. Missing required global keys.");
41
- }
42
-
43
- // Build inputMaps
44
- const inputCount =
45
- this.globalMap.get(KeyType.PSBT_GLOBAL_INPUT_COUNT)?.readUInt8(0) ?? 0;
46
- for (let i = 0; i < inputCount; i++) {
47
- const map = new Map<Key, Value>();
48
- readAndSetKeyPairs(map, br);
49
- this.inputMaps.push(map);
50
- }
51
-
52
- // Build outputMaps
53
- const outputCount =
54
- this.globalMap.get(KeyType.PSBT_GLOBAL_OUTPUT_COUNT)?.readUInt8(0) ?? 0;
55
- for (let i = 0; i < outputCount; i++) {
56
- const map = new Map<Key, Value>();
57
- readAndSetKeyPairs(map, br);
58
- this.outputMaps.push(map);
59
- }
60
- }
61
-
62
- /**
63
- * Return the current state of the psbt as a string in the specified format.
64
- */
65
- public serialize(format: "base64" | "hex" = "base64"): string {
66
- // Build hex string from maps
67
- const bw = new BufferWriter();
68
- bw.writeBytes(PSBT_MAGIC_BYTES);
69
- serializeMap(this.globalMap, bw);
70
-
71
- for (const map of this.inputMaps) {
72
- serializeMap(map, bw);
73
- }
74
-
75
- for (const map of this.outputMaps) {
76
- serializeMap(map, bw);
77
- }
78
-
79
- return bw.render().toString(format);
80
- }
81
-
82
- /**
83
- * Copies the maps in this PsbtV2 object to another PsbtV2 object.
84
- *
85
- * NOTE: This copy method is made available to achieve parity with the PSBT
86
- * api required by `ledger-bitcoin` for creating merklized PSBTs. HOWEVER, it
87
- * is not recommended to use this when avoidable as copying maps bypasses the
88
- * validation defined in the constructor, so it could create a psbtv2 in an
89
- * invalid psbt state. PsbtV2.serialize is preferable whenever possible.
90
- */
91
- public copy(to: PsbtV2) {
92
- this.copyMap(this.globalMap, to.globalMap);
93
- this.copyMaps(this.inputMaps, to.inputMaps);
94
- this.copyMaps(this.outputMaps, to.outputMaps);
95
- }
96
-
97
- private copyMaps(
98
- from: readonly ReadonlyMap<string, Buffer>[],
99
- to: Map<string, Buffer>[],
100
- ) {
101
- from.forEach((m, index) => {
102
- const to_index = new Map<Key, Value>();
103
- this.copyMap(m, to_index);
104
- to[index] = to_index;
105
- });
106
- }
107
-
108
- private copyMap(from: ReadonlyMap<string, Buffer>, to: Map<string, Buffer>) {
109
- from.forEach((v, k) => to.set(k, Buffer.from(v)));
110
- }
111
- }
@@ -1,91 +0,0 @@
1
- /**
2
- * Hex encoded string containing `<keytype><keydata>`. A string is needed for
3
- * Map.get() since it matches by identity. Most commonly, a `Key` only contains a
4
- * keytype byte, however, some with keydata can allow for multiple unique keys
5
- * of the same type.
6
- */
7
- export type Key = string;
8
-
9
- /**
10
- * Values can be of various different types or formats. Here we leave them as
11
- * Buffers so that getters can decide how they should be formatted.
12
- */
13
- export type Value = Buffer;
14
-
15
- export type NonUniqueKeyTypeValue = { key: string; value: string | null };
16
-
17
- /**
18
- * KeyTypes are hex bytes, but within this module are used as string enums to
19
- * assist in Map lookups. See type `Key` for more info.
20
- */
21
- export enum KeyType {
22
- PSBT_GLOBAL_XPUB = "01",
23
- PSBT_GLOBAL_TX_VERSION = "02",
24
- PSBT_GLOBAL_FALLBACK_LOCKTIME = "03",
25
- PSBT_GLOBAL_INPUT_COUNT = "04",
26
- PSBT_GLOBAL_OUTPUT_COUNT = "05",
27
- PSBT_GLOBAL_TX_MODIFIABLE = "06",
28
- PSBT_GLOBAL_VERSION = "fb",
29
- PSBT_GLOBAL_PROPRIETARY = "fc",
30
-
31
- PSBT_IN_NON_WITNESS_UTXO = "00",
32
- PSBT_IN_WITNESS_UTXO = "01",
33
- PSBT_IN_PARTIAL_SIG = "02",
34
- PSBT_IN_SIGHASH_TYPE = "03",
35
- PSBT_IN_REDEEM_SCRIPT = "04",
36
- PSBT_IN_WITNESS_SCRIPT = "05",
37
- PSBT_IN_BIP32_DERIVATION = "06",
38
- PSBT_IN_FINAL_SCRIPTSIG = "07",
39
- PSBT_IN_FINAL_SCRIPTWITNESS = "08",
40
- PSBT_IN_POR_COMMITMENT = "09",
41
- PSBT_IN_RIPEMD160 = "0a",
42
- PSBT_IN_SHA256 = "0b",
43
- PSBT_IN_HASH160 = "0c",
44
- PSBT_IN_HASH256 = "0d",
45
- PSBT_IN_PREVIOUS_TXID = "0e",
46
- PSBT_IN_OUTPUT_INDEX = "0f",
47
- PSBT_IN_SEQUENCE = "10",
48
- PSBT_IN_REQUIRED_TIME_LOCKTIME = "11",
49
- PSBT_IN_REQUIRED_HEIGHT_LOCKTIME = "12",
50
- PSBT_IN_TAP_KEY_SIG = "13",
51
- PSBT_IN_TAP_SCRIPT_SIG = "14",
52
- PSBT_IN_TAP_LEAF_SCRIPT = "15",
53
- PSBT_IN_TAP_BIP32_DERIVATION = "16",
54
- PSBT_IN_TAP_INTERNAL_KEY = "17",
55
- PSBT_IN_TAP_MERKLE_ROOT = "18",
56
- PSBT_IN_PROPRIETARY = "fc",
57
-
58
- PSBT_OUT_REDEEM_SCRIPT = "00",
59
- PSBT_OUT_WITNESS_SCRIPT = "01",
60
- PSBT_OUT_BIP32_DERIVATION = "02",
61
- PSBT_OUT_AMOUNT = "03",
62
- PSBT_OUT_SCRIPT = "04",
63
- PSBT_OUT_TAP_INTERNAL_KEY = "05",
64
- PSBT_OUT_TAP_TREE = "06",
65
- PSBT_OUT_TAP_BIP32_DERIVATION = "07",
66
- PSBT_OUT_PROPRIETARY = "fc",
67
- }
68
-
69
- /**
70
- * Provided to friendly-format the `PSBT_GLOBAL_TX_MODIFIABLE` bitmask from
71
- * `PsbtV2.PSBT_GLOBAL_TX_MODIFIABLE` which returns
72
- * `PsbtGlobalTxModifiableBits[]`.
73
- */
74
- export enum PsbtGlobalTxModifiableBits {
75
- INPUTS = "INPUTS", // 0b00000001
76
- OUTPUTS = "OUTPUTS", // 0b00000010
77
- SIGHASH_SINGLE = "SIGHASH_SINGLE", // 0b00000100
78
- }
79
-
80
- export enum SighashType {
81
- SIGHASH_ALL = 0x01,
82
- SIGHASH_NONE = 0x02,
83
- SIGHASH_SINGLE = 0x03,
84
- SIGHASH_ANYONECANPAY = 0x80,
85
- }
86
-
87
- type InputOutputIndexType = number;
88
- export type MapSelectorType =
89
- | "global"
90
- | ["inputs", InputOutputIndexType]
91
- | ["outputs", InputOutputIndexType];
@@ -1,4 +0,0 @@
1
- export { PSBT_MAGIC_BYTES } from "../psbt";
2
- export const PSBT_MAP_SEPARATOR = Buffer.from([0x00]);
3
- export const BIP_32_NODE_REGEX = /(\/[0-9]+'?)/gi;
4
- export const BIP_32_HARDENING_OFFSET = 0x80000000;
package/tsconfig.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "extends": "@caravan/typescript-config/base.json"
3
- }
package/tsup.config.ts DELETED
@@ -1,6 +0,0 @@
1
- import { defineConfig } from 'tsup'
2
- import { polyfillNode } from "esbuild-plugin-polyfill-node";
3
-
4
- export default defineConfig({
5
- esbuildPlugins: [polyfillNode()],
6
- })