Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix sub account key generation #281

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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