@exodus/bip322-js 1.1.0-exodus.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.
@@ -0,0 +1,27 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * Class that implement BIP137-related utility functions.
4
+ */
5
+ declare class BIP137 {
6
+ /**
7
+ * Check if a given signature satisified the format of a BIP-137 signature.
8
+ * @param signature Base64-encoded signature to be checked
9
+ * @returns True if the provided signature correspond to a valid BIP-137 signature, false if otherwise
10
+ */
11
+ static isBIP137Signature(signature: string): boolean;
12
+ /**
13
+ * Derive the public key that signed a valid BIP-137 signature.
14
+ * @param message Message signed by the signature
15
+ * @param signature Base-64 encoded signature to be decoded
16
+ * @returns Public key that signs the provided signature
17
+ */
18
+ static derivePubKey(message: string, signature: string): Buffer;
19
+ /**
20
+ * Decode a BIP-137 signature.
21
+ * Function copied from bitcoinjs-message library.
22
+ * @param signature BIP-137 signature to be decoded
23
+ * @returns Decoded BIP-137 signature
24
+ */
25
+ private static decodeSignature;
26
+ }
27
+ export default BIP137;
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ // Import dependencies
30
+ const secp256k1_1 = __importDefault(require("@exodus/secp256k1"));
31
+ const bitcoinMessage = __importStar(require("bitcoinjs-message"));
32
+ /**
33
+ * Class that implement BIP137-related utility functions.
34
+ */
35
+ class BIP137 {
36
+ /**
37
+ * Check if a given signature satisified the format of a BIP-137 signature.
38
+ * @param signature Base64-encoded signature to be checked
39
+ * @returns True if the provided signature correspond to a valid BIP-137 signature, false if otherwise
40
+ */
41
+ static isBIP137Signature(signature) {
42
+ // Check if the provided signature satisified the format of a BIP-137 signature
43
+ const signatureBuffer = Buffer.from(signature, 'base64');
44
+ if (signatureBuffer.byteLength === 65) {
45
+ return true;
46
+ }
47
+ else {
48
+ return false;
49
+ }
50
+ }
51
+ /**
52
+ * Derive the public key that signed a valid BIP-137 signature.
53
+ * @param message Message signed by the signature
54
+ * @param signature Base-64 encoded signature to be decoded
55
+ * @returns Public key that signs the provided signature
56
+ */
57
+ static derivePubKey(message, signature) {
58
+ // Compute the hash signed by the signer
59
+ const messageHash = bitcoinMessage.magicHash(message);
60
+ // Decode the provided BIP-137 signature
61
+ const signatureDecoded = this.decodeSignature(Buffer.from(signature, 'base64'));
62
+ // Recover the public key
63
+ return Buffer.from(secp256k1_1.default.ecdsaRecover(signatureDecoded.signature, signatureDecoded.recovery, messageHash, signatureDecoded.compressed));
64
+ }
65
+ /**
66
+ * Decode a BIP-137 signature.
67
+ * Function copied from bitcoinjs-message library.
68
+ * @param signature BIP-137 signature to be decoded
69
+ * @returns Decoded BIP-137 signature
70
+ */
71
+ static decodeSignature(signature) {
72
+ if (signature.length !== 65)
73
+ throw new Error('Invalid signature length');
74
+ const flagByte = signature.readUInt8(0) - 27;
75
+ if (flagByte > 19 || flagByte < 0) {
76
+ throw new Error('Invalid signature parameter');
77
+ }
78
+ return {
79
+ compressed: !!(flagByte & 12),
80
+ recovery: flagByte & 3,
81
+ signature: signature.subarray(1)
82
+ };
83
+ }
84
+ }
85
+ exports.default = BIP137;
86
+ //# sourceMappingURL=BIP137.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BIP137.js","sourceRoot":"","sources":["../../src/helpers/BIP137.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sBAAsB;AACtB,kEAAoC;AACpC,kEAAoD;AAEpD;;GAEG;AACH,MAAM,MAAM;IAER;;;;OAIG;IACI,MAAM,CAAC,iBAAiB,CAAC,SAAiB;QAC7C,+EAA+E;QAC/E,MAAM,eAAe,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;QACzD,IAAI,eAAe,CAAC,UAAU,KAAK,EAAE,EAAE;YACnC,OAAO,IAAI,CAAC;SACf;aACI;YACD,OAAO,KAAK,CAAC;SAChB;IACL,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,YAAY,CAAC,OAAe,EAAE,SAAiB;QACzD,wCAAwC;QACxC,MAAM,WAAW,GAAG,cAAc,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;QACtD,wCAAwC;QACxC,MAAM,gBAAgB,GAAG,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC,CAAC;QAChF,yBAAyB;QACzB,OAAO,MAAM,CAAC,IAAI,CAAC,mBAAG,CAAC,YAAY,CAAC,gBAAgB,CAAC,SAAS,EAAE,gBAAgB,CAAC,QAAQ,EAAE,WAAW,EAAE,gBAAgB,CAAC,UAAU,CAAC,CAAC,CAAC;IAC1I,CAAC;IAED;;;;;OAKG;IACK,MAAM,CAAC,eAAe,CAAC,SAAiB;QAC5C,IAAI,SAAS,CAAC,MAAM,KAAK,EAAE;YAAE,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;QACzE,MAAM,QAAQ,GAAG,SAAS,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;QAC7C,IAAI,QAAQ,GAAG,EAAE,IAAI,QAAQ,GAAG,CAAC,EAAE;YAC/B,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;SAClD;QACD,OAAO;YACH,UAAU,EAAE,CAAC,CAAC,CAAC,QAAQ,GAAG,EAAE,CAAC;YAC7B,QAAQ,EAAE,QAAQ,GAAG,CAAC;YACtB,SAAS,EAAE,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC;SACnC,CAAA;IACL,CAAC;CAEJ;AAED,kBAAe,MAAM,CAAC"}
@@ -0,0 +1,22 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * Class that implement variable length integer (VarInt) in Javascript.
4
+ * Reference: https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
5
+ */
6
+ declare class VarInt {
7
+ /**
8
+ * Encode an integer i as a variable length integer.
9
+ * Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/helper.py#L180
10
+ * @param i Integer to be encoded
11
+ * @returns Encoded varint
12
+ */
13
+ static encode(i: number): Buffer;
14
+ /**
15
+ * Decode a variable length integer from a Buffer into a number.
16
+ * Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/helper.py#L160
17
+ * @param b Buffer which contain the varint
18
+ * @returns Decoded number
19
+ */
20
+ static decode(b: Buffer): number;
21
+ }
22
+ export default VarInt;
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Class that implement variable length integer (VarInt) in Javascript.
5
+ * Reference: https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
6
+ */
7
+ class VarInt {
8
+ /**
9
+ * Encode an integer i as a variable length integer.
10
+ * Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/helper.py#L180
11
+ * @param i Integer to be encoded
12
+ * @returns Encoded varint
13
+ */
14
+ static encode(i) {
15
+ if (i < 0xFD) {
16
+ const buffer = Buffer.alloc(1);
17
+ buffer.writeUInt8(i);
18
+ return buffer;
19
+ }
20
+ else if (i < 0x10000) {
21
+ const buffer = Buffer.alloc(3);
22
+ buffer.writeUInt8(0xfd);
23
+ buffer.writeUInt16LE(i, 1);
24
+ return buffer;
25
+ }
26
+ else if (i < 0x100000000) {
27
+ const buffer = Buffer.alloc(5);
28
+ buffer.writeUInt8(0xfe);
29
+ buffer.writeUInt32LE(i, 1);
30
+ return buffer;
31
+ }
32
+ else if (i < 0x1000000000000) {
33
+ const buffer = Buffer.alloc(9);
34
+ buffer.writeUInt8(0xff);
35
+ buffer.writeUIntLE(i, 1, 6); // Cannot write UInt64LE in Node JS
36
+ buffer.writeUInt8(0x00, 7); // Pad two extra 0x00 at the end to emulate UInt64LE
37
+ buffer.writeUInt8(0x00, 8);
38
+ return buffer;
39
+ }
40
+ else {
41
+ throw new Error(`Integer too large: ${i}`);
42
+ }
43
+ }
44
+ /**
45
+ * Decode a variable length integer from a Buffer into a number.
46
+ * Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/helper.py#L160
47
+ * @param b Buffer which contain the varint
48
+ * @returns Decoded number
49
+ */
50
+ static decode(b) {
51
+ // Check for empty buffer
52
+ if (b.byteLength === 0) {
53
+ throw new Error('Empty buffer provided');
54
+ }
55
+ // Read first byte from the buffer
56
+ const i = b.readUInt8();
57
+ // Check if i is indicating its length
58
+ if (i === 0xfd) {
59
+ // 0xfd means the next two bytes are the number
60
+ return b.readUInt16LE(1);
61
+ }
62
+ else if (i === 0xfe) {
63
+ // 0xfe means the next four bytes are the number
64
+ return b.readUInt32LE(1);
65
+ }
66
+ else if (i === 0xff) {
67
+ // 0xff means the next eight bytes are the number, but Node JS can only read up to 6 bytes
68
+ return b.readUIntLE(1, 6);
69
+ }
70
+ else {
71
+ // Anything else is just the integer
72
+ return i;
73
+ }
74
+ }
75
+ }
76
+ exports.default = VarInt;
77
+ //# sourceMappingURL=VarInt.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"VarInt.js","sourceRoot":"","sources":["../../src/helpers/VarInt.ts"],"names":[],"mappings":";;AAAA;;;GAGG;AACH,MAAM,MAAM;IAER;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,CAAS;QAC1B,IAAI,CAAC,GAAG,IAAI,EAAE;YACV,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAC/B,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;YACrB,OAAO,MAAM,CAAC;SACjB;aACI,IAAI,CAAC,GAAG,OAAO,EAAE;YAClB,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAC/B,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;YACxB,MAAM,CAAC,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;YAC3B,OAAO,MAAM,CAAC;SACjB;aACI,IAAI,CAAC,GAAG,WAAW,EAAE;YACtB,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAC/B,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;YACxB,MAAM,CAAC,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;YAC3B,OAAO,MAAM,CAAC;SACjB;aACI,IAAI,CAAC,GAAG,eAAe,EAAE;YAC1B,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAC/B,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;YACxB,MAAM,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,mCAAmC;YAChE,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,oDAAoD;YAChF,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;YAC3B,OAAO,MAAM,CAAC;SACjB;aACI;YACD,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,EAAE,CAAC,CAAC;SAC9C;IACL,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,CAAS;QAC1B,yBAAyB;QACzB,IAAI,CAAC,CAAC,UAAU,KAAK,CAAC,EAAE;YACpB,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;SAC5C;QACD,kCAAkC;QAClC,MAAM,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,CAAC;QACxB,sCAAsC;QACtC,IAAI,CAAC,KAAK,IAAI,EAAE;YACZ,+CAA+C;YAC/C,OAAO,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;SAC5B;aACI,IAAI,CAAC,KAAK,IAAI,EAAE;YACjB,gDAAgD;YAChD,OAAO,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;SAC5B;aACI,IAAI,CAAC,KAAK,IAAI,EAAE;YACjB,0FAA0F;YAC1F,OAAO,CAAC,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;SAC7B;aACI;YACD,oCAAoC;YACpC,OAAO,CAAC,CAAC;SACZ;IACL,CAAC;CAEJ;AAED,kBAAe,MAAM,CAAC"}
@@ -0,0 +1,22 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * Class that implement variable length string (VarStr) in Javascript.
4
+ * Reference: https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_string
5
+ */
6
+ declare class VarStr {
7
+ /**
8
+ * Encode a string buffer as a variable length string.
9
+ * Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/helper.py#L203
10
+ * @param s String buffer to be encoded
11
+ * @returns Encoded varstr
12
+ */
13
+ static encode(s: Buffer): Buffer;
14
+ /**
15
+ * Decode a variable length string from a Buffer into a string buffer.
16
+ * Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/helper.py#L194
17
+ * @param v Varstr to be decoded
18
+ * @returns Decoded string buffer
19
+ */
20
+ static decode(v: Buffer): Buffer;
21
+ }
22
+ export default VarStr;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ // Import dependency
7
+ const VarInt_1 = __importDefault(require("./VarInt"));
8
+ /**
9
+ * Class that implement variable length string (VarStr) in Javascript.
10
+ * Reference: https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_string
11
+ */
12
+ class VarStr {
13
+ /**
14
+ * Encode a string buffer as a variable length string.
15
+ * Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/helper.py#L203
16
+ * @param s String buffer to be encoded
17
+ * @returns Encoded varstr
18
+ */
19
+ static encode(s) {
20
+ // Encode the length of the string using encodeVarInt
21
+ const lengthBuffer = VarInt_1.default.encode(s.length);
22
+ // Concat the actual string right after the length of the string
23
+ return Buffer.concat([lengthBuffer, s]);
24
+ }
25
+ /**
26
+ * Decode a variable length string from a Buffer into a string buffer.
27
+ * Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/helper.py#L194
28
+ * @param v Varstr to be decoded
29
+ * @returns Decoded string buffer
30
+ */
31
+ static decode(v) {
32
+ // Find the length of the string by using read_varint on the string
33
+ const length = VarInt_1.default.decode(v);
34
+ // Get the length of the VarInt used to represent the length of the string
35
+ const lengthByteLength = VarInt_1.default.encode(length).byteLength;
36
+ // Return from lengthByteLength to (length + lengthByteLength) in the buffer which contain the actual string
37
+ return v.subarray(lengthByteLength, length + lengthByteLength);
38
+ }
39
+ }
40
+ exports.default = VarStr;
41
+ //# sourceMappingURL=VarStr.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"VarStr.js","sourceRoot":"","sources":["../../src/helpers/VarStr.ts"],"names":[],"mappings":";;;;;AAAA,oBAAoB;AACpB,sDAA8B;AAE9B;;;GAGG;AACH,MAAM,MAAM;IAER;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,CAAS;QAC1B,qDAAqD;QACrD,MAAM,YAAY,GAAG,gBAAM,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;QAC7C,gEAAgE;QAChE,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,CAAC;IAC5C,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,CAAS;QAC1B,mEAAmE;QACnE,MAAM,MAAM,GAAG,gBAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QAChC,0EAA0E;QAC1E,MAAM,gBAAgB,GAAG,gBAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,UAAU,CAAC;QAC1D,4GAA4G;QAC5G,OAAO,CAAC,CAAC,QAAQ,CAAC,gBAAgB,EAAE,MAAM,GAAG,gBAAgB,CAAC,CAAC;IACnE,CAAC;CAEJ;AAED,kBAAe,MAAM,CAAC"}
@@ -0,0 +1,23 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * Class that implement witness data serialization and deserialization.
4
+ */
5
+ declare class Witness {
6
+ /**
7
+ * Encode array of witness into its base-64 encoded format.
8
+ * Follows the encoding scheme found in buidl-python:
9
+ * https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/witness.py#L35
10
+ * @param witnesses Array of witness data
11
+ * @returns Base-64 encoded witness data
12
+ */
13
+ static serialize(witnesses: Uint8Array[]): string;
14
+ /**
15
+ * Decode encoded witness data, either as a base-64 encoded string or as a decoded string in a buffer, into an array of witness.
16
+ * Follows the decoding scheme found in buidl-python:
17
+ * https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/witness.py#L62
18
+ * @param encodedWitness Base-64 encoded witness data, or encoded witness data that have already been decoded
19
+ * @returns Decoded witness data
20
+ */
21
+ static deserialize(encodedWitness: string | Buffer): Buffer[];
22
+ }
23
+ export default Witness;
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ // Import dependencies
7
+ const VarInt_1 = __importDefault(require("./VarInt"));
8
+ const VarStr_1 = __importDefault(require("./VarStr"));
9
+ /**
10
+ * Class that implement witness data serialization and deserialization.
11
+ */
12
+ class Witness {
13
+ /**
14
+ * Encode array of witness into its base-64 encoded format.
15
+ * Follows the encoding scheme found in buidl-python:
16
+ * https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/witness.py#L35
17
+ * @param witnesses Array of witness data
18
+ * @returns Base-64 encoded witness data
19
+ */
20
+ static serialize(witnesses) {
21
+ // The first element to be included is the length of the witness array as VarInt
22
+ let witnessStack = VarInt_1.default.encode(witnesses.length);
23
+ // Then, for each witness array,
24
+ witnesses.forEach((witness) => {
25
+ // Append each witness as a VarStr to the witness stack
26
+ witnessStack = Buffer.concat([witnessStack, VarStr_1.default.encode(Buffer.from(witness))]);
27
+ });
28
+ // Return the base-64 encoded witness stack
29
+ return witnessStack.toString('base64');
30
+ }
31
+ /**
32
+ * Decode encoded witness data, either as a base-64 encoded string or as a decoded string in a buffer, into an array of witness.
33
+ * Follows the decoding scheme found in buidl-python:
34
+ * https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/witness.py#L62
35
+ * @param encodedWitness Base-64 encoded witness data, or encoded witness data that have already been decoded
36
+ * @returns Decoded witness data
37
+ */
38
+ static deserialize(encodedWitness) {
39
+ // Store the decoded witness stack
40
+ let witnessDecoded = [];
41
+ // Preprocess the encodedWitness if needed
42
+ let witnessToDecode;
43
+ if (typeof encodedWitness === 'string') {
44
+ // Decode the encoded witness if it is a string (assuming it is encoded using base-64)
45
+ witnessToDecode = Buffer.from(encodedWitness, 'base64');
46
+ }
47
+ else {
48
+ witnessToDecode = encodedWitness;
49
+ }
50
+ // Read a VarInt which indicate the number of elements within the original witness array
51
+ const witnessCount = VarInt_1.default.decode(witnessToDecode);
52
+ // Slice the VarInt in front of the witness buffer before decoding each witness
53
+ const varIntLength = VarInt_1.default.encode(witnessCount).byteLength;
54
+ witnessToDecode = witnessToDecode.subarray(varIntLength);
55
+ // Loop for each witness encoded
56
+ for (let i = 0; i < witnessCount; i++) {
57
+ // Read a VarStr from the remaining buffer
58
+ const witness = VarStr_1.default.decode(witnessToDecode);
59
+ // Append the decoded witness to witnessDecoded
60
+ witnessDecoded.push(witness);
61
+ // Slice the read witness off witnessToDecode before next iteration
62
+ const witnessLength = VarStr_1.default.encode(witness).byteLength;
63
+ witnessToDecode = witnessToDecode.subarray(witnessLength);
64
+ }
65
+ // Return deserialized witness data
66
+ return witnessDecoded;
67
+ }
68
+ }
69
+ exports.default = Witness;
70
+ //# sourceMappingURL=Witness.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Witness.js","sourceRoot":"","sources":["../../src/helpers/Witness.ts"],"names":[],"mappings":";;;;;AAAA,sBAAsB;AACtB,sDAA8B;AAC9B,sDAA8B;AAE9B;;GAEG;AACH,MAAM,OAAO;IAET;;;;;;OAMG;IACI,MAAM,CAAC,SAAS,CAAC,SAAuB;QAC3C,gFAAgF;QAChF,IAAI,YAAY,GAAG,gBAAM,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;QACnD,gCAAgC;QAChC,SAAS,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC1B,uDAAuD;YACvD,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,CAAE,YAAY,EAAE,gBAAM,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAE,CAAC,CAAC;QACxF,CAAC,CAAC,CAAC;QACH,2CAA2C;QAC3C,OAAO,YAAY,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAC3C,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,WAAW,CAAC,cAA+B;QACrD,kCAAkC;QAClC,IAAI,cAAc,GAAkB,EAAE,CAAC;QACvC,0CAA0C;QAC1C,IAAI,eAAuB,CAAC;QAC5B,IAAI,OAAO,cAAc,KAAK,QAAQ,EAAE;YACpC,sFAAsF;YACtF,eAAe,GAAG,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,QAAQ,CAAC,CAAC;SAC3D;aACI;YACD,eAAe,GAAG,cAAc,CAAC;SACpC;QACD,wFAAwF;QACxF,MAAM,YAAY,GAAG,gBAAM,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;QACpD,+EAA+E;QAC/E,MAAM,YAAY,GAAG,gBAAM,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,UAAU,CAAC;QAC5D,eAAe,GAAG,eAAe,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC;QACzD,gCAAgC;QAChC,KAAK,IAAI,CAAC,GAAC,CAAC,EAAE,CAAC,GAAC,YAAY,EAAE,CAAC,EAAE,EAAE;YAC/B,0CAA0C;YAC1C,MAAM,OAAO,GAAG,gBAAM,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;YAC/C,+CAA+C;YAC/C,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YAC7B,mEAAmE;YACnE,MAAM,aAAa,GAAG,gBAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,UAAU,CAAC;YACxD,eAAe,GAAG,eAAe,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC;SAC7D;QACD,mCAAmC;QACnC,OAAO,cAAc,CAAC;IAC1B,CAAC;CAEJ;AAED,kBAAe,OAAO,CAAC"}
@@ -0,0 +1,6 @@
1
+ import Address from "./Address";
2
+ import BIP137 from "./BIP137";
3
+ import VarInt from "./VarInt";
4
+ import VarStr from "./VarStr";
5
+ import Witness from "./Witness";
6
+ export { Address, BIP137, VarInt, VarStr, Witness };
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Witness = exports.VarStr = exports.VarInt = exports.BIP137 = exports.Address = void 0;
7
+ const Address_1 = __importDefault(require("./Address"));
8
+ exports.Address = Address_1.default;
9
+ const BIP137_1 = __importDefault(require("./BIP137"));
10
+ exports.BIP137 = BIP137_1.default;
11
+ const VarInt_1 = __importDefault(require("./VarInt"));
12
+ exports.VarInt = VarInt_1.default;
13
+ const VarStr_1 = __importDefault(require("./VarStr"));
14
+ exports.VarStr = VarStr_1.default;
15
+ const Witness_1 = __importDefault(require("./Witness"));
16
+ exports.Witness = Witness_1.default;
17
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/helpers/index.ts"],"names":[],"mappings":";;;;;;AAAA,wDAAgC;AAMvB,kBANF,iBAAO,CAME;AALhB,sDAA8B;AAKZ,iBALX,gBAAM,CAKW;AAJxB,sDAA8B;AAIJ,iBAJnB,gBAAM,CAImB;AAHhC,sDAA8B;AAGI,iBAH3B,gBAAM,CAG2B;AAFxC,wDAAgC;AAEU,kBAFnC,iBAAO,CAEmC"}
@@ -0,0 +1,5 @@
1
+ import BIP322 from "./BIP322";
2
+ import Signer from "./Signer";
3
+ import Verifier from "./Verifier";
4
+ import { Witness, Address, BIP137 } from "./helpers";
5
+ export { BIP322, Signer, Verifier, Witness, Address, BIP137 };
package/dist/index.js ADDED
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.BIP137 = exports.Address = exports.Witness = exports.Verifier = exports.Signer = exports.BIP322 = void 0;
30
+ // Import modules to be exported
31
+ const BIP322_1 = __importDefault(require("./BIP322"));
32
+ exports.BIP322 = BIP322_1.default;
33
+ const Signer_1 = __importDefault(require("./Signer"));
34
+ exports.Signer = Signer_1.default;
35
+ const Verifier_1 = __importDefault(require("./Verifier"));
36
+ exports.Verifier = Verifier_1.default;
37
+ const helpers_1 = require("./helpers");
38
+ Object.defineProperty(exports, "Witness", { enumerable: true, get: function () { return helpers_1.Witness; } });
39
+ Object.defineProperty(exports, "Address", { enumerable: true, get: function () { return helpers_1.Address; } });
40
+ Object.defineProperty(exports, "BIP137", { enumerable: true, get: function () { return helpers_1.BIP137; } });
41
+ // Provide a ECC library to bitcoinjs-lib
42
+ const secp256k1_1 = __importDefault(require("@bitcoinerlab/secp256k1"));
43
+ const bitcoin = __importStar(require("bitcoinjs-lib"));
44
+ bitcoin.initEccLib(secp256k1_1.default);
45
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gCAAgC;AAChC,sDAA8B;AAWrB,iBAXF,gBAAM,CAWE;AAVf,sDAA8B;AAUb,iBAVV,gBAAM,CAUU;AATvB,0DAAkC;AAST,mBATlB,kBAAQ,CASkB;AARjC,uCAAqD;AAQlB,wFAR1B,iBAAO,OAQ0B;AAAE,wFAR1B,iBAAO,OAQ0B;AAAE,uFAR1B,gBAAM,OAQ0B;AAN3D,yCAAyC;AACzC,wEAA0C;AAC1C,uDAAyC;AACzC,OAAO,CAAC,UAAU,CAAC,mBAAG,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@exodus/bip322-js",
3
+ "version": "1.1.0-exodus.0",
4
+ "description": "A Javascript library that provides utility functions related to the BIP-322 signature scheme",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "publishConfig": {
8
+ "access": "restricted"
9
+ },
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "doc": "typedoc src/index.ts",
13
+ "prepack": "npm run build",
14
+ "test": "jest",
15
+ "test:coverage": "nyc --reporter=text --reporter=text-summary --reporter=lcov npm test"
16
+ },
17
+ "keywords": [
18
+ "bip322",
19
+ "bitcoinjs",
20
+ "javascript",
21
+ "typescript",
22
+ "no-WASM"
23
+ ],
24
+ "author": "Ken Sze <acken2@outlook.com>",
25
+ "repository": "https://github.com/ExodusMovement/bip322-js/",
26
+ "license": "MIT",
27
+ "devDependencies": {
28
+ "@babel/core": "^7.22.20",
29
+ "@babel/preset-env": "^7.22.20",
30
+ "@babel/preset-flow": "^7.22.15",
31
+ "@babel/preset-typescript": "^7.22.15",
32
+ "@types/node": "^20.2.5",
33
+ "@types/secp256k1": "^4.0.3",
34
+ "babel-jest": "^29.7.0",
35
+ "jest": "^29.7.0",
36
+ "nyc": "^15.1.0",
37
+ "typedoc": "^0.24.8",
38
+ "typescript": "^5.1.3"
39
+ },
40
+ "dependencies": {
41
+ "@bitcoinerlab/secp256k1": "^1.0.2",
42
+ "@exodus/secp256k1": "^4.0.2-exodus.0",
43
+ "bitcoinjs-lib": "^6.1.1",
44
+ "bitcoinjs-message": "^2.2.0",
45
+ "ecpair": "^2.0.1"
46
+ }
47
+ }