-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[BC/CWC] Wallet private key updates [1] #4068
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
Open
MichaelAJay
wants to merge
53
commits into
bitpay:master
Choose a base branch
from
MichaelAJay:wallet-privatekey-ref
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
53 commits
Select commit
Hold shift + click to select a range
0e5e6b4
add privateKeyToBuffer to IDeriver, implement method for concrete cla…
MichaelAJay b63ad0f
And encryptBuffer & decryptToBuffer pairs to Encryption class.
MichaelAJay 01215fa
adds Storage.addKeysSafe method & implements private-key encrypted wr…
MichaelAJay 4bf659c
fix Deriver.privateKeyToBuffer call
MichaelAJay 2bb39e5
implement privateKeyBuffertoNativePrivateKey on IDeriver concrete cla…
MichaelAJay 23ba0e3
add backwards-compatible attempt to decrypt key.privKey & serialize i…
MichaelAJay dfe9aca
fixed backwards compat issue and wrote tests to backwards compat
MichaelAJay bb01d24
bug fix
MichaelAJay 5b8d5c3
fix BTCDeriver private key buffer to native private key
MichaelAJay e1b901e
fix Wallet.unlock buffer to string conversion
MichaelAJay 2670f11
add another backwards compatibility check for bitcoin core wallet
MichaelAJay 1b61909
update IDeriver and implementation classes' privaetKeyToBuffer method…
MichaelAJay 57c4d0c
remove legacy wallet creation code
MichaelAJay 2afe18e
refactored loadWallet to auto-migrate wallets to current version
MichaelAJay 77c541d
add HDPrivateKey toObjectWithBufferPrivateKey
MichaelAJay 2f40d20
update encrypt/decrypt methods with flexibility and buffer-first appr…
MichaelAJay c05c5b9
remove backwards compatibility, add migration method
MichaelAJay 9750fa2
implement storage method replacements for add/get keys, and overload …
MichaelAJay 3a6794b
Merge branch 'master' of https://github.com/bitpay/bitcore into walle…
MichaelAJay b3c1078
remove extra decrypt in unlock for old versions
MichaelAJay 34735a8
fix password issue
MichaelAJay 1d861bb
fix privkey buff assignment in key treatment - signTx
MichaelAJay d6bb7cf
implementation complete, cleanup complete
MichaelAJay f5cefb8
Merge branch 'master' of https://github.com/bitpay/bitcore into walle…
MichaelAJay fe6e8d1
Add getPublicKey to DeriverProxy and to IDeriver
MichaelAJay 516c4a0
buffer-based privkey implementation of Deriver.getPublicKey
MichaelAJay 702eca7
fix deriver bug in eth and xrp getPublicKey
MichaelAJay 2169c91
add getPublicKey tests to derivers
MichaelAJay c476975
add check to lock() to handle not unlocked cases
MichaelAJay d9fa38b
Merge branch 'master' of https://github.com/bitpay/bitcore into walle…
MichaelAJay 2aa2e35
address feedback
MichaelAJay cf2b8ce
fix @deprecated comment style (to jsdocs)
MichaelAJay 0b5e9ae
fix naming issue
MichaelAJay f8930a6
simplify deprecated method implementation
MichaelAJay ab53de1
rename DeriverProxy method to match IDeriver implementation.
MichaelAJay 5a5bb0d
update method return type - temp buffer to private key
MichaelAJay aecdc18
fix getPublicKey - rm private key to string
MichaelAJay e7ff3c0
update encrypt/decrypt methods to handle unspecified error conditions…
MichaelAJay b2e30fa
add log statements for migration process and update file path for bac…
MichaelAJay bf9bc36
wallet test stubs
MichaelAJay 5e779ff
implement unlock & migrateWallet tests & add test fixture
MichaelAJay 6e7bc5d
add edge case tests for migrateWallet
MichaelAJay 0fd4944
refactor importKeys and write unit tests
MichaelAJay 05e4d33
refactor derivePrivateKey and test it
MichaelAJay 49bc4ae
add XRP and SOL signing tests
MichaelAJay 21bfe67
Merge branch 'master' of https://github.com/bitpay/bitcore into walle…
MichaelAJay 1a2b91b
remove duplicate import statement
MichaelAJay b5640eb
fix import statement
MichaelAJay a1ad584
fix import statements
MichaelAJay 7745ae4
fix getPublicKey methods to return compressed public keys
MichaelAJay b07f3a5
rm .only on test
MichaelAJay 4763757
fix lack of mkdir in wallet migration and update tests
MichaelAJay 0266e6a
refactor wallet tests to make them less brittle
MichaelAJay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -23,33 +23,83 @@ export function encryptEncryptionKey(encryptionKey, password) { | |
| return encData; | ||
| } | ||
|
|
||
| export function decryptEncryptionKey(encEncryptionKey, password) { | ||
| export function decryptEncryptionKey(encEncryptionKey, password, toBuffer: true): Buffer; | ||
| export function decryptEncryptionKey(encEncryptionKey, password, toBuffer: false): string; | ||
| export function decryptEncryptionKey(encEncryptionKey, password, toBuffer?: boolean): Buffer | string { | ||
| const password_hash = Buffer.from(SHA512(password)); | ||
| const key = password_hash.subarray(0, 32); | ||
| const iv = password_hash.subarray(32, 48); | ||
| const decipher = crypto.createDecipheriv(algo, key, iv); | ||
| const decrypted = decipher.update(encEncryptionKey, 'hex', 'hex' as any) + decipher.final('hex'); | ||
| return decrypted; | ||
|
|
||
| let payload: Buffer | undefined; | ||
| let final: Buffer | undefined; | ||
| let output: Buffer | undefined; | ||
| try { | ||
| payload = decipher.update(encEncryptionKey, 'hex'); | ||
| final = decipher.final(); | ||
| output = Buffer.concat([payload, final]); | ||
| return toBuffer ? output : output.toString('hex'); | ||
| } finally { | ||
| payload.fill(0); | ||
| final.fill(0); | ||
| if (!toBuffer) { | ||
| // Don't fill output if it's what's returned directly | ||
| output.fill(0); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** @deprecated - Use encryptBuffer */ | ||
| export function encryptPrivateKey(privKey, pubKey, encryptionKey) { | ||
MichaelAJay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const key = Buffer.from(encryptionKey, 'hex'); | ||
| const doubleHash = Buffer.from(SHA256(SHA256(pubKey)), 'hex'); | ||
| const iv = doubleHash.subarray(0, 16); | ||
| const cipher = crypto.createCipheriv(algo, key, iv); | ||
| const encData = cipher.update(privKey, 'utf8', 'hex') + cipher.final('hex'); | ||
| return encData; | ||
| encryptionKey = Buffer.from(encryptionKey, 'hex'); | ||
| privKey = Buffer.from(privKey, 'utf8'); | ||
| return encryptBuffer(privKey, pubKey, encryptionKey).toString('hex'); | ||
| } | ||
|
|
||
| function decryptPrivateKey(encPrivateKey: string, pubKey: string, encryptionKey: string) { | ||
| const key = Buffer.from(encryptionKey, 'hex'); | ||
| function decryptPrivateKey(encPrivateKey: string, pubKey: string, encryptionKey: Buffer | string) { | ||
| if (!Buffer.isBuffer(encryptionKey)) { | ||
| encryptionKey = Buffer.from(encryptionKey, 'hex'); | ||
| } | ||
| const doubleHash = Buffer.from(SHA256(SHA256(pubKey)), 'hex'); | ||
| const iv = doubleHash.subarray(0, 16); | ||
| const decipher = crypto.createDecipheriv(algo, key, iv); | ||
| const decipher = crypto.createDecipheriv(algo, encryptionKey, iv); | ||
| const decrypted = decipher.update(encPrivateKey, 'hex', 'utf8') + decipher.final('utf8'); | ||
| return decrypted; | ||
| } | ||
|
|
||
| function encryptBuffer(data: Buffer, pubKey: string, encryptionKey: Buffer): Buffer { | ||
| let payload: Buffer | undefined; | ||
| try { | ||
| const iv = Buffer.from(SHA256(SHA256(pubKey)), 'hex').subarray(0, 16); | ||
| const cipher = crypto.createCipheriv(algo, encryptionKey, iv); | ||
| payload = cipher.update(data); | ||
| return Buffer.concat([payload, cipher.final()]); | ||
| } finally { | ||
| if (Buffer.isBuffer(payload)) { | ||
| payload.fill(0); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function decryptToBuffer(encHex: string, pubKey: string, encryptionKey: Buffer): Buffer { | ||
| let decrypted: Buffer | undefined; | ||
| let final: Buffer | undefined; | ||
| try { | ||
| const iv = Buffer.from(SHA256(SHA256(pubKey)), 'hex').subarray(0, 16); | ||
| const decipher = crypto.createDecipheriv(algo, encryptionKey, iv); | ||
| decrypted = decipher.update(encHex, 'hex'); | ||
| final = decipher.final(); | ||
| return Buffer.concat([decrypted, final]); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include the |
||
| } finally { | ||
| if (Buffer.isBuffer(decrypted)) { | ||
| decrypted.fill(0); | ||
| } | ||
| if (Buffer.isBuffer(final)) { | ||
| final.fill(0); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function sha512KDF(passphrase: string, salt: Buffer, derivationOptions: { rounds?: number }): string { | ||
| const rounds = derivationOptions.rounds || 1; | ||
| // if salt was sent in as a string, we will have to assume the default encoding type | ||
|
|
@@ -134,6 +184,8 @@ export const Encryption = { | |
| decryptEncryptionKey, | ||
| encryptPrivateKey, | ||
| decryptPrivateKey, | ||
| encryptBuffer, | ||
| decryptToBuffer, | ||
| generateEncryptionKey, | ||
| bitcoinCoreDecrypt | ||
| }; | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
include
decipher.final()in the try.Not sure if defining
finaloutside the try is necessary. If this code is going to error, it's highly likely that it's due to an invalid checksum/tag in thedecipher.final()call, so it may be fine to just doBuffer.concat([payload, decipher.final()]);. Whatever is decided also applies to the other encrypt/decrypt buffer methods.