-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
web-wallet: Secure seed with ephemeral encryption
Resolves #3354
- Loading branch information
1 parent
e3bd946
commit 2ff97cd
Showing
18 changed files
with
181 additions
and
114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { generateMnemonic } from "bip39"; | ||
|
||
import { decryptBuffer, encryptBuffer } from ".."; | ||
|
||
describe("decryptBuffer", () => { | ||
const plaintext = new TextEncoder().encode(generateMnemonic()); | ||
const pwd = "some password"; | ||
|
||
it("should be able to decrypt the mnemonic phrase using the given password", async () => { | ||
const encryptInfo = await encryptBuffer(plaintext, pwd); | ||
const decrypted = await decryptBuffer(encryptInfo, pwd); | ||
|
||
expect(decrypted.toString()).toBe(plaintext.buffer.toString()); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { generateMnemonic } from "bip39"; | ||
|
||
import { encryptBuffer, getSeedFromMnemonic } from ".."; | ||
|
||
describe("encryptBuffer", () => { | ||
it("should be able to encrypt a buffer using the given password", async () => { | ||
const pwd = "some password"; | ||
const buffer = getSeedFromMnemonic(generateMnemonic()); | ||
const result = await encryptBuffer(buffer, pwd); | ||
|
||
expect(result).toMatchObject({ | ||
data: expect.any(Uint8Array), | ||
iv: expect.any(Uint8Array), | ||
salt: expect.any(Uint8Array), | ||
}); | ||
expect(result.data.toString()).not.toBe(buffer.toString()); | ||
expect(result.iv.length).toBe(12); | ||
expect(result.salt.length).toBe(32); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import getDerivedKey from "./getDerivedKey"; | ||
|
||
/** | ||
* @param {WalletEncryptInfo} encryptInfo | ||
* @param {string} pwd | ||
* @returns {Promise<ArrayBuffer>} | ||
*/ | ||
async function decryptBuffer(encryptInfo, pwd) { | ||
const { data, iv, salt } = encryptInfo; | ||
const key = await getDerivedKey(pwd, salt); | ||
|
||
return await crypto.subtle.decrypt({ iv, name: "AES-GCM" }, key, data); | ||
} | ||
|
||
export default decryptBuffer; |
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,20 +1,11 @@ | ||
import getDerivedKey from "./getDerivedKey"; | ||
import decryptBuffer from "./decryptBuffer"; | ||
|
||
/** | ||
* @param {MnemonicEncryptInfo} mnemonicEncryptInfo | ||
* @param {String} pwd | ||
* @returns {Promise<String>} | ||
* @param {WalletEncryptInfo} encryptInfo | ||
* @param {string} pwd | ||
* @returns {Promise<string>} | ||
*/ | ||
async function decryptMnemonic(mnemonicEncryptInfo, pwd) { | ||
const { data, iv, salt } = mnemonicEncryptInfo; | ||
const key = await getDerivedKey(pwd, salt); | ||
const plaintext = await crypto.subtle.decrypt( | ||
{ iv, name: "AES-GCM" }, | ||
key, | ||
data | ||
); | ||
|
||
return new TextDecoder().decode(plaintext); | ||
} | ||
const decryptMnemonic = async (encryptInfo, pwd) => | ||
new TextDecoder().decode(await decryptBuffer(encryptInfo, pwd)); | ||
|
||
export default decryptMnemonic; |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import getDerivedKey from "./getDerivedKey"; | ||
|
||
/** | ||
* @param {BufferSource} buffer | ||
* @param {string} pwd | ||
* @returns {Promise<WalletEncryptInfo>} | ||
*/ | ||
async function encryptBuffer(buffer, pwd) { | ||
const salt = crypto.getRandomValues(new Uint8Array(32)); | ||
const iv = crypto.getRandomValues(new Uint8Array(12)); | ||
const key = await getDerivedKey(pwd, salt); | ||
const data = new Uint8Array( | ||
await crypto.subtle.encrypt({ iv, name: "AES-GCM" }, key, buffer) | ||
); | ||
|
||
return { data, iv, salt }; | ||
} | ||
|
||
export default encryptBuffer; |
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,20 +1,11 @@ | ||
import getDerivedKey from "./getDerivedKey"; | ||
import encryptBuffer from "./encryptBuffer"; | ||
|
||
/** | ||
* @param {String} mnemonic | ||
* @param {String} pwd | ||
* @returns {Promise<MnemonicEncryptInfo>} | ||
* @param {string} mnemonic | ||
* @param {string} pwd | ||
* @returns {Promise<WalletEncryptInfo>} | ||
*/ | ||
async function encryptMnemonic(mnemonic, pwd) { | ||
const plaintext = new TextEncoder().encode(mnemonic); | ||
const salt = crypto.getRandomValues(new Uint8Array(32)); | ||
const iv = crypto.getRandomValues(new Uint8Array(12)); | ||
const key = await getDerivedKey(pwd, salt); | ||
const data = new Uint8Array( | ||
await crypto.subtle.encrypt({ iv, name: "AES-GCM" }, key, plaintext) | ||
); | ||
|
||
return { data, iv, salt }; | ||
} | ||
const encryptMnemonic = async (mnemonic, pwd) => | ||
await encryptBuffer(new TextEncoder().encode(mnemonic), pwd); | ||
|
||
export default encryptMnemonic; |
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,4 +1,4 @@ | ||
type MnemonicEncryptInfo = { | ||
type WalletEncryptInfo = { | ||
data: Uint8Array; | ||
iv: Uint8Array; | ||
salt: Uint8Array; | ||
|
Oops, something went wrong.