Skip to content

Commit

Permalink
fix sub account key generation (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackson-dean authored Sep 29, 2023
1 parent c252107 commit 00763db
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 30 deletions.
19 changes: 12 additions & 7 deletions src/app/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,12 @@ export class AccountService {
);

if (foundAccount) {
const keychain = this.cryptoService.getSubAccountKeychain(
rootUser.seedHex,
foundAccount.accountNumber
const keychain = this.cryptoService.mnemonicToKeychain(
rootUser.mnemonic,
{
extraText: rootUser.extraText,
accountNumber: foundAccount.accountNumber,
}
);
const subAccountSeedHex =
this.cryptoService.keychainToSeedHex(keychain);
Expand Down Expand Up @@ -1322,10 +1325,12 @@ export class AccountService {
);
}

const parentSeedHex = parentAccount.seedHex;
const childKey = this.cryptoService.getSubAccountKeychain(
parentSeedHex,
accountNumber
const childKey = this.cryptoService.mnemonicToKeychain(
parentAccount.mnemonic,
{
accountNumber,
extraText: parentAccount.extraText,
}
);
const ec = new EC('secp256k1');
const keyPair = ec.keyFromPrivate(childKey.privateKey);
Expand Down
14 changes: 6 additions & 8 deletions src/app/auth/google/google.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@ export class GoogleComponent implements OnInit {
const mnemonic = fileContents.mnemonic;
const extraText = fileContents.extraText;
const network = fileContents.network;
const keychain = this.cryptoService.mnemonicToKeychain(
mnemonic,
extraText
);
const keychain = this.cryptoService.mnemonicToKeychain(mnemonic, {
extraText,
});

this.publicKey = this.accountService.addUser(
keychain,
Expand Down Expand Up @@ -139,10 +138,9 @@ export class GoogleComponent implements OnInit {
this.googleDrive
.uploadFile(this.fileName(), JSON.stringify(userInfo))
.subscribe(() => {
const keychain = this.cryptoService.mnemonicToKeychain(
mnemonic,
extraText
);
const keychain = this.cryptoService.mnemonicToKeychain(mnemonic, {
extraText,
});
this.publicKey = this.accountService.addUser(
keychain,
mnemonic,
Expand Down
18 changes: 10 additions & 8 deletions src/app/crypto.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,22 @@ export class CryptoService {

mnemonicToKeychain(
mnemonic: string,
extraText?: string,
nonStandard?: boolean
{
extraText,
nonStandard,
accountNumber = 0,
}: {
extraText?: string;
nonStandard?: boolean;
accountNumber?: number;
} = {}
): HDNode {
const seed = bip39.mnemonicToSeedSync(mnemonic, extraText);
return generateSubAccountKeys(seed, 0, {
return generateSubAccountKeys(seed, accountNumber, {
nonStandard,
});
}

getSubAccountKeychain(masterSeedHex: string, accountIndex: number): HDNode {
const seedBytes = Buffer.from(masterSeedHex, 'hex');
return generateSubAccountKeys(seedBytes, accountIndex);
}

keychainToSeedHex(keychain: HDNode): string {
return keychain.privateKey.toString('hex');
}
Expand Down
13 changes: 7 additions & 6 deletions src/app/log-in-seed/log-in-seed.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ export class LogInSeedComponent implements OnInit {
return;
}

const keychain = this.cryptoService.mnemonicToKeychain(
mnemonic,
extraText
);
const keychain = this.cryptoService.mnemonicToKeychain(mnemonic, {
extraText,
});
const keychainNonStandard = this.cryptoService.mnemonicToKeychain(
mnemonic,
extraText,
true
{
extraText,
nonStandard: true,
}
);
userPublicKey = this.accountService.addUser(
keychain,
Expand Down
4 changes: 3 additions & 1 deletion src/app/sign-up/sign-up.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ export class SignUpComponent implements OnInit, OnDestroy {
const network = this.globalVars.network;
const mnemonic = this.mnemonicCheck;
const extraText = this.extraTextCheck;
const keychain = this.cryptoService.mnemonicToKeychain(mnemonic, extraText);
const keychain = this.cryptoService.mnemonicToKeychain(mnemonic, {
extraText,
});
this.seedHex = this.cryptoService.keychainToSeedHex(keychain);
this.publicKeyAdded = this.accountService.addUser(
keychain,
Expand Down

0 comments on commit 00763db

Please sign in to comment.