@stacks/encryption
Advanced tools
Comparing version 6.14.1-pr.25 to 6.14.1-pr.26
import { GetRandomBytes } from './cryptoRandom'; | ||
import { TriplesecDecryptSignature } from './cryptoUtils'; | ||
export declare function encryptMnemonic(phrase: string, password: string, opts?: { | ||
getRandomBytes?: GetRandomBytes; | ||
}): Promise<Uint8Array>; | ||
export declare function decryptMnemonic(data: string | Uint8Array, password: string, triplesecDecrypt?: TriplesecDecryptSignature): Promise<string>; | ||
export declare function decryptMnemonic(data: string | Uint8Array, password: string): Promise<string>; |
@@ -1,9 +0,9 @@ | ||
import { validateMnemonic, mnemonicToEntropy, entropyToMnemonic } from '@scure/bip39'; | ||
import { entropyToMnemonic, mnemonicToEntropy, validateMnemonic } from '@scure/bip39'; | ||
import { wordlist } from '@scure/bip39/wordlists/english'; | ||
import { bytesToHex, concatBytes, equals, hexToBytes } from '@stacks/common'; | ||
import { createCipher } from './aesCipher'; | ||
import { randomBytes } from './cryptoRandom'; | ||
import { hmacSha256 } from './ec'; | ||
import { createPbkdf2 } from './pbkdf2'; | ||
import { createSha2Hash } from './sha2Hash'; | ||
import { createCipher } from './aesCipher'; | ||
import { createPbkdf2 } from './pbkdf2'; | ||
import { bytesToHex, bytesToUtf8, concatBytes, equals, hexToBytes, utf8ToBytes, } from '@stacks/common'; | ||
import { hmacSha256 } from './ec'; | ||
export async function encryptMnemonic(phrase, password, opts) { | ||
@@ -31,4 +31,3 @@ let mnemonicEntropy; | ||
const hmacDigest = hmacSha256(macKey, hmacPayload); | ||
const payload = concatBytes(salt, hmacDigest, cipherText); | ||
return payload; | ||
return concatBytes(salt, hmacDigest, cipherText); | ||
} | ||
@@ -70,32 +69,6 @@ class PasswordError extends Error { | ||
} | ||
function decryptLegacy(dataBytes, password, triplesecDecrypt) { | ||
return new Promise((resolve, reject) => { | ||
if (!triplesecDecrypt) { | ||
reject(new Error('The `triplesec.decrypt` function must be provided')); | ||
} | ||
triplesecDecrypt({ | ||
key: utf8ToBytes(password), | ||
data: dataBytes, | ||
}, (err, plaintextBytes) => { | ||
if (!err) { | ||
resolve(plaintextBytes); | ||
} | ||
else { | ||
reject(err); | ||
} | ||
}); | ||
}); | ||
} | ||
export async function decryptMnemonic(data, password, triplesecDecrypt) { | ||
export async function decryptMnemonic(data, password) { | ||
const dataBytes = typeof data === 'string' ? hexToBytes(data) : data; | ||
try { | ||
return await decryptMnemonicBytes(dataBytes, password); | ||
} | ||
catch (error) { | ||
if (error instanceof PasswordError) | ||
throw error; | ||
const data = await decryptLegacy(dataBytes, password, triplesecDecrypt); | ||
return bytesToUtf8(data); | ||
} | ||
return await decryptMnemonicBytes(dataBytes, password); | ||
} | ||
//# sourceMappingURL=wallet.js.map |
import { GetRandomBytes } from './cryptoRandom'; | ||
import { TriplesecDecryptSignature } from './cryptoUtils'; | ||
export declare function encryptMnemonic(phrase: string, password: string, opts?: { | ||
getRandomBytes?: GetRandomBytes; | ||
}): Promise<Uint8Array>; | ||
export declare function decryptMnemonic(data: string | Uint8Array, password: string, triplesecDecrypt?: TriplesecDecryptSignature): Promise<string>; | ||
export declare function decryptMnemonic(data: string | Uint8Array, password: string): Promise<string>; |
@@ -6,8 +6,8 @@ "use strict"; | ||
const english_1 = require("@scure/bip39/wordlists/english"); | ||
const common_1 = require("@stacks/common"); | ||
const aesCipher_1 = require("./aesCipher"); | ||
const cryptoRandom_1 = require("./cryptoRandom"); | ||
const ec_1 = require("./ec"); | ||
const pbkdf2_1 = require("./pbkdf2"); | ||
const sha2Hash_1 = require("./sha2Hash"); | ||
const aesCipher_1 = require("./aesCipher"); | ||
const pbkdf2_1 = require("./pbkdf2"); | ||
const common_1 = require("@stacks/common"); | ||
const ec_1 = require("./ec"); | ||
async function encryptMnemonic(phrase, password, opts) { | ||
@@ -35,4 +35,3 @@ let mnemonicEntropy; | ||
const hmacDigest = (0, ec_1.hmacSha256)(macKey, hmacPayload); | ||
const payload = (0, common_1.concatBytes)(salt, hmacDigest, cipherText); | ||
return payload; | ||
return (0, common_1.concatBytes)(salt, hmacDigest, cipherText); | ||
} | ||
@@ -75,33 +74,7 @@ exports.encryptMnemonic = encryptMnemonic; | ||
} | ||
function decryptLegacy(dataBytes, password, triplesecDecrypt) { | ||
return new Promise((resolve, reject) => { | ||
if (!triplesecDecrypt) { | ||
reject(new Error('The `triplesec.decrypt` function must be provided')); | ||
} | ||
triplesecDecrypt({ | ||
key: (0, common_1.utf8ToBytes)(password), | ||
data: dataBytes, | ||
}, (err, plaintextBytes) => { | ||
if (!err) { | ||
resolve(plaintextBytes); | ||
} | ||
else { | ||
reject(err); | ||
} | ||
}); | ||
}); | ||
} | ||
async function decryptMnemonic(data, password, triplesecDecrypt) { | ||
async function decryptMnemonic(data, password) { | ||
const dataBytes = typeof data === 'string' ? (0, common_1.hexToBytes)(data) : data; | ||
try { | ||
return await decryptMnemonicBytes(dataBytes, password); | ||
} | ||
catch (error) { | ||
if (error instanceof PasswordError) | ||
throw error; | ||
const data = await decryptLegacy(dataBytes, password, triplesecDecrypt); | ||
return (0, common_1.bytesToUtf8)(data); | ||
} | ||
return await decryptMnemonicBytes(dataBytes, password); | ||
} | ||
exports.decryptMnemonic = decryptMnemonic; | ||
//# sourceMappingURL=wallet.js.map |
{ | ||
"name": "@stacks/encryption", | ||
"version": "6.14.1-pr.25+f66fbb0d", | ||
"version": "6.14.1-pr.26+4721d1ac", | ||
"description": "Encryption utilities for Stacks", | ||
@@ -26,3 +26,3 @@ "license": "MIT", | ||
"@scure/bip39": "1.1.0", | ||
"@stacks/common": "^6.14.1-pr.25+f66fbb0d", | ||
"@stacks/common": "^6.14.1-pr.26+4721d1ac", | ||
"@types/node": "^18.0.4", | ||
@@ -36,8 +36,7 @@ "base64-js": "^1.5.1", | ||
"@peculiar/webcrypto": "^1.1.6", | ||
"@stacks/network": "^6.14.1-pr.25+f66fbb0d", | ||
"@stacks/transactions": "^6.14.1-pr.25+f66fbb0d", | ||
"@stacks/network": "^6.14.1-pr.26+4721d1ac", | ||
"@stacks/transactions": "^6.14.1-pr.26+4721d1ac", | ||
"@types/bs58check": "^2.1.0", | ||
"@types/elliptic": "^6.4.12", | ||
"@types/sha.js": "^2.4.0", | ||
"@types/triplesec": "^3.0.0", | ||
"bitcoinjs-lib": "^5.2.0", | ||
@@ -50,4 +49,3 @@ "bs58check": "^2.1.2", | ||
"rimraf": "^3.0.2", | ||
"stream-browserify": "^3.0.0", | ||
"triplesec": "^4.0.3" | ||
"stream-browserify": "^3.0.0" | ||
}, | ||
@@ -74,3 +72,3 @@ "sideEffects": false, | ||
}, | ||
"gitHead": "f66fbb0d07fe7d1610de56f08ff505e03ff511b2" | ||
"gitHead": "4721d1ac086d773689b9cddc062329df4d0d436f" | ||
} |
@@ -1,4 +0,2 @@ | ||
// https://github.com/paulmillr/scure-bip39 | ||
// Secure, audited & minimal implementation of BIP39 mnemonic phrases. | ||
import { validateMnemonic, mnemonicToEntropy, entropyToMnemonic } from '@scure/bip39'; | ||
import { entropyToMnemonic, mnemonicToEntropy, validateMnemonic } from '@scure/bip39'; | ||
// Word lists not imported by default as that would increase bundle sizes too much as in case of bitcoinjs/bip39 | ||
@@ -10,16 +8,8 @@ // Use default english world list similiar to bitcoinjs/bip39 | ||
import { wordlist } from '@scure/bip39/wordlists/english'; | ||
import { randomBytes, GetRandomBytes } from './cryptoRandom'; | ||
import { createSha2Hash } from './sha2Hash'; | ||
import { bytesToHex, concatBytes, equals, hexToBytes } from '@stacks/common'; | ||
import { createCipher } from './aesCipher'; | ||
import { GetRandomBytes, randomBytes } from './cryptoRandom'; | ||
import { hmacSha256 } from './ec'; | ||
import { createPbkdf2 } from './pbkdf2'; | ||
import { TriplesecDecryptSignature } from './cryptoUtils'; | ||
import { | ||
bytesToHex, | ||
bytesToUtf8, | ||
concatBytes, | ||
equals, | ||
hexToBytes, | ||
utf8ToBytes, | ||
} from '@stacks/common'; | ||
import { hmacSha256 } from './ec'; | ||
import { createSha2Hash } from './sha2Hash'; | ||
@@ -71,4 +61,3 @@ /** | ||
const payload = concatBytes(salt, hmacDigest, cipherText); | ||
return payload; | ||
return concatBytes(salt, hmacDigest, cipherText); | ||
} | ||
@@ -128,36 +117,3 @@ | ||
/** | ||
* Decrypt legacy triplesec keys | ||
* @param {Uint8Array} dataBytes - The encrypted key | ||
* @param {String} password - Password for data | ||
* @return {Promise<BuUint8Arrayffer>} Decrypted seed | ||
* @ignore | ||
*/ | ||
function decryptLegacy( | ||
dataBytes: Uint8Array, | ||
password: string, | ||
triplesecDecrypt?: TriplesecDecryptSignature | ||
): Promise<Uint8Array> { | ||
return new Promise<Uint8Array>((resolve, reject) => { | ||
if (!triplesecDecrypt) { | ||
reject(new Error('The `triplesec.decrypt` function must be provided')); | ||
} | ||
triplesecDecrypt!( | ||
{ | ||
key: utf8ToBytes(password), | ||
data: dataBytes, | ||
}, | ||
(err, plaintextBytes) => { | ||
if (!err) { | ||
resolve(plaintextBytes!); | ||
} else { | ||
reject(err); | ||
} | ||
} | ||
); | ||
}); | ||
} | ||
/** | ||
* Decrypt an encrypted mnemonic phrase with a password. | ||
* Legacy triplesec encrypted payloads are also supported. | ||
* @param data - Bytes or hex-encoded string of the encrypted mnemonic | ||
@@ -170,13 +126,6 @@ * @param password - Password for data | ||
data: string | Uint8Array, | ||
password: string, | ||
triplesecDecrypt?: TriplesecDecryptSignature | ||
password: string | ||
): Promise<string> { | ||
const dataBytes = typeof data === 'string' ? hexToBytes(data) : data; | ||
try { | ||
return await decryptMnemonicBytes(dataBytes, password); | ||
} catch (error) { | ||
if (error instanceof PasswordError) throw error; | ||
const data = await decryptLegacy(dataBytes, password, triplesecDecrypt); | ||
return bytesToUtf8(data); | ||
} | ||
return await decryptMnemonicBytes(dataBytes, password); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
14
674381
3923