bitcoinfiles-node
Version:
Upload and Download files to Bitcoin Cash blockchain with Node and web
47 lines (40 loc) • 1.25 kB
JavaScript
class BfpUtils {
static getPushDataOpcode(data) {
let length = data.length
if (length === 0)
return [0x4c, 0x00]
else if (length < 76)
return length
else if (length < 256)
return [0x4c, length]
else
throw Error("Pushdata too large")
}
static int2FixedBuffer(amount, size) {
let hex = amount.toString(16);
hex = hex.padStart(size * 2, '0');
if (hex.length % 2) hex = '0' + hex;
return Buffer.from(hex, 'hex');
}
static encodeScript(script) {
const bufferSize = script.reduce((acc, cur) => {
if (Array.isArray(cur)) return acc + cur.length
else return acc + 1
}, 0)
const buffer = Buffer.allocUnsafe(bufferSize)
let offset = 0
script.forEach((scriptItem) => {
if (Array.isArray(scriptItem)) {
scriptItem.forEach((item) => {
buffer.writeUInt8(item, offset)
offset += 1
})
} else {
buffer.writeUInt8(scriptItem, offset)
offset += 1
}
})
return buffer
}
}
module.exports = BfpUtils