bitcoyne
Version:
Keybase Bitcoin Library (2nd Edition)
157 lines (142 loc) • 4.11 kB
JavaScript
// Generated by IcedCoffeeScript 108.0.11
(function() {
var base32, bufeq_secure, checksum, crc16_xmodem, decode_check, encode_check, is_valid, version_byte, version_bytes;
bufeq_secure = require('pgp-utils').util.bufeq_secure;
base32 = require('base32.js');
crc16_xmodem = function(x) {
var byte, code, crc, _i, _len;
crc = 0;
for (_i = 0, _len = x.length; _i < _len; _i++) {
byte = x[_i];
code = crc >>> 8 & 0xFF;
code ^= byte & 0xFF;
code ^= code >>> 4;
crc = crc << 8 & 0xFFFF;
crc ^= code;
code = code << 5 & 0xFFFF;
crc ^= code;
code = code << 7 & 0xFFFF;
crc ^= code;
}
return crc;
};
exports.checksum = checksum = function(version, data) {
var b, crc, ret;
ret = Buffer.from([0, 0]);
b = Buffer.concat([version, data]);
crc = crc16_xmodem(b);
ret.writeUInt16LE(crc, 0);
return ret;
};
version_bytes = {
ed25519PublicKey: 6 << 3,
ed25519SecretSeed: 18 << 3,
preAuthTx: 19 << 3,
sha256Hash: 23 << 3
};
version_byte = function(typ) {
var b;
if ((b = version_bytes[typ])) {
return Buffer.from([b]);
} else {
return null;
}
};
encode_check = function(typ, data) {
var buf, version;
if (!(data != null ? data.length : void 0)) {
throw new Error("cannot encode null data");
}
if ((version = version_byte(typ)) == null) {
throw new Error("" + versionByteName + " is not a valid version byte name. Expected one of \"ed25519PublicKey\", \"ed25519SecretSeed\", \"preAuthTx\", \"sha256Hash\" ");
}
buf = Buffer.concat([version, data, checksum(version, data)]);
return base32.encode(buf).toUpperCase();
};
decode_check = function(typ, s) {
var b, crc, data, version;
if (typeof s !== 'string') {
throw new Error("Bad value, expected a string");
}
b = base32.decode(s.toLowerCase());
if ((version = version_byte(typ)) == null) {
throw new Error("" + versionByteName + " is not a valid version byte name. Expected one of \"ed25519PublicKey\", \"ed25519SecretSeed\", \"preAuthTx\", \"sha256Hash\" ");
}
if (b.length < 5) {
throw new Error("need at least 5 bytes of data");
}
version = b.slice(0, 1);
data = b.slice(1, -2);
crc = b.slice(-2);
if (base32.encode(b) !== s) {
throw new Error("bad encoding");
}
if (!bufeq_secure(crc, checksum(version, data))) {
throw new Error("Bad checksum");
}
if (!bufeq_secure(version_byte(typ), version)) {
throw new Error("Unexpected version byte");
}
if (encode_check(typ, data) !== s) {
throw new Error("failed encode/decode round-trip check");
}
return data;
};
is_valid = function(typ, s) {
var decoded, e;
if ((s != null ? s.length : void 0) !== 56) {
return false;
}
try {
decoded = decode_check(typ, s);
return decoded.length === 32;
} catch (_error) {
e = _error;
return false;
}
};
exports.public_key = {
encode: function(d) {
return encode_check('ed25519PublicKey', d);
},
decode: function(s) {
return decode_check('ed25519PublicKey', s);
},
is_valid: function(s) {
return is_valid('ed25519PublicKey', s);
}
};
exports.secret_key = {
encode: function(d) {
return encode_check('ed25519SecretSeed', d);
},
decode: function(s) {
return decode_check('ed25519SecretSeed', s);
},
is_valid: function(s) {
return is_valid('ed25519SecretSeed', s);
}
};
exports.pre_tx_auth = {
encode: function(d) {
return encode_check('preAuthTx', d);
},
decode: function(s) {
return decode_check('preAuthTx', s);
},
is_valid: function(s) {
return is_valid('preAuthTx', s);
}
};
exports.hash = {
encode: function(d) {
return encode_check('sha256Hash', d);
},
decode: function(s) {
return decode_check('sha256Hash', s);
},
is_valid: function(s) {
return is_valid('sha256Hash', s);
}
};
}).call(this);