First iteration of account creation. More refactoring to come

This commit is contained in:
Matthew Aaron Raymer
2022-12-09 18:23:53 +08:00
parent cbad2e7308
commit b58c0ce820
7 changed files with 467 additions and 58 deletions

View File

@@ -167,77 +167,62 @@
<script lang="ts">
import { Options, Vue } from "vue-class-component";
import { getRandomBytesSync } from "ethereum-cryptography/random";
import { entropyToMnemonic } from "ethereum-cryptography/bip39";
import { wordlist } from "ethereum-cryptography/bip39/wordlists/english";
import { HDNode } from "@ethersproject/hdnode";
import { useAppStore } from "../store/app";
import { useAccountStore } from "../store/account";
import { createIdentifier, deriveAddress, newIdentifier } from "../libs/crypto";
import { IIdentifier } from "@veramo/core";
import * as R from "ramda";
import R from "ramda";
@Options({
components: {},
})
export default class AccountViewView extends Vue {
created() {
console.log("Component has been created!");
const entropy = getRandomBytesSync(32);
const mnemonic = entropyToMnemonic(entropy, wordlist);
const mnemonicPassword = "HardCodedPassword";
console.log(entropy, mnemonic);
this.importAndStoreIdentifier(mnemonic, mnemonicPassword, false, []);
}
importAndStoreIdentifier(
mnemonic: string,
mnemonicPassword: string,
toLowercase: boolean,
previousIdentifiers: Array<IIdentifier>
) {
const UPORT_ROOT_DERIVATION_PATH = "m/7696500'/0'/0'/0'";
mnemonic = mnemonic.trim().toLowerCase();
console.log(mnemonic);
const hdnode: HDNode = HDNode.fromMnemonic(mnemonic);
const rootNode: HDNode = hdnode.derivePath(UPORT_ROOT_DERIVATION_PATH);
const privateHex = rootNode.privateKey.substring(2); // original starts with '0x'
const publicHex = rootNode.publicKey.substring(2); // original starts with '0x'
let address = rootNode.address;
const previousIdentifiers: Array<IIdentifier> = [];
const toLowercase = true;
const appStore = useAppStore();
const accountStore = useAccountStore();
console.log(address, privateHex, publicHex);
if (appStore._condition == "uninitialized") {
const mnemonic = createIdentifier();
const prevIds = previousIdentifiers || [];
if (toLowercase) {
const foundEqual = R.find(
(id: IIdentifier) => id.did.split(":")[2] === address,
prevIds
);
if (foundEqual) {
// They're trying to create a lowercase version of one that exists in normal case.
// (We really should notify the user.)
// appStore.dispatch(appSlice.actions.addLog({log: true, msg: "Will create a normal-case version of the DID since a regular version exists."}))
const [address, privateHex, publicHex, UPORT_ROOT_DERIVATION_PATH] =
deriveAddress(mnemonic);
//appStore.dispatch(appSlice.actions.addLog({log: false, msg: "... derived keys and address..."}))
const prevIds = previousIdentifiers || [];
let addr = address;
if (toLowercase) {
const foundEqual = R.find(
(id: IIdentifier) => id.did.split(":")[2] === address,
prevIds
);
if (foundEqual) {
// appStore.dispatch(appSlice.actions.addLog({log: true, msg: "Will create a normal-case version of the DID since a regular version exists."}))
} else {
addr = address.toLowerCase();
}
} else {
address = address.toLowerCase();
// They're not trying to convert to lowercase.
const foundLower = R.find(
(id: IIdentifier) => id.did.split(":")[2] === address.toLowerCase(),
prevIds
);
if (foundLower) {
// appStore.dispatch(appSlice.actions.addLog({log: true, msg: "Will create a lowercase version of the DID since a lowercase version exists."}))
addr = address.toLowerCase();
}
}
} else {
// They're not trying to convert to lowercase.
const foundLower = R.find(
(id: IIdentifier) => id.did.split(":")[2] === address.toLowerCase(),
prevIds
const newId = newIdentifier(
addr,
publicHex,
privateHex,
UPORT_ROOT_DERIVATION_PATH
);
if (foundLower) {
// They're trying to create a normal case version of one that exists in lowercase.
// (We really should notify the user.)
//appStore.dispatch(appSlice.actions.addLog({log: true, msg: "Will create a lowercase version of the DID since a lowercase version exists."}))
address = address.toLowerCase();
}
//appStore.dispatch(appSlice.actions.addLog({log: false, msg: "... created new ID..."}))
accountStore.account = JSON.stringify(newId);
//appStore.dispatch(appSlice.actions.addLog({log: false, msg: "... stored new ID..."}))
}
//appStore.dispatch(appSlice.actions.addLog({log: false, msg: "... derived keys and address..."}))
//const newId = newIdentifier(address, publicHex, privateHex, UPORT_ROOT_DERIVATION_PATH)
//appStore.dispatch(appSlice.actions.addLog({log: false, msg: "... created new ID..."}))
// awaiting because otherwise the UI may not see that a mnemonic was created
//const savedId = await storeIdentifier(newId, mnemonic, mnemonicPassword)
//appStore.dispatch(appSlice.actions.addLog({log: false, msg: "... stored new ID..."}))
//return savedId;
}
}
</script>