Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
14 changes: 14 additions & 0 deletions scripts/composables/use_wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
vaults as rawVaults,
Wallet,
} from '../wallet.js';
import { ref, computed, watch, reactive } from 'vue';

Check warning on line 9 in scripts/composables/use_wallet.js

View workflow job for this annotation

GitHub Actions / Run linters

'watch' is defined but never used
import { fPublicMode, strCurrency } from '../settings.js';
import { cOracle } from '../prices.js';
import { LedgerController } from '../ledger.js';
Expand Down Expand Up @@ -321,6 +321,20 @@
isSeeded.value = v.isSeeded();
if (encryptedSecret) canGenerateMore.value = true;
},
async getSecretToBackup() {
const database = await Database.getInstance();
const { encryptedSecret } = (await database.getVault(
v.getDefaultKeyToExport()
)) ?? { encryptedSecret: null };
if (encryptedSecret) return encryptedSecret;
// vault was not encrypted, return raw seed
const secret = v.getSecretToExport();
if (typeof secret === 'string') {
return secret;
} else {
return buff_to_base64(secret);
}
},
async encrypt(password) {
const secretToExport = v.getSecretToExport();
if (!secretToExport)
Expand Down
3 changes: 1 addition & 2 deletions scripts/dashboard/Dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const restoreWalletReason = ref('');
const importLock = ref(false);
watch(showExportModal, async (showExportModal) => {
if (showExportModal) {
keyToBackup.value = await activeWallet.value.getKeyToBackup();
keyToBackup.value = await activeVault.value.getSecretToBackup();
} else {
// Wipe key to backup, just in case
keyToBackup.value = '';
Expand Down Expand Up @@ -1042,7 +1042,6 @@ defineExpose({
<ExportPrivKey
:show="showExportModal"
:privateKey="keyToBackup"
:isJSON="hasShield && !activeVault?.isEncrypted"
@close="showExportModal = false"
/>
<!-- WALLET FEATURES -->
Expand Down
14 changes: 9 additions & 5 deletions scripts/dashboard/ExportPrivKey.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@ import { downloadBlob } from '../misc';
import pIconExport from '../../assets/icons/icon-export.svg';
const props = defineProps({
privateKey: String,
// Note: isJSON should probably be temporary, maybe we have a "Wallet Type" enum that determines the export UI?
isJSON: Boolean,
show: Boolean,
});
const blur = ref(true);

const emit = defineEmits(['close']);

function downloadWalletFile() {
let isJSON = false;
try {
JSON.parse(props.privateKey);
isJSON = true;
} catch {
isJSON = false;
}
downloadBlob(
props.privateKey,
'wallet.json',
'application/json;charset=utf-8;'
isJSON ? 'wallet.json' : 'wallet.txt',
`${isJSON ? application / json : 'text/plain'};charset=utf-8;`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo that would lead to a MIME type of NaN;charset=utf-8;

Suggested change
`${isJSON ? application / json : 'text/plain'};charset=utf-8;`
`${isJSON ? 'application/json' : 'text/plain'};charset=utf-8;`

);
}

Expand Down Expand Up @@ -69,7 +74,6 @@ function close() {
</span>
</button>
<button
v-if="isJSON"
class="pivx-button-big"
@click="downloadWalletFile()"
>
Expand Down
50 changes: 31 additions & 19 deletions scripts/parsed_secret.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { cChainParams } from './chain_params.js';
import { LegacyMasterKey, HdMasterKey } from './masterkey.js';
import { PIVXShield } from 'pivx-shield';
import { mnemonicToSeed } from 'bip39';
import { decrypt } from './aes-gcm.js';
import { base64_to_buf, decrypt } from './aes-gcm.js';
import { isBase64 } from './misc.js';

export class ParsedSecret {
Expand All @@ -23,6 +23,29 @@ export class ParsedSecret {
this.seed = seed;
}

/**
* @param {Uint8Array} seed
*/
static async createParsedSecretBySeed(seed) {
const pivxShield = await PIVXShield.create({
seed,
// hardcoded value considering the last checkpoint, this is good both for mainnet and testnet
// TODO: take the wallet creation height in input from users
blockHeight: cChainParams.current.defaultStartingShieldBlock,
coinType: cChainParams.current.BIP44_TYPE,
// TODO: Change account index once account system is made
accountIndex: 0,
loadSaplingData: false,
});
return new ParsedSecret(
new HdMasterKey({
seed,
}),
pivxShield,
seed
);
}

/**
* Parses whatever the secret is to a MasterKey
* @param {string|number[]|Uint8Array} secret
Expand All @@ -38,6 +61,12 @@ export class ParsedSecret {
test: (s) => isBase64(s) && s.length >= 128,
f: async (s, p) => ParsedSecret.parse(await decrypt(s, p)),
},
{
// If it's base64, and the length is 88 (64 decoded), it's likely a seed
test: (s) => isBase64(s) && s.length === 88,
f: (s) =>
ParsedSecret.createParsedSecretBySeed(base64_to_buf(s)),
},
{
test: (s) => s.startsWith('xprv'),
f: (s) => new ParsedSecret(new HdMasterKey({ xpriv: s })),
Expand Down Expand Up @@ -65,24 +94,7 @@ export class ParsedSecret {
);
if (!ok) throw new Error(msg);
const seed = await mnemonicToSeed(phrase, password);
const pivxShield = await PIVXShield.create({
seed,
// hardcoded value considering the last checkpoint, this is good both for mainnet and testnet
// TODO: take the wallet creation height in input from users
blockHeight:
cChainParams.current.defaultStartingShieldBlock,
coinType: cChainParams.current.BIP44_TYPE,
// TODO: Change account index once account system is made
accountIndex: 0,
loadSaplingData: false,
});
return new ParsedSecret(
new HdMasterKey({
seed,
}),
pivxShield,
seed
);
return await ParsedSecret.createParsedSecretBySeed(seed);
},
},
{
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/parsed_secret.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,18 @@
{
"secret": ["invalidsecret", "{\"invalid\":\"secret\"}"],
"expected": {}
},
{
"secret": "L3+fT7N2LvkyTjvC1s61jgFKPWqWTL0ssTCbGqF2zrIe/iTVwmwk7GZsTIL3zf4ot5xHQqmLB+RKySdzHNjO2g==",
"expected": {
"seed": "2f7f9f4fb3762ef9324e3bc2d6ceb58e014a3d6a964cbd2cb1309b1aa176ceb21efe24d5c26c24ec666c4c82f7cdfe28b79c4742a98b07e44ac927731cd8ceda"
}
},
{
"secret": "UkyLUezEXEuZfGl2uTxijyx9tczTXC8iNm1eKO8kJQBeyLsChmRAlO/fNxyCKTmoMlJRZ1VIM8Oyd3EYG1tx1i2JYOwQHV2ekuJ/aUwZivw/AeQi6FKbK6Kbiow2eXl3W/oVO/v53KyJ3jPqiqgsPyULEV65ab122j6oXyoI9pPTB9RY",
"password": "123456",
"expected": {
"seed": "2f7f9f4fb3762ef9324e3bc2d6ceb58e014a3d6a964cbd2cb1309b1aa176ceb21efe24d5c26c24ec666c4c82f7cdfe28b79c4742a98b07e44ac927731cd8ceda"
}
}
]
Loading