Skip to content

Commit

Permalink
Add support for non-standard derivations (deso-protocol#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
maebeam authored May 1, 2021
1 parent e18076b commit 625a4d2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
13 changes: 6 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"bip39": "^3.0.3",
"crypto-browserify": "^3.12.0",
"elliptic": "^6.5.4",
"hdkey": "^2.0.1",
"hdkey": "github:bitclout/hdkey",
"jsonwebtoken": "^8.5.1",
"key-encoder": "^2.0.3",
"ngx-bootstrap": "^6.2.0",
Expand Down
5 changes: 3 additions & 2 deletions src/app/crypto.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ export class CryptoService {
return this.seedHexToPrivateKey(seedHex);
}

mnemonicToKeychain(mnemonic: string, extraText?: string): HDNode {
mnemonicToKeychain(mnemonic: string, extraText?: string, nonStandard?: boolean): HDNode {
const seed = bip39.mnemonicToSeedSync(mnemonic, extraText);
return HDKey.fromMasterSeed(seed).derive('m/44\'/0\'/0\'/0/0');
// @ts-ignore
return HDKey.fromMasterSeed(seed).derive('m/44\'/0\'/0\'/0/0', nonStandard);
}

keychainToSeedHex(keychain: HDNode): string {
Expand Down
37 changes: 34 additions & 3 deletions src/app/log-in/log-in.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {EntropyService} from '../entropy.service';
import {GlobalVarsService} from '../global-vars.service';
import {BackendAPIService} from '../backend-api.service';
import {AccessLevel} from '../../types/identity';
import HDNode from 'hdkey';

@Component({
selector: 'app-log-in',
Expand Down Expand Up @@ -53,7 +54,6 @@ export class LogInComponent implements OnInit {
this.showLoadAccount = true;
} else {
this.showLoadAccount = false;
this.selectAccount(publicKeys[0]);
this.backendApi.GetUsersStateless(publicKeys).subscribe(res2 => {
for (const user of res2.UserList) {
this.allUsers[user.PublicKeyBase58Check] = {
Expand All @@ -62,6 +62,9 @@ export class LogInComponent implements OnInit {
};
}
});
if (!this.selectedAccount) {
this.selectAccount(publicKeys[0]);
}
}
}

Expand All @@ -85,19 +88,47 @@ export class LogInComponent implements OnInit {
return;
}

const network = this.globalVars.network;
const keychain = this.cryptoService.mnemonicToKeychain(this.mnemonic, this.extraText);
const keychainNonStandard = this.cryptoService.mnemonicToKeychain(this.mnemonic, this.extraText, true);

this.addKeychain(keychain);

// NOTE: Temporary support for 1 in 128 legacy users who have non-standard derivations
if (keychain.publicKey !== keychainNonStandard.publicKey) {
const network = this.globalVars.network;
const seedHex = this.cryptoService.keychainToSeedHex(keychainNonStandard);
const privateKey = this.cryptoService.seedHexToPrivateKey(seedHex);
const publicKey = this.cryptoService.privateKeyToBitcloutPublicKey(privateKey, network);

// We only want to add nonStandard derivations if the account is worth importing
this.backendApi.GetUsersStateless([publicKey]).subscribe(res => {
const user = res.UserList[0];
if (user.ProfileEntryResponse || user.BalanceNanos > 0 || user.UsersYouHODL?.length) {
// Add the non-standard key if the user has a profile, a balance, or holdings
this.addKeychain(keychainNonStandard);
}
});
}

// Clear the form
this.mnemonic = '';
this.extraText = '';
}

addKeychain(keychain: HDNode): void {
const network = this.globalVars.network;
const seedHex = this.cryptoService.keychainToSeedHex(keychain);
const btcDepositAddress = this.cryptoService.keychainToBtcAddress(keychain, network);

this.accountService.addUser({
const publicKeyAdded = this.accountService.addUser({
seedHex,
mnemonic: this.mnemonic,
extraText: this.extraText,
btcDepositAddress,
network,
});

this.selectAccount(publicKeyAdded);
this.loadUsers();
}

Expand Down

0 comments on commit 625a4d2

Please sign in to comment.