@mysten/enoki
Advanced tools
Comparing version 0.0.0-experimental-20231121172842 to 0.0.0-experimental-20231121174042
# @mysten/enoki | ||
## 0.0.0-experimental-20231121172842 | ||
## 0.0.0-experimental-20231121174042 | ||
### Patch Changes | ||
- 180616bef: Rewrite the encryption layer | ||
- 9ac7e2f3d: Introduce Enoki SDK | ||
@@ -12,3 +13,3 @@ - Updated dependencies [194c980cb] | ||
- Updated dependencies [64d45ba27] | ||
- @mysten/sui.js@0.0.0-experimental-20231121172842 | ||
- @mysten/zklogin@0.0.0-experimental-20231121172842 | ||
- @mysten/sui.js@0.0.0-experimental-20231121174042 | ||
- @mysten/zklogin@0.0.0-experimental-20231121174042 |
@@ -25,11 +25,60 @@ "use strict"; | ||
module.exports = __toCommonJS(encryption_exports); | ||
var import_browser_passworder = require("@metamask/browser-passworder"); | ||
var import_utils = require("@mysten/sui.js/utils"); | ||
function createDefaultEncryption() { | ||
async function keyFromPassword(password, salt) { | ||
const key = await crypto.subtle.importKey( | ||
"raw", | ||
new TextEncoder().encode(password), | ||
{ name: "PBKDF2" }, | ||
false, | ||
["deriveBits", "deriveKey"] | ||
); | ||
const derivedKey = await crypto.subtle.deriveKey( | ||
{ | ||
name: "PBKDF2", | ||
salt, | ||
iterations: 9e5, | ||
hash: "SHA-256" | ||
}, | ||
key, | ||
{ name: "AES-GCM", length: 256 }, | ||
false, | ||
["encrypt", "decrypt"] | ||
); | ||
return { key, derivedKey }; | ||
} | ||
return { | ||
async encrypt(password, data) { | ||
return (0, import_browser_passworder.encrypt)(password, data); | ||
const salt = crypto.getRandomValues(new Uint8Array(16)); | ||
const iv = crypto.getRandomValues(new Uint8Array(12)); | ||
const { derivedKey } = await keyFromPassword(password, salt); | ||
const payload = await crypto.subtle.encrypt( | ||
{ | ||
name: "AES-GCM", | ||
iv | ||
}, | ||
derivedKey, | ||
new TextEncoder().encode(data) | ||
); | ||
return JSON.stringify({ | ||
payload: (0, import_utils.toB64)(new Uint8Array(payload)), | ||
iv: (0, import_utils.toB64)(iv), | ||
salt: (0, import_utils.toB64)(salt) | ||
}); | ||
}, | ||
async decrypt(password, data) { | ||
const decrypted = await (0, import_browser_passworder.decrypt)(password, data); | ||
return decrypted; | ||
const parsed = JSON.parse(data); | ||
if (!parsed.payload || !parsed.iv || !parsed.salt) { | ||
throw new Error("Invalid encrypted data"); | ||
} | ||
const { derivedKey } = await keyFromPassword(password, (0, import_utils.fromB64)(parsed.salt)); | ||
const decryptedContent = await crypto.subtle.decrypt( | ||
{ | ||
name: "AES-GCM", | ||
iv: (0, import_utils.fromB64)(parsed.iv) | ||
}, | ||
derivedKey, | ||
(0, import_utils.fromB64)(parsed.payload) | ||
); | ||
return new TextDecoder().decode(decryptedContent); | ||
} | ||
@@ -36,0 +85,0 @@ }; |
@@ -1,13 +0,59 @@ | ||
import { | ||
decrypt as metamaskDecrypt, | ||
encrypt as metamaskEncrypt | ||
} from "@metamask/browser-passworder"; | ||
import { fromB64, toB64 } from "@mysten/sui.js/utils"; | ||
function createDefaultEncryption() { | ||
async function keyFromPassword(password, salt) { | ||
const key = await crypto.subtle.importKey( | ||
"raw", | ||
new TextEncoder().encode(password), | ||
{ name: "PBKDF2" }, | ||
false, | ||
["deriveBits", "deriveKey"] | ||
); | ||
const derivedKey = await crypto.subtle.deriveKey( | ||
{ | ||
name: "PBKDF2", | ||
salt, | ||
iterations: 9e5, | ||
hash: "SHA-256" | ||
}, | ||
key, | ||
{ name: "AES-GCM", length: 256 }, | ||
false, | ||
["encrypt", "decrypt"] | ||
); | ||
return { key, derivedKey }; | ||
} | ||
return { | ||
async encrypt(password, data) { | ||
return metamaskEncrypt(password, data); | ||
const salt = crypto.getRandomValues(new Uint8Array(16)); | ||
const iv = crypto.getRandomValues(new Uint8Array(12)); | ||
const { derivedKey } = await keyFromPassword(password, salt); | ||
const payload = await crypto.subtle.encrypt( | ||
{ | ||
name: "AES-GCM", | ||
iv | ||
}, | ||
derivedKey, | ||
new TextEncoder().encode(data) | ||
); | ||
return JSON.stringify({ | ||
payload: toB64(new Uint8Array(payload)), | ||
iv: toB64(iv), | ||
salt: toB64(salt) | ||
}); | ||
}, | ||
async decrypt(password, data) { | ||
const decrypted = await metamaskDecrypt(password, data); | ||
return decrypted; | ||
const parsed = JSON.parse(data); | ||
if (!parsed.payload || !parsed.iv || !parsed.salt) { | ||
throw new Error("Invalid encrypted data"); | ||
} | ||
const { derivedKey } = await keyFromPassword(password, fromB64(parsed.salt)); | ||
const decryptedContent = await crypto.subtle.decrypt( | ||
{ | ||
name: "AES-GCM", | ||
iv: fromB64(parsed.iv) | ||
}, | ||
derivedKey, | ||
fromB64(parsed.payload) | ||
); | ||
return new TextDecoder().decode(decryptedContent); | ||
} | ||
@@ -14,0 +60,0 @@ }; |
{ | ||
"name": "@mysten/enoki", | ||
"version": "0.0.0-experimental-20231121172842", | ||
"version": "0.0.0-experimental-20231121174042", | ||
"description": "TODO: Description", | ||
@@ -44,8 +44,7 @@ "license": "Apache-2.0", | ||
"dependencies": { | ||
"@metamask/browser-passworder": "^4.1.0", | ||
"@nanostores/react": "^0.7.1", | ||
"jose": "^4.14.4", | ||
"nanostores": "^0.9.3", | ||
"@mysten/sui.js": "0.0.0-experimental-20231121172842", | ||
"@mysten/zklogin": "0.0.0-experimental-20231121172842" | ||
"@mysten/sui.js": "0.0.0-experimental-20231121174042", | ||
"@mysten/zklogin": "0.0.0-experimental-20231121174042" | ||
}, | ||
@@ -52,0 +51,0 @@ "peerDependencies": { |
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { | ||
decrypt as metamaskDecrypt, | ||
encrypt as metamaskEncrypt, | ||
} from '@metamask/browser-passworder'; | ||
import { fromB64, toB64 } from '@mysten/sui.js/utils'; | ||
@@ -21,9 +18,73 @@ /** | ||
export function createDefaultEncryption(): Encryption { | ||
type EncryptedJSON = { | ||
payload: string; | ||
iv: string; | ||
salt: string; | ||
}; | ||
async function keyFromPassword(password: string, salt: Uint8Array) { | ||
const key = await crypto.subtle.importKey( | ||
'raw', | ||
new TextEncoder().encode(password), | ||
{ name: 'PBKDF2' }, | ||
false, | ||
['deriveBits', 'deriveKey'], | ||
); | ||
const derivedKey = await crypto.subtle.deriveKey( | ||
{ | ||
name: 'PBKDF2', | ||
salt, | ||
iterations: 900_000, | ||
hash: 'SHA-256', | ||
}, | ||
key, | ||
{ name: 'AES-GCM', length: 256 }, | ||
false, | ||
['encrypt', 'decrypt'], | ||
); | ||
return { key, derivedKey }; | ||
} | ||
return { | ||
async encrypt(password, data) { | ||
return metamaskEncrypt(password, data); | ||
const salt = crypto.getRandomValues(new Uint8Array(16)); | ||
const iv = crypto.getRandomValues(new Uint8Array(12)); | ||
const { derivedKey } = await keyFromPassword(password, salt); | ||
const payload = await crypto.subtle.encrypt( | ||
{ | ||
name: 'AES-GCM', | ||
iv, | ||
}, | ||
derivedKey, | ||
new TextEncoder().encode(data), | ||
); | ||
return JSON.stringify({ | ||
payload: toB64(new Uint8Array(payload)), | ||
iv: toB64(iv), | ||
salt: toB64(salt), | ||
} satisfies EncryptedJSON); | ||
}, | ||
async decrypt(password, data) { | ||
const decrypted = await metamaskDecrypt(password, data); | ||
return decrypted as string; | ||
const parsed = JSON.parse(data) as EncryptedJSON; | ||
if (!parsed.payload || !parsed.iv || !parsed.salt) { | ||
throw new Error('Invalid encrypted data'); | ||
} | ||
const { derivedKey } = await keyFromPassword(password, fromB64(parsed.salt)); | ||
const decryptedContent = await crypto.subtle.decrypt( | ||
{ | ||
name: 'AES-GCM', | ||
iv: fromB64(parsed.iv), | ||
}, | ||
derivedKey, | ||
fromB64(parsed.payload), | ||
); | ||
return new TextDecoder().decode(decryptedContent); | ||
}, | ||
@@ -30,0 +91,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
240976
7
2361
+ Added@mysten/sui.js@0.0.0-experimental-20231121174042(transitive)
+ Added@mysten/zklogin@0.0.0-experimental-20231121174042(transitive)
+ Added@noble/hashes@1.6.1(transitive)
- Removed@metamask/browser-passworder@^4.1.0
- Removed@ethereumjs/common@3.2.0(transitive)
- Removed@ethereumjs/rlp@4.0.1(transitive)
- Removed@ethereumjs/tx@4.2.0(transitive)
- Removed@ethereumjs/util@8.1.0(transitive)
- Removed@metamask/browser-passworder@4.3.0(transitive)
- Removed@metamask/superstruct@3.1.0(transitive)
- Removed@metamask/utils@8.5.0(transitive)
- Removed@mysten/sui.js@0.0.0-experimental-20231121172842(transitive)
- Removed@mysten/zklogin@0.0.0-experimental-20231121172842(transitive)
- Removed@noble/curves@1.4.2(transitive)
- Removed@noble/hashes@1.4.0(transitive)
- Removed@scure/base@1.1.9(transitive)
- Removed@scure/bip32@1.4.0(transitive)
- Removed@scure/bip39@1.3.0(transitive)
- Removed@types/debug@4.1.12(transitive)
- Removed@types/ms@0.7.34(transitive)
- Removedcrc-32@1.2.2(transitive)
- Removeddebug@4.3.7(transitive)
- Removedethereum-cryptography@2.2.1(transitive)
- Removedmicro-ftch@0.3.1(transitive)
- Removedms@2.1.3(transitive)
- Removedpony-cause@2.1.11(transitive)
- Removedsemver@7.6.3(transitive)
- Removeduuid@9.0.1(transitive)