const BN = require('bn.js')
const hexToBytes = (a) => {
return new BN(a, 16).toArray(null, a.length / 2)
}
const decodeToastOfflineCode = (code) => {
let offlinecode = code.trim().toUpperCase().replace(/[^A-Z0-9]/g, '')
let accID
let ledID
let fee
if (/^[A-F0-9]+$/m.test(offlinecode)) {
try {
let compression = hexToBytes(offlinecode.slice(2, 4))
offlinecode = offlinecode.slice(4)
let removedZerosLedId = compression & 7
let removedZerosAccId = compression >> 3
accID = '0'.repeat(removedZerosAccId) + offlinecode.slice(0, 8 - removedZerosAccId)
offlinecode = offlinecode.slice(8 - removedZerosAccId)
ledID = '0'.repeat(removedZerosLedId) + offlinecode.slice(0, 8 - removedZerosLedId)
offlinecode = offlinecode.slice(8 - removedZerosLedId)
while (offlinecode.length < 8) offlinecode = '0' + offlinecode
fee = offlinecode
offlinecode = accID + ledID + fee
} catch (e) {
console.log(e.message)
}
}
return {
accID: parseInt('0x' + accID),
ledID: parseInt('0x' + ledID),
fee: parseInt('0x' + fee) / 1000000
}
}
console.log(decodeToastOfflineCode('703A 12C5 173C'))