Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function publicKeyFromId (id) {
// The last byte is the checksum, slice it off
return new Uint8Array(Base58.decode(id).slice(0, 32))
}
static fromWif(_private_wif) {
var private_wif = new Buffer(base58.decode(_private_wif));
var version = private_wif.readUInt8(0);
assert.equal(0x80, version, `Expected version ${0x80}, instead got ${version}`);
// checksum includes the version
var private_key = private_wif.slice(0, -4);
var checksum = private_wif.slice(-4);
var new_checksum = hash.sha256(private_key);
new_checksum = hash.sha256(new_checksum);
new_checksum = new_checksum.slice(0, 4);
if (checksum.toString() !== new_checksum.toString())
throw new Error('Invalid WIF key (checksum miss-match)')
private_key = private_key.slice(1);
return PrivateKey.fromBuffer(private_key);
}
public verifySignature(signer: string, data: Buffer | string, signature: string): boolean {
// TODO remove dummy keys
const publicKey: PublicKey = this.nodePublicKeys.get(signer) || this.nodePublicKeys.get("dummy");
logger.debug(`Got public key ${base58.encode(publicKey)} for ${signer}`);
if (!publicKey) {
return false;
}
const dataBuf: Buffer = (typeof(data) === "string") ? new Buffer(data, "utf8") : data;
const digest: Buffer = crypto.createHash("SHA256").update(dataBuf).digest();
return ec.verify(digest, base58.decode(signature), publicKey);
}
function getIpfsHashFromBytes32(bytes32Hex) {
// Add our default ipfs values for first 2 bytes:
// function:0x12=sha2, size:0x20=256 bits
// and cut off leading "0x"
const hashHex = '1220' + bytes32Hex.slice(2)
const hashBytes = Buffer.from(hashHex, 'hex')
const hashStr = bs58.encode(hashBytes)
return hashStr
}
function validateId (id) {
var idRegex = /^[1-9ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{40,55}$/
if (!idRegex.test(id)) return false
var bytes = Base58.decode(id)
if (bytes.length !== 33) return false
var hash = new BLAKE2s(1)
hash.update(bytes.slice(0, 32))
return hash.digest()[0] === bytes[32]
}
function bytes32ToIPFSHash(hash_hex) {
//console.log('bytes32ToIPFSHash starts with hash_buffer', hash_hex.replace(/^0x/, ''));
var buf = new Buffer(hash_hex.replace(/^0x/, '1220'), 'hex')
return bs58.encode(buf)
}
toPublicKeyString(address_prefix = 'STM') {
if(this.pubdata) return address_prefix + this.pubdata
const pub_buf = this.toBuffer();
const checksum = hash.ripemd160(pub_buf);
const addy = Buffer.concat([pub_buf, checksum.slice(0, 4)]);
this.pubdata = base58.encode(addy)
return address_prefix + this.pubdata;
}
toPublicKeyString(address_prefix = config.get('address_prefix')) {
if(this.pubdata) return address_prefix + this.pubdata
const pub_buf = this.toBuffer();
const checksum = hash.ripemd160(pub_buf);
const addy = Buffer.concat([pub_buf, checksum.slice(0, 4)]);
this.pubdata = base58.encode(addy)
return address_prefix + this.pubdata;
}
toPublicKeyString(address_prefix = config.address_prefix) {
if(this.pubdata) return address_prefix + this.pubdata
const pub_buf = this.toBuffer();
const checksum = hash.ripemd160(pub_buf);
const addy = Buffer.concat([pub_buf, checksum.slice(0, 4)]);
this.pubdata = base58.encode(addy)
return address_prefix + this.pubdata;
}
toAddressString(address_prefix = ChainConfig.address_prefix) {
let pub_buf = this.toBuffer();
let pub_sha = sha512(pub_buf);
let addy = ripemd160(pub_sha);
let checksum = ripemd160(addy);
addy = Buffer.concat([addy, checksum.slice(0, 4)]);
return address_prefix + encode(addy);
}