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),
}
}
_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),
}
}
api.load = function (data) {
var bytes = new Buffer(data, 'hex');
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 = (0, _functions.uint8_hex)(blake.blake2bFinal(context));
if (hash != checksum.toString('hex').toUpperCase()) throw "Wallet is corrupted or has been tampered.";
var walletData = JSON.parse(decryptedBytes.toString('utf8'));
if (typeof walletData.loginKey == 'undefined') { walletData.loginKey = false; }
if(walletData.loginKey) {
console.log("returning");
return api.loadweb(data);
}
seed = (0, _functions.hex_uint8)(walletData.seed);
lastKeyFromSeed = walletData.last;
recentTxs = walletData.recent;
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;
}
export function signChangeBlock(
account,
frontier,
representative,
newBalancePadded,
link,
privKey
) {
let 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(link))
const hashBytes = blake.blake2bFinal(context)
const signed = nacl.sign.detached(hashBytes, privKey)
const signature = uint8ToHex(signed)
return signature
}
function generateAccountSecretKeyBytes(seedBytes, accountIndex) {
const accountBytes = hexToUint8(decToHex(accountIndex, 4));
const context = blake.blake2bInit(32);
blake.blake2bUpdate(context, seedBytes);
blake.blake2bUpdate(context, accountBytes);
const newKey = blake.blake2bFinal(context);
return newKey;
}
api.checkWork = function (hash, work) {
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(hash));
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;
}
_private.newKeyDataFromSeed = function(index) {
if (seed.length != 32)
throw "Seed should be set first.";
let index_bytes = hex_uint8(dec2hex(index, 4));
let context = blake.blake2bInit(32);
blake.blake2bUpdate(context, seed);
blake.blake2bUpdate(context, index_bytes);
let secretKey = blake.blake2bFinal(context);
let publicKey = nacl.sign.keyPair.fromSecretKey(secretKey).publicKey;
return {
type: KEY_TYPE.SEEDED,
seedIndex: index,
priv: secretKey,
pub: publicKey,
};
}
api.newKeyFromSeed = function () {
if (seed.length != 32) throw "Seed should be set first.";
var index = lastKeyFromSeed + 1;
index = (0, _functions.hex_uint8)((0, _functions.dec2hex)(index, 4));
var context = blake.blake2bInit(32);
blake.blake2bUpdate(context, seed);
blake.blake2bUpdate(context, index);
var newKey = blake.blake2bFinal(context);
lastKeyFromSeed++;
logger.log("New key generated");
api.addSecretKey((0, _functions.uint8_hex)(newKey));
return (0, _functions.accountFromHexKey)((0, _functions.uint8_hex)(nacl.sign.keyPair.fromSecretKey(newKey).publicKey));
};
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;
}