Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_generatePair(seed, accountIndex = 0) {
if (!this._isValidSeed(seed)) {
alert("This is not a valid SEED!");
return null;
}
if(!this._isValidIndexAccount(accountIndex)) {
alert("Invalid account index!");
return null;
}
let index = hex_uint8(dec2hex(accountIndex, 4)); // 00000000 - FFFFFFFF
let context = blake.blake2bInit(32);
blake.blake2bUpdate(context, hex_uint8(seed));
blake.blake2bUpdate(context, index);
let key = blake.blake2bFinal(context);
return {
public_key: accountFromHexKey(uint8_hex(nacl.sign.keyPair.fromSecretKey(key).publicKey)),
private_key: uint8_hex(key),
}
}
label: key.label,
color: key.color,
secretKey: uint8_hex(key.priv),
});
break;
default: throw "Unsupported key type"
}
}
pack = JSON.stringify(pack);
pack = stringToHex(pack);
pack = new Buffer(pack, 'hex');
var context = blake.blake2bInit(32);
blake.blake2bUpdate(context, pack);
var checksum = blake.blake2bFinal(context);
var salt = new Buffer(nacl.randomBytes(16));
var key = pbkdf2.pbkdf2Sync(passPhrase, salt, iterations, 32, 'sha1');
var options = { mode: AES.CBC, padding: Iso10126 };
var encryptedBytes = AES.encrypt(pack, key, salt, options);
var payload = Buffer.concat([new Buffer(checksum), salt, encryptedBytes]);
// decrypt to check if wallet was corrupted during ecryption somehow
if(api.decryptAndCheck(payload).toString('hex') === false)
return api.pack(); // try again, shouldnt happen often
return payload.toString('hex');
}
signOpenBlock(walletAccount, previousBlock, sourceBlock, newBalancePadded, representative) {
const context = blake.blake2bInit(32, null);
blake.blake2bUpdate(context, this.util.hex.toUint8(STATE_BLOCK_PREAMBLE));
blake.blake2bUpdate(context, this.util.hex.toUint8(this.util.account.getAccountPublicKey(walletAccount.id)));
blake.blake2bUpdate(context, this.util.hex.toUint8(previousBlock));
blake.blake2bUpdate(context, this.util.hex.toUint8(this.util.account.getAccountPublicKey(representative)));
blake.blake2bUpdate(context, this.util.hex.toUint8(newBalancePadded));
blake.blake2bUpdate(context, this.util.hex.toUint8(sourceBlock));
const hashBytes = blake.blake2bFinal(context);
const privKey = walletAccount.keyPair.secretKey;
const signed = nacl.sign.detached(hashBytes, privKey);
const signature = this.util.hex.fromUint8(signed);
return signature;
}
exports.hashBlake2b256 = function(input) {
const context = blake2.blake2bInit(32)
blake2.blake2bUpdate(context, new Buffer(cbor.encode(input), 'hex'))
return (new Buffer(blake2.blake2bFinal(context))).toString('hex')
}
export function signSendBlock(
account,
frontier,
representative,
newBalancePadded,
toAccountID,
privKey
) {
const context = blake.blake2bInit(32, null)
blake.blake2bUpdate(context, hexToUint8(STATE_BLOCK_PREAMBLE))
blake.blake2bUpdate(context, hexToUint8(getAccountPublicKey(account)))
blake.blake2bUpdate(context, hexToUint8(frontier))
blake.blake2bUpdate(context, hexToUint8(getAccountPublicKey(representative)))
blake.blake2bUpdate(context, hexToUint8(newBalancePadded))
blake.blake2bUpdate(context, hexToUint8(getAccountPublicKey(toAccountID)))
const hashBytes = blake.blake2bFinal(context)
const signed = nacl.sign.detached(hashBytes, privKey)
const signature = uint8ToHex(signed)
return signature
}
api.decryptAndCheck = function(data) {
var bytes = new Buffer(data, 'hex');
var checksum = bytes.slice(0, 32);
var salt = bytes.slice(32, 48);
var payload = bytes.slice(48);
var key = pbkdf2.pbkdf2Sync(passPhrase, salt, iterations, 32, 'sha1');
var options = {};
options.padding = options.padding || Iso10126;
var decryptedBytes = AES.decrypt(payload, key, salt, options);
var context = blake.blake2bInit(32);
blake.blake2bUpdate(context, decryptedBytes);
var hash = uint8_hex(blake.blake2bFinal(context));
if (hash != checksum.toString('hex').toUpperCase())
return false;
return decryptedBytes;
}
api.checkWork = function (work, blockHash = false) {
if (blockHash === false) {
blockHash = api.getPrevious();
}
var t = hex_uint8(MAIN_NET_WORK_THRESHOLD);
var context = blake.blake2bInit(8, null);
blake.blake2bUpdate(context, hex_uint8(work).reverse());
blake.blake2bUpdate(context, hex_uint8(blockHash));
var threshold = blake.blake2bFinal(context).reverse();
if (threshold[0] == t[0])
if (threshold[1] == t[1])
if (threshold[2] == t[2])
if (threshold[3] >= t[3])
return true;
return false;
}
signChangeBlock(walletAccount, toAcct, representativeAccount, balancePadded, link) {
let context = blake.blake2bInit(32, null);
blake.blake2bUpdate(context, this.util.hex.toUint8(STATE_BLOCK_PREAMBLE));
blake.blake2bUpdate(context, this.util.hex.toUint8(this.util.account.getAccountPublicKey(walletAccount.id)));
blake.blake2bUpdate(context, this.util.hex.toUint8(toAcct.frontier));
blake.blake2bUpdate(context, this.util.hex.toUint8(this.util.account.getAccountPublicKey(representativeAccount)));
blake.blake2bUpdate(context, this.util.hex.toUint8(balancePadded));
blake.blake2bUpdate(context, this.util.hex.toUint8(link));
const hashBytes = blake.blake2bFinal(context);
const privKey = walletAccount.keyPair.secretKey;
const signed = nacl.sign.detached(hashBytes, privKey);
const signature = this.util.hex.fromUint8(signed);
return signature;
}
api.checkWork = function (work) {
var blockHash = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
if (blockHash === false) {
blockHash = api.getPrevious();
}
var t = (0, _functions.hex_uint8)(MAIN_NET_WORK_THRESHOLD);
var context = blake.blake2bInit(8, null);
blake.blake2bUpdate(context, (0, _functions.hex_uint8)(work).reverse());
blake.blake2bUpdate(context, (0, _functions.hex_uint8)(blockHash));
var threshold = blake.blake2bFinal(context).reverse();
if (threshold[0] == t[0]) if (threshold[1] == t[1]) if (threshold[2] == t[2]) if (threshold[3] >= t[3]) return true;
return false;
};
export function signOpenBlock(
account,
previousBlock,
sourceBlock,
newBalancePadded,
representative,
privKey
) {
const context = blake.blake2bInit(32, null)
blake.blake2bUpdate(context, hexToUint8(STATE_BLOCK_PREAMBLE))
blake.blake2bUpdate(context, hexToUint8(getAccountPublicKey(account)))
blake.blake2bUpdate(context, hexToUint8(previousBlock))
blake.blake2bUpdate(context, hexToUint8(getAccountPublicKey(representative)))
blake.blake2bUpdate(context, hexToUint8(newBalancePadded))
blake.blake2bUpdate(context, hexToUint8(sourceBlock))
const hashBytes = blake.blake2bFinal(context)
const signed = nacl.sign.detached(hashBytes, privKey)
const signature = uint8ToHex(signed)
return signature
}