Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
getSeedFromWalletAccount = async (
wallet: TheWallet,
address: string,
passphrase: string
): Promise => {
try {
const originalWallet = this.reloadWallet(wallet);
await originalWallet.unlock(passphrase);
assert(wallet.accounts, `missing accounts`);
assert(address, `missing address`);
const json = await originalWallet.exportAccount(address, passphrase);
const decodeMsg = decode(passphrase, hexToU8a(json.encoded));
let publicKey = null;
let secretKey = null;
if (decodeMsg.secretKey.length === 64) {
publicKey = decodeMsg.publicKey;
secretKey = decodeMsg.secretKey;
Logger.debug(`api::getSeedFromWalletAccount decoded length: ${decodeMsg.secretKey.length}`);
} else {
// default is sr
const pair = naclFromSeed(decodeMsg.secretKey);
publicKey = pair.publicKey;
secretKey = pair.secretKey;
Logger.debug(`api::getSeedFromWalletAccount ed : ${pair}`);
}
// const { publicKey, secretKey } = decodeMsg;
try {
const originalWallet = this.reloadWallet(wallet);
await originalWallet.unlock(passphrase);
assert(wallet.accounts, `missing accounts`);
assert(address, `missing address`);
const json = await originalWallet.exportAccount(address, passphrase);
const decodeMsg = decode(passphrase, hexToU8a(json.encoded));
let publicKey = null;
let secretKey = null;
if (decodeMsg.secretKey.length === 64) {
publicKey = decodeMsg.publicKey;
secretKey = decodeMsg.secretKey;
Logger.debug(`api::getSeedFromWalletAccount decoded length: ${decodeMsg.secretKey.length}`);
} else {
// default is sr
const pair = naclFromSeed(decodeMsg.secretKey);
publicKey = pair.publicKey;
secretKey = pair.secretKey;
Logger.debug(`api::getSeedFromWalletAccount ed : ${pair}`);
}
// const { publicKey, secretKey } = decodeMsg;
const SEC_LENGTH = 64;
const SEED_LENGTH = 32;
const ZERO_STR = '0x00';
const seedU8a = secretKey.subarray(0, SEC_LENGTH - SEED_LENGTH);
const seed = u8aToHex(seedU8a);
assert(publicKey && publicKey.length === 32, 'Expected valid publicKey, 32-bytes');
assert(secretKey && secretKey.length === 64, 'Expected valid secretKey, 64-bytes');
assert(seed && seed.length > ZERO_STR.length, 'Expected valid seed, 32-bytes');
Logger.debug(`api::getSeedFromWalletAccount seed: ${seed.length}`);
Logger.debug(`api::getSeedFromWalletAccount decoded length: ${decodeMsg.secretKey.length}`);
} else {
// default is sr
const pair = naclFromSeed(decodeMsg.secretKey);
publicKey = pair.publicKey;
secretKey = pair.secretKey;
Logger.debug(`api::getSeedFromWalletAccount ed : ${pair}`);
}
// const { publicKey, secretKey } = decodeMsg;
const SEC_LENGTH = 64;
const SEED_LENGTH = 32;
const ZERO_STR = '0x00';
const seedU8a = secretKey.subarray(0, SEC_LENGTH - SEED_LENGTH);
const seed = u8aToHex(seedU8a);
assert(publicKey && publicKey.length === 32, 'Expected valid publicKey, 32-bytes');
assert(secretKey && secretKey.length === 64, 'Expected valid secretKey, 64-bytes');
assert(seed && seed.length > ZERO_STR.length, 'Expected valid seed, 32-bytes');
Logger.debug(`api::getSeedFromWalletAccount seed: ${seed.length}`);
return seed;
} catch (error) {
Logger.error('api::getSeedFromWalletAccount error: ' + stringifyError(error));
throw new GenericApiError();
}
};
getAddressFromSeed = async (seed, keyType = 'ed25519') => {
try {
const keyring = new SimpleKeyring();
Logger.debug(`api::getAddressFromSeed seed: ${seed}`);
const seedHex = hexToU8a(seed);
const account = await keyring.addFromSeed(seedHex, {}, keyType);
const address = account.address();
Logger.debug(`api::getAddressFromSeed: ${address}`);
return address;
} catch (error) {
Logger.error('api::getAddressFromSeed error: ' + stringifyError(error));
throw new GenericApiError();
}
};