identity.rs icon indicating copy to clipboard operation
identity.rs copied to clipboard

[Doc] Add a mnemonic-seed-to-identity example

Open PhilippGackstatter opened this issue 2 years ago • 0 comments

Description

By somewhat popular demand, we should consider adding an example that creates an identity from a BIP39 mnemonic seed.

In the example, we might want to describe the trade-off between using a mnemonic seed vs. storing the key in a Storage / stronghold.

Reference material

A while back I posted such an example on Discord, but only in JS, and it has some problems that need fixing, in particular how to go from the 64 byte seed to a 32 byte ed25519 private key. Truncating is maybe a bad idea, applying a key derivation or hash function might be safer. If the example is used, it needs to be updated (still uses the base58 functions that we removed). Posting it for reference:

import { Document, KeyPair, KeyType, Storage } from '@iota/identity-wasm';
import base58 from 'bs58';
import * as bip39 from 'bip39';
import * as ed from '@noble/ed25519';

async function createIdentity() {
    const mnemonic = bip39.generateMnemonic();
    console.log(mnemonic);

    // Truncate the 64 byte seed, because ed25519 private keys are 32 bytes.
    const seed = (await bip39.mnemonicToSeed(mnemonic)).slice(0, 32);

    const publicKey = await ed.getPublicKey(seed)
    const keypair = KeyPair.fromBase58(KeyType.Ed25519, base58.encode(publicKey), base58.encode(seed));

    const doc = new Document(keypair);

    doc.signSelf(keypair, doc.defaultSigningMethod().id());
    console.log(doc)
}

To-do List

Create a task-specific to-do list

  • [ ] Add an mnemonic seed identity creation example
  • [ ] Determine how to safely use the 64 byte seed as input for an ed25519 key

PhilippGackstatter avatar May 07 '22 14:05 PhilippGackstatter