You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
738 B
32 lines
738 B
// https://en.bitcoin.it/wiki/Base58Check_encoding
|
|
var assert = require('assert')
|
|
var base58 = require('./base58')
|
|
var crypto = require('./crypto')
|
|
|
|
// Encode a buffer as a base58-check-encoded string
|
|
function encode(payload) {
|
|
var checksum = crypto.hash256(payload).slice(0, 4)
|
|
|
|
return base58.encode(Buffer.concat([
|
|
payload,
|
|
checksum
|
|
]))
|
|
}
|
|
|
|
// Decode a base58-check-encoded string to a buffer
|
|
function decode(string) {
|
|
var buffer = base58.decode(string)
|
|
|
|
var payload = buffer.slice(0, -4)
|
|
var checksum = buffer.slice(-4)
|
|
var newChecksum = crypto.hash256(payload).slice(0, 4)
|
|
|
|
assert.deepEqual(newChecksum, checksum, 'Invalid checksum')
|
|
|
|
return payload
|
|
}
|
|
|
|
module.exports = {
|
|
encode: encode,
|
|
decode: decode
|
|
}
|
|
|