Last active
February 4, 2020 21:59
-
-
Save RyanHendricks/032dcc0343596be7f584773de735bf00 to your computer and use it in GitHub Desktop.
decoder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SOURCE ----> https://github.com/sipa/bech32/blob/master/ref/javascript/bech32.js | |
// Copyright (c) 2017 Pieter Wuille | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
// THE SOFTWARE. | |
const CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l'; | |
const GENERATOR = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]; | |
module.exports = { | |
decode, | |
encode, | |
}; | |
function polymod(values) { | |
let chk = 1; | |
for (let p = 0; p < values.length; ++p) { | |
const top = chk >> 25; | |
chk = (chk & 0x1ffffff) << 5 ^ values[p]; | |
for (let i = 0; i < 5; ++i) { | |
if ((top >> i) & 1) { | |
chk ^= GENERATOR[i]; | |
} | |
} | |
} | |
return chk; | |
} | |
function hrpExpand(hrp) { | |
const ret = []; | |
let p; | |
for (p = 0; p < hrp.length; ++p) { | |
ret.push(hrp.charCodeAt(p) >> 5); | |
} | |
ret.push(0); | |
for (p = 0; p < hrp.length; ++p) { | |
ret.push(hrp.charCodeAt(p) & 31); | |
} | |
return ret; | |
} | |
function verifyChecksum(hrp, data) { | |
return polymod(hrpExpand(hrp).concat(data)) === 1; | |
} | |
function createChecksum(hrp, data) { | |
const values = hrpExpand(hrp).concat(data).concat([0, 0, 0, 0, 0, 0]); | |
const mod = polymod(values) ^ 1; | |
const ret = []; | |
for (let p = 0; p < 6; ++p) { | |
ret.push((mod >> 5 * (5 - p)) & 31); | |
} | |
return ret; | |
} | |
function encode(hrp, data) { | |
const combined = data.concat(createChecksum(hrp, data)); | |
let ret = `${hrp}1`; | |
for (let p = 0; p < combined.length; ++p) { | |
ret += CHARSET.charAt(combined[p]); | |
} | |
return ret; | |
} | |
function decode(bechString) { | |
let p; | |
let has_lower = false; | |
let has_upper = false; | |
for (p = 0; p < bechString.length; ++p) { | |
if (bechString.charCodeAt(p) < 33 || bechString.charCodeAt(p) > 126) { | |
return null; | |
} | |
if (bechString.charCodeAt(p) >= 97 && bechString.charCodeAt(p) <= 122) { | |
has_lower = true; | |
} | |
if (bechString.charCodeAt(p) >= 65 && bechString.charCodeAt(p) <= 90) { | |
has_upper = true; | |
} | |
} | |
if (has_lower && has_upper) { | |
return null; | |
} | |
bechString = bechString.toLowerCase(); | |
const pos = bechString.lastIndexOf('1'); | |
if (pos < 1 || pos + 7 > bechString.length || bechString.length > 90) { | |
return null; | |
} | |
const hrp = bechString.substring(0, pos); | |
const data = []; | |
for (p = pos + 1; p < bechString.length; ++p) { | |
const d = CHARSET.indexOf(bechString.charAt(p)); | |
if (d === -1) { | |
return null; | |
} | |
data.push(d); | |
} | |
if (!verifyChecksum(hrp, data)) { | |
return null; | |
} | |
return { hrp, data: data.slice(0, data.length - 6) }; | |
} | |
// | |
function main() { | |
const str = 'fva169x02pq8km0rvum8tgqseexaq7dk5mx5tftt9j'; // replace with whichever address you wish to decode/encode | |
console.log('starting with: ', str); | |
const decoded = decode(str); | |
console.log('first decode to get: ', decoded); | |
const encoded = encode('faa', decoded.data); | |
console.log('then reencode to : ', encoded); | |
} | |
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const crypto = require('crypto') | |
// Replace TRANSACTION with the transaction to decode | |
const txn = "TRANSACTION" | |
function decode(txstring) { | |
const tx = txstring; | |
const rawtx = crypto.createHash('sha256') | |
.update(Buffer.from(tx, 'base64')) | |
.digest() | |
.toString(`hex`); | |
console.log(rawtx) | |
}; | |
decode(txn); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment