@cvbb/bc-bech32
Advanced tools
Comparing version 1.1.4 to 1.1.5
import { Bech32Version } from './index'; | ||
declare const _default: { | ||
encode: (hrp: string, data: number[], version: Bech32Version) => string; | ||
decode: (bechString: string) => { | ||
hrp: string; | ||
data: number[]; | ||
}; | ||
encode: (hrp: string | undefined, data: number[], version: Bech32Version) => string; | ||
decode: ( | ||
bechString: string, | ||
) => | ||
| { | ||
hrp: null; | ||
data: number[]; | ||
} | ||
| { | ||
hrp: string; | ||
data: number[]; | ||
} | ||
| null; | ||
}; | ||
export default _default; |
@@ -1,12 +0,12 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var index_1 = require("./index"); | ||
var CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l'; | ||
var GENERATOR = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]; | ||
function polymod(values) { | ||
var chk = 1; | ||
for (var p = 0; p < values.length; ++p) { | ||
var top_1 = chk >> 25; | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const index_1 = require('./index'); | ||
const CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l'; | ||
const GENERATOR = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]; | ||
const polymod = function (values) { | ||
let chk = 1; | ||
for (let p = 0; p < values.length; ++p) { | ||
const top_1 = chk >> 25; | ||
chk = ((chk & 0x1ffffff) << 5) ^ values[p]; | ||
for (var i = 0; i < 6; ++i) { | ||
for (let i = 0; i < 6; ++i) { | ||
if ((top_1 >> i) & 1) { | ||
@@ -18,6 +18,6 @@ chk ^= GENERATOR[i]; | ||
return chk; | ||
} | ||
function hrpExpand(hrp) { | ||
var ret = []; | ||
var p; | ||
}; | ||
const hrpExpand = function (hrp) { | ||
const ret = []; | ||
let p; | ||
for (p = 0; p < hrp.length; ++p) { | ||
@@ -31,40 +31,37 @@ ret.push(hrp.charCodeAt(p) >> 5); | ||
return ret; | ||
} | ||
function verifyChecksum(hrp, data, version) { | ||
var header; | ||
}; | ||
const verifyChecksum = function (hrp, data, version) { | ||
let header; | ||
if (hrp) { | ||
header = hrpExpand(hrp); | ||
} | ||
else { | ||
} else { | ||
header = [0]; | ||
} | ||
var check = version === index_1.Bech32Version.Origin ? 1 : 0x3fffffff; | ||
const check = version === index_1.Bech32Version.Origin ? 1 : 0x3fffffff; | ||
return polymod(header.concat(data)) === check; | ||
} | ||
function createChecksum(hrp, data, bech32Version) { | ||
var values; | ||
}; | ||
const createChecksum = function (hrp, data, bech32Version) { | ||
let values; | ||
if (hrp) { | ||
values = hrpExpand(hrp).concat(data).concat([0, 0, 0, 0, 0, 0]); | ||
} | ||
else { | ||
} else { | ||
values = [0].concat(data).concat([0, 0, 0, 0, 0, 0]); | ||
} | ||
var chk = bech32Version === index_1.Bech32Version.Origin ? 1 : 0x3fffffff; | ||
var mod = polymod(values) ^ chk; | ||
var ret = []; | ||
for (var p = 0; p < 6; ++p) { | ||
const chk = bech32Version === index_1.Bech32Version.Origin ? 1 : 0x3fffffff; | ||
const mod = polymod(values) ^ chk; | ||
const ret = []; | ||
for (let p = 0; p < 6; ++p) { | ||
ret.push((mod >> (5 * (5 - p))) & 31); | ||
} | ||
return ret; | ||
} | ||
var encode = function (hrp, data, version) { | ||
var combined = data.concat(createChecksum(hrp, data, version)); | ||
var ret; | ||
}; | ||
const encode = function (hrp, data, version) { | ||
const combined = data.concat(createChecksum(hrp, data, version)); | ||
let ret; | ||
if (hrp) { | ||
ret = hrp + '1'; | ||
} | ||
else { | ||
} else { | ||
ret = ''; | ||
} | ||
for (var p = 0; p < combined.length; ++p) { | ||
for (let p = 0; p < combined.length; ++p) { | ||
ret += CHARSET.charAt(combined[p]); | ||
@@ -74,6 +71,6 @@ } | ||
}; | ||
var decodeBc32 = function (bechString) { | ||
var data = []; | ||
for (var p = 0; p < bechString.length; ++p) { | ||
var d = CHARSET.indexOf(bechString.charAt(p)); | ||
const decodeBc32 = function (bechString) { | ||
const data = []; | ||
for (let p = 0; p < bechString.length; ++p) { | ||
const d = CHARSET.indexOf(bechString.charAt(p)); | ||
if (d === -1) { | ||
@@ -84,3 +81,3 @@ return null; | ||
} | ||
if (!verifyChecksum(null, data, index_1.Bech32Version.bis)) { | ||
if (!verifyChecksum(undefined, data, index_1.Bech32Version.bis)) { | ||
return null; | ||
@@ -90,6 +87,6 @@ } | ||
}; | ||
var decode = function (bechString) { | ||
var p; | ||
var hasLower = false; | ||
var hasUpper = false; | ||
const decode = function (bechString) { | ||
let p; | ||
let hasLower = false; | ||
let hasUpper = false; | ||
for (p = 0; p < bechString.length; ++p) { | ||
@@ -110,3 +107,3 @@ if (bechString.charCodeAt(p) < 33 || bechString.charCodeAt(p) > 126) { | ||
bechString = bechString.toLowerCase(); | ||
var pos = bechString.lastIndexOf('1'); | ||
const pos = bechString.lastIndexOf('1'); | ||
if (pos === -1) { | ||
@@ -118,6 +115,6 @@ return decodeBc32(bechString); | ||
} | ||
var hrp = bechString.substring(0, pos); | ||
var data = []; | ||
const hrp = bechString.substring(0, pos); | ||
const data = []; | ||
for (p = pos + 1; p < bechString.length; ++p) { | ||
var d = CHARSET.indexOf(bechString.charAt(p)); | ||
const d = CHARSET.indexOf(bechString.charAt(p)); | ||
if (d === -1) { | ||
@@ -137,2 +134,2 @@ return null; | ||
}; | ||
//# sourceMappingURL=bech32.js.map | ||
//# sourceMappingURL=bech32.js.map |
export declare enum Bech32Version { | ||
Origin = 1, | ||
bis = 2 | ||
bis = 2, | ||
} | ||
export declare const decodeSegwitAddress: (hrp: string, addr: string) => { | ||
export declare const decodeSegwitAddress: ( | ||
hrp: string, | ||
addr: string, | ||
) => { | ||
version: number; | ||
program: number[]; | ||
} | null; | ||
export declare const encodeSegwitAddress: (hrp: string, version: number, program: Uint8Array | number[]) => string | null; | ||
export declare const encodeSegwitAddress: ( | ||
hrp: string, | ||
version: number, | ||
program: Uint8Array | number[], | ||
) => string | null; | ||
export declare const encodeBc32Data: (hex: string) => string; | ||
export declare const decodeBc32Data: (data: string) => null | string; |
@@ -1,20 +0,22 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
'use strict'; | ||
const __importDefault = | ||
(this && this.__importDefault) || | ||
function (mod) { | ||
return mod && mod.__esModule ? mod : { default: mod }; | ||
}; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
exports.decodeBc32Data = exports.encodeBc32Data = exports.encodeSegwitAddress = exports.decodeSegwitAddress = exports.Bech32Version = void 0; | ||
var bech32_1 = __importDefault(require("./bech32")); | ||
var Bech32Version; | ||
const bech32_1 = __importDefault(require('./bech32')); | ||
let Bech32Version; | ||
(function (Bech32Version) { | ||
Bech32Version[Bech32Version["Origin"] = 1] = "Origin"; | ||
Bech32Version[Bech32Version["bis"] = 2] = "bis"; | ||
})(Bech32Version = exports.Bech32Version || (exports.Bech32Version = {})); | ||
var convertBits = function (data, fromBits, toBits, pad) { | ||
var acc = 0; | ||
var bits = 0; | ||
var ret = []; | ||
var maxv = (1 << toBits) - 1; | ||
for (var p = 0; p < data.length; ++p) { | ||
var value = data[p]; | ||
Bech32Version[(Bech32Version['Origin'] = 1)] = 'Origin'; | ||
Bech32Version[(Bech32Version['bis'] = 2)] = 'bis'; | ||
})((Bech32Version = exports.Bech32Version || (exports.Bech32Version = {}))); | ||
const convertBits = function (data, fromBits, toBits, pad) { | ||
let acc = 0; | ||
let bits = 0; | ||
const ret = []; | ||
const maxv = (1 << toBits) - 1; | ||
for (let p = 0; p < data.length; ++p) { | ||
const value = data[p]; | ||
if (value < 0 || value >> fromBits !== 0) { | ||
@@ -34,4 +36,3 @@ return null; | ||
} | ||
} | ||
else if (bits >= fromBits || (acc << (toBits - bits)) & maxv) { | ||
} else if (bits >= fromBits || (acc << (toBits - bits)) & maxv) { | ||
return null; | ||
@@ -42,7 +43,7 @@ } | ||
exports.decodeSegwitAddress = function (hrp, addr) { | ||
var dec = bech32_1.default.decode(addr); | ||
const dec = bech32_1.default.decode(addr); | ||
if (dec === null || dec.hrp !== hrp || dec.data.length < 1 || dec.data[0] > 16) { | ||
return null; | ||
} | ||
var res = convertBits(Uint8Array.from(dec.data.slice(1)), 5, 8, false); | ||
const res = convertBits(Uint8Array.from(dec.data.slice(1)), 5, 8, false); | ||
if (res === null || res.length < 2 || res.length > 40) { | ||
@@ -57,3 +58,7 @@ return null; | ||
exports.encodeSegwitAddress = function (hrp, version, program) { | ||
var ret = bech32_1.default.encode(hrp, [version].concat(convertBits(program, 8, 5, true)), Bech32Version.Origin); | ||
const u82u5 = convertBits(program, 8, 5, true); | ||
if (!u82u5) { | ||
return null; | ||
} | ||
const ret = bech32_1.default.encode(hrp, [version].concat(u82u5), Bech32Version.Origin); | ||
if (exports.decodeSegwitAddress(hrp, ret) === null) { | ||
@@ -65,15 +70,20 @@ return null; | ||
exports.encodeBc32Data = function (hex) { | ||
var data = Buffer.from(hex, 'hex'); | ||
return bech32_1.default.encode(null, convertBits(data, 8, 5, true), Bech32Version.bis); | ||
const data = Buffer.from(hex, 'hex'); | ||
const u82u5 = convertBits(data, 8, 5, true); | ||
if (!u82u5) { | ||
throw new Error('invalid input'); | ||
} else { | ||
return bech32_1.default.encode(undefined, u82u5, Bech32Version.bis); | ||
} | ||
}; | ||
exports.decodeBc32Data = function (data) { | ||
var result = bech32_1.default.decode(data); | ||
const result = bech32_1.default.decode(data); | ||
if (result) { | ||
var res = convertBits(Buffer.from(result.data), 5, 8, false); | ||
return Buffer.from(res).toString('hex'); | ||
} | ||
else { | ||
const res = convertBits(Buffer.from(result.data), 5, 8, false); | ||
if (res) return Buffer.from(res).toString('hex'); | ||
return null; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
//# sourceMappingURL=index.js.map | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@cvbb/bc-bech32", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"main": "dist/index.js", | ||
@@ -9,3 +9,4 @@ "types": "src/index.ts", | ||
"clean": "rm -rf ./dist", | ||
"build": "npm run clean && npx tsc -p tsconfig.json -outDir ./dist" | ||
"build": "npm run clean && npx tsc -p tsconfig.json -outDir ./dist", | ||
"test": "jest" | ||
}, | ||
@@ -33,3 +34,3 @@ "files": [ | ||
}, | ||
"gitHead": "a847bcaf87f8be42fe2fab61111eca0a19ceffe4" | ||
"gitHead": "2541d8c33eafa93bcec9431be2008ed663d4fd40" | ||
} |
@@ -6,3 +6,3 @@ import { Bech32Version } from './index'; | ||
function polymod(values: number[]): number { | ||
const polymod = (values: number[]): number => { | ||
let chk = 1; | ||
@@ -19,5 +19,5 @@ for (let p = 0; p < values.length; ++p) { | ||
return chk; | ||
} | ||
}; | ||
function hrpExpand(hrp: string) { | ||
const hrpExpand = (hrp: string): number[] => { | ||
const ret: number[] = []; | ||
@@ -33,5 +33,5 @@ let p: number; | ||
return ret; | ||
} | ||
}; | ||
function verifyChecksum(hrp: string | undefined, data: number[], version: Bech32Version) { | ||
const verifyChecksum = (hrp: string | undefined, data: number[], version: Bech32Version): boolean => { | ||
let header: number[]; | ||
@@ -45,5 +45,5 @@ if (hrp) { | ||
return polymod(header.concat(data)) === check; | ||
} | ||
}; | ||
function createChecksum(hrp: string | undefined, data: number[], bech32Version: Bech32Version): number[] { | ||
const createChecksum = (hrp: string | undefined, data: number[], bech32Version: Bech32Version): number[] => { | ||
let values: number[]; | ||
@@ -64,3 +64,3 @@ if (hrp) { | ||
return ret; | ||
} | ||
}; | ||
@@ -81,3 +81,3 @@ const encode = (hrp: string | undefined, data: number[], version: Bech32Version): string => { | ||
const decodeBc32 = (bechString: string): { hrp: null; data: number[] } => { | ||
const decodeBc32 = (bechString: string): { hrp: null; data: number[] } | null => { | ||
const data: number[] = []; | ||
@@ -91,3 +91,3 @@ for (let p = 0; p < bechString.length; ++p) { | ||
} | ||
if (!verifyChecksum(null, data, Bech32Version.bis)) { | ||
if (!verifyChecksum(undefined, data, Bech32Version.bis)) { | ||
return null; | ||
@@ -94,0 +94,0 @@ } |
@@ -8,3 +8,3 @@ import bech32 from './bech32'; | ||
const convertBits = (data: Uint8Array | number[], fromBits: number, toBits: number, pad: boolean): number[] => { | ||
const convertBits = (data: Uint8Array | number[], fromBits: number, toBits: number, pad: boolean): number[] | null => { | ||
let acc = 0; | ||
@@ -52,3 +52,7 @@ let bits = 0; | ||
export const encodeSegwitAddress = (hrp: string, version: number, program: Uint8Array | number[]): string | null => { | ||
const ret = bech32.encode(hrp, [version].concat(convertBits(program, 8, 5, true)), Bech32Version.Origin); | ||
const u82u5 = convertBits(program, 8, 5, true); | ||
if (!u82u5) { | ||
return null; | ||
} | ||
const ret = bech32.encode(hrp, [version].concat(u82u5), Bech32Version.Origin); | ||
if (decodeSegwitAddress(hrp, ret) === null) { | ||
@@ -62,3 +66,8 @@ return null; | ||
const data = Buffer.from(hex, 'hex'); | ||
return bech32.encode(null, convertBits(data, 8, 5, true), Bech32Version.bis); | ||
const u82u5 = convertBits(data, 8, 5, true); | ||
if (!u82u5) { | ||
throw new Error('invalid input'); | ||
} else { | ||
return bech32.encode(undefined, u82u5, Bech32Version.bis); | ||
} | ||
}; | ||
@@ -70,3 +79,4 @@ | ||
const res = convertBits(Buffer.from(result.data), 5, 8, false); | ||
return Buffer.from(res).toString('hex'); | ||
if (res) return Buffer.from(res).toString('hex'); | ||
return null; | ||
} else { | ||
@@ -73,0 +83,0 @@ return null; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23045
446