Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function main () {
// Create mnemonic string for Alice using BIP39
const mnemonicAlice = mnemonicGenerate();
console.log(`Generated mnemonic: ${mnemonicAlice}`);
// Validate the mnemic string that was generated
const isValidMnemonic = mnemonicValidate(mnemonicAlice);
console.log(`isValidMnemonic: ${isValidMnemonic}`);
// Create valid seed from mnemonic as u8a and convert it to a string
// FIXME - Replace with mnemonicToSeed once exposed
const seedAlice = mnemonicToSeed(mnemonicAlice);
// Generate new public/secret keypair for Alice from the supplied seed
const { secretKey, publicKey } = naclKeypairFromSeed(seedAlice);
// Encrypt, Sign and Validate the message. See Example 'Sign & Verify Message'
}
private seedValidate ({ suri, type }: RequestSeedValidate): ResponseSeedValidate {
const { phrase } = keyExtractSuri(suri);
if (isHex(phrase)) {
assert(isHex(phrase, 256), 'Hex seed needs to be 256-bits');
} else {
// sadly isHex detects as string, so we need a cast here
assert(SEED_LENGTHS.includes((phrase as string).split(' ').length), `Mnemonic needs to contain ${SEED_LENGTHS.join(', ')} words`);
assert(mnemonicValidate(phrase), 'Not a valid mnemonic seed');
}
return {
address: keyring.createFromUri(suri, {}, type).address,
suri
};
}
useEffect((): void => {
try {
const { phrase } = keyExtractSuri(suri);
assert(mnemonicValidate(phrase), 'Invalid mnemonic phrase');
setPublicKey(u8aToHex(keyring.createFromUri(suri, {}, crypto).publicKey));
} catch (error) {
setPublicKey(EMPTY_KEY);
}
}, [crypto, suri]);
function updateAddress (seed: string, derivePath: string, seedType: SeedType, pairType: KeypairType): AddressState {
const deriveError = deriveValidate(seed, derivePath, pairType);
let isSeedValid = seedType === 'raw'
? rawValidate(seed)
: mnemonicValidate(seed);
let address: string | null = null;
if (!deriveError && isSeedValid) {
try {
address = addressFromSeed(seed, derivePath, pairType);
} catch (error) {
isSeedValid = false;
}
}
return {
address,
deriveError,
derivePath,
isSeedValid,
pairType,
function updateAddress (seed: string, derivePath: string, seedType: SeedType, pairType: KeypairType): AddressState {
const deriveError = deriveValidate(seed, derivePath, pairType);
let isSeedValid = seedType === 'raw'
? rawValidate(seed)
: mnemonicValidate(seed);
let address: string | null = null;
if (!deriveError && isSeedValid) {
try {
address = addressFromSeed(seed, derivePath, pairType);
} catch (error) {
isSeedValid = false;
}
}
return {
address,
deriveError,
derivePath,
isSeedValid,
pairType,
function generateValidMnemonic() {
const maxCount = 3;
let count = 0;
let isValidMnemonic = false;
let mnemonic;
while (!isValidMnemonic) {
if (count > maxCount) {
throw new Error('could not generate valid mnemonic!');
}
mnemonic = mnemonicGenerate();
isValidMnemonic = mnemonicValidate(mnemonic);
count++;
}
return mnemonic;
}