-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca2b85c
commit 5dcea37
Showing
10 changed files
with
455 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,80 @@ | ||
import { key, PrivateKey, PublicKey, Aes } from 'bitsharesjs'; | ||
import { PrivateKey, PublicKey, Aes } from 'bitsharesjs'; | ||
import { getAccountIdByOwnerPubkey } from './account'; | ||
import lib from '../../utils/lzma/lzma_worker-min.js'; | ||
|
||
const decryptWalletBackup = (wif, input) => { | ||
return new Promise((resolve, reject) => { | ||
let backupBuffer; | ||
if (!Buffer.isBuffer(input)) { | ||
backupBuffer = Buffer.from(input, 'binary'); | ||
} else { | ||
backupBuffer = input; | ||
} | ||
|
||
const restoreBackup = async ({ password, backup }) => { | ||
const privateKey = PrivateKey.fromSeed(password || ""); | ||
const result = await restore(privateKey.toWif(), backup, 'test'); | ||
return result; | ||
} | ||
|
||
const restore = async (backup_wif, backup, wallet_name) => { | ||
const privateKey = PrivateKey.fromWif(wif); | ||
let publicKey; | ||
try { | ||
const wallet = await decryptWalletBackup(backup_wif, backup); | ||
console.log('Wallet restore', wallet); | ||
if (!wallet.linked_accounts.length) { | ||
const pubkey = wallet.private_keys[0].pubkey; | ||
const id = await getAccountIdByOwnerPubkey(pubkey); | ||
wallet.linked_accounts[0] = { name: id[0] }; | ||
} | ||
return { success: true, wallet }; | ||
publicKey = PublicKey.fromBuffer(backupBuffer.slice(0, 33)); | ||
} catch (e) { | ||
return { success: false, error: e } | ||
console.error(e, e.stack); | ||
throw new Error('Invalid backup file'); | ||
} | ||
} | ||
|
||
const decryptWalletBackup = (backup_wif, backup_buffer) => { | ||
return new Promise((resolve, reject) => { | ||
if (!Buffer.isBuffer(backup_buffer)) | ||
backup_buffer = new Buffer(backup_buffer, "binary"); | ||
|
||
let private_key = PrivateKey.fromWif(backup_wif); | ||
let public_key; | ||
try { | ||
public_key = PublicKey.fromBuffer(backup_buffer.slice(0, 33)); | ||
} catch (e) { | ||
console.error(e, e.stack); | ||
throw new Error("Invalid backup file"); | ||
} | ||
|
||
const slicedBuffer = backupBuffer.slice(33); | ||
let decryptedBuffer; | ||
try { | ||
decryptedBuffer = Aes.decrypt_with_checksum( | ||
privateKey, | ||
publicKey, | ||
null /* nonce */, | ||
slicedBuffer | ||
); | ||
} catch (error) { | ||
reject(new Error('invalid_decryption_key')); | ||
return; | ||
} | ||
|
||
console.log('restoreBackup', backup_buffer, backup_wif) | ||
console.log('Decrypted', decryptedBuffer); | ||
|
||
backup_buffer = backup_buffer.slice(33); | ||
try { | ||
lib.LZMA_WORKER.decompress(decryptedBuffer, walletJson => { | ||
try { | ||
backup_buffer = Aes.decrypt_with_checksum( | ||
private_key, | ||
public_key, | ||
null /*nonce*/, | ||
backup_buffer | ||
); | ||
const wallet = JSON.parse(walletJson); | ||
console.log('Wallet obj', wallet); | ||
resolve(wallet); | ||
} catch (error) { | ||
reject("invalid_decryption_key"); | ||
return; | ||
console.error('Error parsing wallet json'); | ||
reject(new Error('Error parsing wallet json')); | ||
} | ||
}); | ||
} catch (error) { | ||
console.error('Error decompressing wallet', error, error.stack); | ||
reject(new Error('Error decompressing wallet')); | ||
} | ||
}); | ||
}; | ||
|
||
console.log('Decrypted', backup_buffer); | ||
const restore = async (wif, backup) => { | ||
try { | ||
const wallet = await decryptWalletBackup(wif, backup); | ||
if (!wallet.linked_accounts.length) { | ||
const { pubkey } = wallet.private_keys[0]; | ||
const id = await getAccountIdByOwnerPubkey(pubkey); | ||
wallet.linked_accounts[0] = { name: id[0] }; | ||
} | ||
return { success: true, wallet }; | ||
} catch (e) { | ||
return { success: false, error: e }; | ||
} | ||
}; | ||
|
||
const restoreBackup = async ({ password, backup }) => { | ||
const privateKey = PrivateKey.fromSeed(password || ''); | ||
const result = await restore(privateKey.toWif(), backup); | ||
return result; | ||
}; | ||
|
||
try { | ||
lib.LZMA_WORKER.decompress(backup_buffer, wallet_string => { | ||
try { | ||
let wallet_object = JSON.parse(wallet_string); | ||
console.log('Wallet obj', wallet_object) | ||
resolve(wallet_object); | ||
} catch (error) { | ||
if (!wallet_string) wallet_string = ""; | ||
console.error( | ||
"Error parsing wallet json", | ||
wallet_string.substring(0, 10) + "..." | ||
); | ||
reject("Error parsing wallet json"); | ||
} | ||
}); | ||
} catch (error) { | ||
console.error("Error decompressing wallet", error, error.stack); | ||
reject("Error decompressing wallet"); | ||
return; | ||
} | ||
}); | ||
} | ||
|
||
export default { | ||
restoreBackup | ||
} | ||
restoreBackup | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.