working rss feed to nostr publish
This commit is contained in:
+115
@@ -0,0 +1,115 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createBitArray = createBitArray;
|
||||
exports.fromBits = fromBits;
|
||||
exports.toBits = toBits;
|
||||
|
||||
/* eslint-disable no-unused-vars */
|
||||
|
||||
/**
|
||||
* Virtual type for bit arrays, i.e., arrays in which each element contains
|
||||
* an integer in range `[0, 1 << L)`, where `1 <= L <= 8`.
|
||||
*/
|
||||
|
||||
/* eslint-enable no-unused-vars */
|
||||
|
||||
/**
|
||||
* Performs unchecked conversion from `Uint8Array` to `BitArray`.
|
||||
* This function is translated as the indentity operation by Babel; it's needed purely
|
||||
* for Flow type checks.
|
||||
*
|
||||
* @param {Uint8Array} src
|
||||
* array to convert
|
||||
* @returns {Uint8Array}
|
||||
* `src` interpreted as a `BitArray` with the specified bitness
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
function toBitArrayUnchecked(src) {
|
||||
return src;
|
||||
}
|
||||
/**
|
||||
* Creates a new array with specified bitness.
|
||||
*
|
||||
* @param {number} len
|
||||
* length of the created array
|
||||
* @returns {Uint8Array}
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
|
||||
function createBitArray(len) {
|
||||
return toBitArrayUnchecked(new Uint8Array(len));
|
||||
}
|
||||
/**
|
||||
* Converts an array from one number of bits per element to another.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
|
||||
function convert(src, srcBits, dst, dstBits, pad) {
|
||||
var mask = (1 << dstBits) - 1;
|
||||
var acc = 0;
|
||||
var bits = 0;
|
||||
var pos = 0;
|
||||
src.forEach(function (b) {
|
||||
// Pull next bits from the input buffer into accumulator.
|
||||
acc = (acc << srcBits) + b;
|
||||
bits += srcBits; // Push into the output buffer while there are enough bits in the accumulator.
|
||||
|
||||
while (bits >= dstBits) {
|
||||
bits -= dstBits;
|
||||
dst[pos] = acc >> bits & mask;
|
||||
pos += 1;
|
||||
}
|
||||
});
|
||||
|
||||
if (pad) {
|
||||
if (bits > 0) {
|
||||
// `dstBits - rem.bits` is the number of trailing zero bits needed to be appended
|
||||
// to accumulator bits to get the trailing bit group.
|
||||
dst[pos] = acc << dstBits - bits & mask;
|
||||
}
|
||||
} else {
|
||||
// Truncate the remaining padding, but make sure that it is zeroed and not
|
||||
// overly long first.
|
||||
if (bits >= srcBits) {
|
||||
throw new Error("Excessive padding: ".concat(bits, " (max ").concat(srcBits - 1, " allowed)"));
|
||||
}
|
||||
|
||||
if (acc % (1 << bits) !== 0) {
|
||||
throw new Error('Non-zero padding');
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Encodes a `Uint8Array` buffer as an array with a lesser number of bits per element.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
|
||||
function toBits(src, bits, dst) {
|
||||
if (bits > 8 || bits < 1) {
|
||||
throw new RangeError('Invalid bits per element; 1 to 8 expected');
|
||||
} // `BitArray<8>` is equivalent to `Uint8Array`; unfortunately, Flow
|
||||
// has problems expressing this, so the explicit conversion is performed here.
|
||||
|
||||
|
||||
convert(toBitArrayUnchecked(src), 8, dst, bits, true);
|
||||
return dst;
|
||||
}
|
||||
|
||||
function fromBits(src, bits, dst) {
|
||||
if (bits > 8 || bits < 1) {
|
||||
throw new RangeError('Invalid bits per element; 1 to 8 expected');
|
||||
}
|
||||
|
||||
convert(src, bits, toBitArrayUnchecked(dst), 8, false);
|
||||
return dst;
|
||||
}
|
||||
+217
@@ -0,0 +1,217 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.CHECKSUM_LENGTH = void 0;
|
||||
exports.createChecksum = createChecksum;
|
||||
exports.decode = decode;
|
||||
exports.decodeWithPrefix = decodeWithPrefix;
|
||||
exports.detectCase = detectCase;
|
||||
exports.encode = encode;
|
||||
exports.expandPrefix = expandPrefix;
|
||||
exports.verifyChecksum = verifyChecksum;
|
||||
|
||||
var _bitConverter = require("./bit-converter");
|
||||
|
||||
// Alphabet for Bech32
|
||||
var CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l'; // Checksum constant for Bech32m.
|
||||
|
||||
var BECH32M_CHECKSUM = 0x2bc830a3; // Minimum char code that could be present in the encoded message
|
||||
|
||||
var MIN_CHAR_CODE = 33; // Maximum char code that could be present in the encoded message
|
||||
|
||||
var MAX_CHAR_CODE = 126;
|
||||
var CHECKSUM_LENGTH = 6; // Reverse lookup for characters
|
||||
|
||||
exports.CHECKSUM_LENGTH = CHECKSUM_LENGTH;
|
||||
|
||||
var CHAR_LOOKUP = function () {
|
||||
var lookup = new Map();
|
||||
|
||||
for (var i = 0; i < CHARSET.length; i += 1) {
|
||||
lookup.set(CHARSET[i], i);
|
||||
}
|
||||
|
||||
return lookup;
|
||||
}(); // Poly generators
|
||||
|
||||
|
||||
var GEN = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3];
|
||||
|
||||
function polymod(values) {
|
||||
return values.reduce(function (checksum, value) {
|
||||
var bits = checksum >> 25;
|
||||
var newChecksum = (checksum & 0x1ffffff) << 5 ^ value;
|
||||
return GEN.reduce(function (chk, gen, i) {
|
||||
return (bits >> i & 1) === 0 ? chk : chk ^ gen;
|
||||
}, newChecksum);
|
||||
},
|
||||
/* initial checksum */
|
||||
1);
|
||||
}
|
||||
/**
|
||||
* Expands a prefix into the specified output buffer.
|
||||
*/
|
||||
|
||||
|
||||
function expandPrefix(prefix, outBuffer) {
|
||||
for (var i = 0; i < prefix.length; i += 1) {
|
||||
var code = prefix.charCodeAt(i);
|
||||
outBuffer[i] = code >> 5;
|
||||
outBuffer[i + prefix.length + 1] = code & 31;
|
||||
}
|
||||
|
||||
outBuffer[prefix.length] = 0;
|
||||
}
|
||||
/**
|
||||
* Verifies the checksum for a particular buffer.
|
||||
*/
|
||||
|
||||
|
||||
function verifyChecksum(buffer) {
|
||||
switch (polymod(buffer)) {
|
||||
case 1:
|
||||
return 'bech32';
|
||||
|
||||
case BECH32M_CHECKSUM:
|
||||
return 'bech32m';
|
||||
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Creates a checksum for a buffer and writes it to the last 6 5-bit groups
|
||||
* of the buffer.
|
||||
*/
|
||||
|
||||
|
||||
function createChecksum(buffer, encoding) {
|
||||
var checksumConstant;
|
||||
|
||||
switch (encoding) {
|
||||
case 'bech32':
|
||||
checksumConstant = 1;
|
||||
break;
|
||||
|
||||
case 'bech32m':
|
||||
checksumConstant = BECH32M_CHECKSUM;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw Error("Invalid encoding value: ".concat(encoding, "; expected bech32 or bech32m"));
|
||||
}
|
||||
|
||||
var mod = polymod(buffer) ^ checksumConstant;
|
||||
|
||||
for (var i = 0; i < CHECKSUM_LENGTH; i += 1) {
|
||||
var shift = 5 * (5 - i);
|
||||
buffer[buffer.length - CHECKSUM_LENGTH + i] = mod >> shift & 31;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Encodes an array of 5-bit groups into a string.
|
||||
*
|
||||
* @param {Uint8Array} buffer
|
||||
* @returns {string}
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
|
||||
function encode(buffer) {
|
||||
return buffer.reduce(function (acc, bits) {
|
||||
return acc + CHARSET[bits];
|
||||
}, '');
|
||||
}
|
||||
/**
|
||||
* Decodes a string into an array of 5-bit groups.
|
||||
*
|
||||
* @param {string} message
|
||||
* @param {Uint8Array} [dst]
|
||||
* Optional array to write the output to. If not specified, the array is created.
|
||||
* @returns {Uint8Array}
|
||||
* Array with the result of decoding
|
||||
*
|
||||
* @throws {Error}
|
||||
* if there are characters in `message` not present in the encoding alphabet
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
|
||||
function decode(message, dst) {
|
||||
var realDst = dst || (0, _bitConverter.createBitArray)(message.length);
|
||||
|
||||
for (var i = 0; i < message.length; i += 1) {
|
||||
var idx = CHAR_LOOKUP.get(message[i]);
|
||||
|
||||
if (idx === undefined) {
|
||||
throw new Error("Invalid char in message: ".concat(message[i]));
|
||||
}
|
||||
|
||||
realDst[i] = idx;
|
||||
}
|
||||
|
||||
return realDst;
|
||||
}
|
||||
/**
|
||||
* Decodes a string and a human-readable prefix into an array of 5-bit groups.
|
||||
* The prefix is expanded as specified by Bech32.
|
||||
*
|
||||
* @param {string} prefix
|
||||
* @param {string} message
|
||||
* @returns {Uint8Array}
|
||||
* Array with the result of decoding
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
|
||||
function decodeWithPrefix(prefix, message) {
|
||||
var len = message.length + 2 * prefix.length + 1;
|
||||
var dst = (0, _bitConverter.createBitArray)(len);
|
||||
expandPrefix(prefix, dst.subarray(0, 2 * prefix.length + 1));
|
||||
decode(message, dst.subarray(2 * prefix.length + 1));
|
||||
return dst;
|
||||
}
|
||||
/**
|
||||
* Detects the character case used in `message`. If the message doesn't
|
||||
* contain either lower-case or upper-case chars, returns `null`.
|
||||
*
|
||||
* @param {string} message
|
||||
* @param {string} messageDescription
|
||||
* Human-readable description of the message to put into an error message, should any occur
|
||||
* @returns {'lower'|'upper'|null}
|
||||
* @throws if the message contains both lowercase and uppercase chars,
|
||||
* or contains chars not valid for Bech32(m) encoding
|
||||
*/
|
||||
|
||||
|
||||
function detectCase(message) {
|
||||
var messageDescription = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'message';
|
||||
var hasLowerCase = false;
|
||||
var hasUpperCase = false;
|
||||
|
||||
for (var i = 0; i < message.length; i += 1) {
|
||||
var ord = message.charCodeAt(i); // 3. Allowed chars in the encoding
|
||||
|
||||
if (ord < MIN_CHAR_CODE || ord > MAX_CHAR_CODE) {
|
||||
throw new TypeError("Invalid char in ".concat(messageDescription, ": ").concat(ord, "; ") + "should be in ASCII range ".concat(MIN_CHAR_CODE, "-").concat(MAX_CHAR_CODE));
|
||||
}
|
||||
|
||||
hasUpperCase = hasUpperCase || ord >= 65 && ord <= 90;
|
||||
hasLowerCase = hasLowerCase || ord >= 97 && ord <= 122;
|
||||
}
|
||||
|
||||
if (hasLowerCase && hasUpperCase) {
|
||||
throw new TypeError("Mixed-case ".concat(messageDescription));
|
||||
} else if (hasUpperCase) {
|
||||
return 'upper';
|
||||
} else if (hasLowerCase) {
|
||||
return 'lower';
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+349
@@ -0,0 +1,349 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.BitcoinAddress = void 0;
|
||||
exports.decode = decode;
|
||||
exports.decodeTo5BitArray = decodeTo5BitArray;
|
||||
exports.encode = encode;
|
||||
exports.encode5BitArray = encode5BitArray;
|
||||
exports.from5BitArray = from5BitArray;
|
||||
exports.to5BitArray = to5BitArray;
|
||||
|
||||
var _bitConverter = require("./bit-converter");
|
||||
|
||||
var _encoding = require("./encoding");
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
||||
|
||||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
||||
|
||||
// Maximum encoded message length
|
||||
var MAX_ENC_LENGTH = 90;
|
||||
|
||||
/**
|
||||
* Converts a Uint8Array into a Uint8Array variant, in which each element
|
||||
* encodes 5 bits of the original byte array.
|
||||
*
|
||||
* @param {Uint8Array} src
|
||||
* Input to convert
|
||||
* @param {?Uint8Array} dst
|
||||
* Optional output buffer. If specified, the sequence of 5-bit chunks will be written there;
|
||||
* if not specified, the output buffer will be created from scratch. The length
|
||||
* of `outBuffer` is not checked.
|
||||
* @returns {Uint8Array}
|
||||
* Output buffer consisting of 5-bit chunks
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
function to5BitArray(src, dst) {
|
||||
var len = Math.ceil(src.length * 8 / 5);
|
||||
var realDst = dst || (0, _bitConverter.createBitArray)(len);
|
||||
return (0, _bitConverter.toBits)(src, 5, realDst);
|
||||
}
|
||||
|
||||
function from5BitArray(src, dst) {
|
||||
var len = Math.floor(src.length * 5 / 8);
|
||||
var realDst = dst || new Uint8Array(len);
|
||||
return (0, _bitConverter.fromBits)(src, 5, realDst);
|
||||
}
|
||||
/**
|
||||
* Encodes binary data into Bech32 encoding.
|
||||
*
|
||||
* The case is preserved: if the prefix is uppercase, then the output will be uppercase
|
||||
* as well; otherwise, the output will be lowercase (including the case when the prefix does
|
||||
* not contain any letters).
|
||||
*
|
||||
* Ordinarily, you may want to use [`encode`](#encode) because it converts
|
||||
* binary data to an array of 5-bit integers automatically.
|
||||
*
|
||||
* @param {string} prefix
|
||||
* Human-readable prefix to place at the beginning of the encoding
|
||||
* @param {Uint8Array} data
|
||||
* Array of 5-bit integers with data to encode
|
||||
* @param {Encoding} encoding
|
||||
* Encoding to use; influences the checksum computation. If not specified,
|
||||
* Bech32 encoding will be used.
|
||||
* @returns {string}
|
||||
* Bech32 encoding of data in the form `<prefix>1<base32 of data><checksum>`
|
||||
* @throws If the prefix is mixed-case or contains chars that are not eligible for Bech32 encoding
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
|
||||
function encode5BitArray(prefix, data) {
|
||||
var _detectCase;
|
||||
|
||||
var encoding = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'bech32';
|
||||
// 1. Allocate buffer for all operations
|
||||
var len = 2 * prefix.length + 1 // expanded prefix
|
||||
+ data.length // five-bit data encoding
|
||||
+ _encoding.CHECKSUM_LENGTH; // checksum
|
||||
|
||||
if (len - prefix.length > MAX_ENC_LENGTH) {
|
||||
throw new Error("Message to be produced is too long (max ".concat(MAX_ENC_LENGTH, " supported)"));
|
||||
}
|
||||
|
||||
var prefixCase = (_detectCase = (0, _encoding.detectCase)(prefix, 'prefix')) !== null && _detectCase !== void 0 ? _detectCase : 'lower';
|
||||
var buffer = (0, _bitConverter.createBitArray)(len); // 2. Expand the human-readable prefix into the beginning of the buffer
|
||||
|
||||
(0, _encoding.expandPrefix)(prefix.toLowerCase(), buffer.subarray(0, 2 * prefix.length + 1)); // 3. Copy `data` into the output
|
||||
|
||||
var dataBuffer = buffer.subarray(2 * prefix.length + 1, buffer.length - _encoding.CHECKSUM_LENGTH);
|
||||
dataBuffer.set(data); // 4. Create the checksum
|
||||
|
||||
(0, _encoding.createChecksum)(buffer, encoding); // 5. Convert into string
|
||||
|
||||
var encoded = (0, _encoding.encode)(buffer.subarray(2 * prefix.length + 1));
|
||||
|
||||
if (prefixCase === 'upper') {
|
||||
encoded = encoded.toUpperCase();
|
||||
}
|
||||
|
||||
return "".concat(prefix, "1").concat(encoded);
|
||||
}
|
||||
/**
|
||||
* Encodes binary data into Bech32 encoding.
|
||||
*
|
||||
* The case is preserved: if the prefix is uppercase, then the output will be uppercase
|
||||
* as well; otherwise, the output will be lowercase (including the case when the prefix does
|
||||
* not contain any letters).
|
||||
*
|
||||
* @param {string} prefix
|
||||
* Human-readable prefix to place at the beginning of the encoding
|
||||
* @param {Uint8Array} data
|
||||
* Binary data to encode
|
||||
* @param {Encoding} encoding
|
||||
* Encoding to use; influences the checksum computation. If not specified,
|
||||
* Bech32 encoding will be used.
|
||||
* @returns {string}
|
||||
* Bech32 encoding of data in the form `<prefix>1<base32 of data><checksum>`
|
||||
* @throws If the prefix is mixed-case or contains chars that are not eligible for Bech32 encoding
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
|
||||
function encode(prefix, data) {
|
||||
var encoding = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'bech32';
|
||||
return encode5BitArray(prefix, to5BitArray(data), encoding);
|
||||
}
|
||||
/**
|
||||
* Decodes data from Bech32 encoding into an array of 5-bit integers.
|
||||
*
|
||||
* Ordinarily, you may want to use [`decode`](#decode) because it automatically
|
||||
* converts the array of 5-bit integers into an ordinary `Uint8Array`.
|
||||
*
|
||||
* @param {string} message
|
||||
* Bech32-encoded message
|
||||
* @returns {DecodeResult<FiveBitArray>}
|
||||
* Decoded object with `prefix` and `data` fields, which contain the human-readable
|
||||
* prefix and the array of 5-bit integers respectively.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
|
||||
function decodeTo5BitArray(message) {
|
||||
// Check preconditions
|
||||
// 1. Message length
|
||||
if (message.length > MAX_ENC_LENGTH) {
|
||||
throw new TypeError("Message too long; max ".concat(MAX_ENC_LENGTH, " expected"));
|
||||
} // 2. Mixed case
|
||||
|
||||
|
||||
(0, _encoding.detectCase)(message); // we don't care about the result, only about checks.
|
||||
|
||||
var lowerCaseMsg = message.toLowerCase(); // 4. Existence of the separator char
|
||||
|
||||
var sepIdx = lowerCaseMsg.lastIndexOf('1');
|
||||
|
||||
if (sepIdx < 0) {
|
||||
throw new Error('No separator char ("1") found');
|
||||
} // 5. Placing of the separator char in the message
|
||||
|
||||
|
||||
if (sepIdx > message.length - _encoding.CHECKSUM_LENGTH - 1) {
|
||||
throw new Error("Data part of the message too short (at least ".concat(_encoding.CHECKSUM_LENGTH, " chars expected)"));
|
||||
}
|
||||
|
||||
var prefix = lowerCaseMsg.substring(0, sepIdx); // Checked within `decodeWithPrefix`:
|
||||
// 6. Invalid chars in the data part of the message
|
||||
|
||||
var bitArray = (0, _encoding.decodeWithPrefix)(prefix, lowerCaseMsg.substring(sepIdx + 1)); // 7. Checksum
|
||||
|
||||
var encoding = (0, _encoding.verifyChecksum)(bitArray);
|
||||
|
||||
if (encoding === undefined) {
|
||||
throw new Error('Invalid checksum');
|
||||
}
|
||||
|
||||
return {
|
||||
prefix: prefix,
|
||||
encoding: encoding,
|
||||
// Strip off the prefix from the front and the checksum from the end
|
||||
data: bitArray.subarray(2 * prefix.length + 1, bitArray.length - _encoding.CHECKSUM_LENGTH)
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Decodes data from Bech32 encoding into an array of 5-bit integers.
|
||||
*
|
||||
* @param {string} message
|
||||
* Bech32-encoded message
|
||||
* @returns {DecodeResult}
|
||||
* Decoded object with `prefix` and `data` fields, which contain the human-readable
|
||||
* prefix and the decoded binary data respectively.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
|
||||
function decode(message) {
|
||||
var _decodeTo5BitArray = decodeTo5BitArray(message),
|
||||
prefix = _decodeTo5BitArray.prefix,
|
||||
encoding = _decodeTo5BitArray.encoding,
|
||||
bitArray = _decodeTo5BitArray.data;
|
||||
|
||||
return {
|
||||
prefix: prefix,
|
||||
encoding: encoding,
|
||||
data: from5BitArray(bitArray)
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Bitcoin address.
|
||||
*/
|
||||
|
||||
|
||||
var BitcoinAddress = /*#__PURE__*/function () {
|
||||
function BitcoinAddress(prefix, scriptVersion, data) {
|
||||
_classCallCheck(this, BitcoinAddress);
|
||||
|
||||
if (prefix !== 'bc' && prefix !== 'tb') {
|
||||
throw new Error('Invalid human-readable prefix, "bc" or "tb" expected');
|
||||
}
|
||||
|
||||
if (scriptVersion < 0 || scriptVersion > 16) {
|
||||
throw new RangeError('Invalid scriptVersion, value in range [0, 16] expected');
|
||||
}
|
||||
|
||||
if (data.length < 2 || data.length > 40) {
|
||||
throw new RangeError('Invalid script length: expected 2 to 40 bytes');
|
||||
}
|
||||
|
||||
if (scriptVersion === 0 && data.length !== 20 && data.length !== 32) {
|
||||
throw new Error('Invalid v0 script length: expected 20 or 32 bytes');
|
||||
}
|
||||
|
||||
this.prefix = prefix;
|
||||
this.scriptVersion = scriptVersion;
|
||||
this.data = data;
|
||||
}
|
||||
/**
|
||||
* Guesses the address type based on its internal structure.
|
||||
*
|
||||
* @returns {void | 'p2wpkh' | 'p2wsh'}
|
||||
*/
|
||||
|
||||
|
||||
_createClass(BitcoinAddress, [{
|
||||
key: "type",
|
||||
value: function type() {
|
||||
if (this.scriptVersion !== 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
switch (this.data.length) {
|
||||
case 20:
|
||||
return 'p2wpkh';
|
||||
|
||||
case 32:
|
||||
return 'p2wsh';
|
||||
// should be unreachable, but it's JS, so you never know
|
||||
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Encodes this address in Bech32 or Bech32m format, depending on the script version.
|
||||
* Version 0 scripts are encoded using original Bech32 encoding as per BIP 173,
|
||||
* while versions 1-16 are encoded using the modified encoding as per BIP 350.
|
||||
*
|
||||
* @returns {string}
|
||||
* Bech32(m)-encoded address
|
||||
*/
|
||||
|
||||
}, {
|
||||
key: "encode",
|
||||
value: function encode() {
|
||||
// Bitcoin addresses use Bech32 in a peculiar way - script version is
|
||||
// not a part of the serialized binary data, but is rather prepended as 5-bit value
|
||||
// before the rest of the script. This necessitates some plumbing here.
|
||||
var len = Math.ceil(this.data.length * 8 / 5);
|
||||
var converted = (0, _bitConverter.createBitArray)(len + 1);
|
||||
converted[0] = this.scriptVersion;
|
||||
to5BitArray(this.data, converted.subarray(1));
|
||||
var encoding = this.scriptVersion === 0 ? 'bech32' : 'bech32m';
|
||||
return encode5BitArray(this.prefix, converted, encoding);
|
||||
}
|
||||
}], [{
|
||||
key: "decode",
|
||||
value:
|
||||
/**
|
||||
* Human-readable prefix. Equal to `'bc'` (for mainnet addresses)
|
||||
* or `'tb'` (for testnet addresses).
|
||||
*/
|
||||
|
||||
/**
|
||||
* Script version. An integer between 0 and 16 (inclusive).
|
||||
*/
|
||||
|
||||
/**
|
||||
* Script data. A byte string with length 2 to 40 (inclusive).
|
||||
*/
|
||||
|
||||
/**
|
||||
* Decodes a Bitcoin address from a Bech32(m) string.
|
||||
* As per BIP 350, the original encoding is expected for version 0 scripts, while
|
||||
* other script versions expect the modified encoding.
|
||||
*
|
||||
* This method does not check whether the address is well-formed;
|
||||
* use `type()` method on returned address to find that out.
|
||||
*
|
||||
* @param {string} message
|
||||
* @returns {BitcoinAddress}
|
||||
*/
|
||||
function decode(message) {
|
||||
var _decodeTo5BitArray2 = decodeTo5BitArray(message),
|
||||
prefix = _decodeTo5BitArray2.prefix,
|
||||
data = _decodeTo5BitArray2.data,
|
||||
encoding = _decodeTo5BitArray2.encoding; // Extra check to satisfy Flow.
|
||||
|
||||
|
||||
if (prefix !== 'bc' && prefix !== 'tb') {
|
||||
throw new Error('Invalid human-readable prefix, "bc" or "tb" expected');
|
||||
}
|
||||
|
||||
var scriptVersion = data[0];
|
||||
|
||||
if (scriptVersion === 0 && encoding !== 'bech32') {
|
||||
throw Error("Unexpected encoding ".concat(encoding, " used for version 0 script"));
|
||||
}
|
||||
|
||||
if (scriptVersion > 0 && encoding !== 'bech32m') {
|
||||
throw Error("Unexpected encoding ".concat(encoding, " used for version ").concat(scriptVersion, " script"));
|
||||
}
|
||||
|
||||
return new this(prefix, scriptVersion, from5BitArray(data.subarray(1)));
|
||||
}
|
||||
}]);
|
||||
|
||||
return BitcoinAddress;
|
||||
}();
|
||||
|
||||
exports.BitcoinAddress = BitcoinAddress;
|
||||
Reference in New Issue
Block a user