@stacks/encryption
Advanced tools
Comparing version 6.14.0-beta.0 to 6.14.1-next.31
@@ -0,1 +1,2 @@ | ||
import { PrivateKey } from '@stacks/common'; | ||
export declare function makeECPrivateKey(): string; | ||
@@ -9,5 +10,5 @@ export declare function base58CheckDecode(btcAddress: string): { | ||
export declare function publicKeyToBtcAddress(publicKey: string | Uint8Array, version?: number): string; | ||
export declare function getPublicKeyFromPrivate(privateKey: string | Uint8Array): string; | ||
export declare function ecSign(messageHash: Uint8Array, hexPrivateKey: string | Uint8Array): Uint8Array; | ||
export declare function isValidPrivateKey(privateKey: string | Uint8Array): boolean; | ||
export declare function compressPrivateKey(privateKey: string | Uint8Array): Uint8Array; | ||
export declare function getPublicKeyFromPrivate(privateKey: PrivateKey): string; | ||
export declare function ecSign(messageHash: Uint8Array, privateKey: PrivateKey): Uint8Array; | ||
export declare function isValidPrivateKey(privateKey: PrivateKey): boolean; | ||
export declare function compressPrivateKey(privateKey: PrivateKey): Uint8Array; |
import { hmac } from '@noble/hashes/hmac'; | ||
import { sha256 } from '@noble/hashes/sha256'; | ||
import { getPublicKey as nobleGetPublicKey, signSync, utils } from '@noble/secp256k1'; | ||
import { bytesToHex, concatBytes, hexToBytes, privateKeyToBytes, PRIVATE_KEY_COMPRESSED_LENGTH, readUInt8, } from '@stacks/common'; | ||
import { PRIVATE_KEY_COMPRESSED_LENGTH, bytesToHex, concatBytes, hexToBytes, privateKeyToBytes, readUInt8, } from '@stacks/common'; | ||
import base58 from 'bs58'; | ||
@@ -50,4 +50,4 @@ import { hashRipemd160 } from './hashRipemd160'; | ||
} | ||
export function ecSign(messageHash, hexPrivateKey) { | ||
return signSync(messageHash, privateKeyToBytes(hexPrivateKey).slice(0, 32), { | ||
export function ecSign(messageHash, privateKey) { | ||
return signSync(messageHash, privateKeyToBytes(privateKey).slice(0, 32), { | ||
der: false, | ||
@@ -54,0 +54,0 @@ }); |
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 |
@@ -0,1 +1,2 @@ | ||
import { PrivateKey } from '@stacks/common'; | ||
export declare function makeECPrivateKey(): string; | ||
@@ -9,5 +10,5 @@ export declare function base58CheckDecode(btcAddress: string): { | ||
export declare function publicKeyToBtcAddress(publicKey: string | Uint8Array, version?: number): string; | ||
export declare function getPublicKeyFromPrivate(privateKey: string | Uint8Array): string; | ||
export declare function ecSign(messageHash: Uint8Array, hexPrivateKey: string | Uint8Array): Uint8Array; | ||
export declare function isValidPrivateKey(privateKey: string | Uint8Array): boolean; | ||
export declare function compressPrivateKey(privateKey: string | Uint8Array): Uint8Array; | ||
export declare function getPublicKeyFromPrivate(privateKey: PrivateKey): string; | ||
export declare function ecSign(messageHash: Uint8Array, privateKey: PrivateKey): Uint8Array; | ||
export declare function isValidPrivateKey(privateKey: PrivateKey): boolean; | ||
export declare function compressPrivateKey(privateKey: PrivateKey): Uint8Array; |
@@ -62,4 +62,4 @@ "use strict"; | ||
exports.getPublicKeyFromPrivate = getPublicKeyFromPrivate; | ||
function ecSign(messageHash, hexPrivateKey) { | ||
return (0, secp256k1_1.signSync)(messageHash, (0, common_1.privateKeyToBytes)(hexPrivateKey).slice(0, 32), { | ||
function ecSign(messageHash, privateKey) { | ||
return (0, secp256k1_1.signSync)(messageHash, (0, common_1.privateKeyToBytes)(privateKey).slice(0, 32), { | ||
der: false, | ||
@@ -66,0 +66,0 @@ }); |
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.0-beta.0", | ||
"version": "6.14.1-next.31+e618d2f3", | ||
"description": "Encryption utilities for Stacks", | ||
@@ -26,3 +26,3 @@ "license": "MIT", | ||
"@scure/bip39": "1.1.0", | ||
"@stacks/common": "^6.14.0-beta.0", | ||
"@stacks/common": "^6.14.1-next.31+e618d2f3", | ||
"@types/node": "^18.0.4", | ||
@@ -36,7 +36,7 @@ "base64-js": "^1.5.1", | ||
"@peculiar/webcrypto": "^1.1.6", | ||
"@stacks/transactions": "^6.14.0-beta.0", | ||
"@stacks/network": "^6.14.1-next.31+e618d2f3", | ||
"@stacks/transactions": "^6.14.1-next.31+e618d2f3", | ||
"@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", | ||
@@ -49,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" | ||
}, | ||
@@ -73,3 +72,3 @@ "sideEffects": false, | ||
}, | ||
"gitHead": "a91f28c64f6201435ee8c7ca328c6c5100ab2f0a" | ||
"gitHead": "e618d2f38d09bddd441311db9fa6cc549f702c4e" | ||
} |
@@ -5,2 +5,4 @@ import { hmac } from '@noble/hashes/hmac'; | ||
import { | ||
PRIVATE_KEY_COMPRESSED_LENGTH, | ||
PrivateKey, | ||
bytesToHex, | ||
@@ -10,3 +12,2 @@ concatBytes, | ||
privateKeyToBytes, | ||
PRIVATE_KEY_COMPRESSED_LENGTH, | ||
readUInt8, | ||
@@ -101,3 +102,3 @@ } from '@stacks/common'; | ||
*/ | ||
export function getPublicKeyFromPrivate(privateKey: string | Uint8Array): string { | ||
export function getPublicKeyFromPrivate(privateKey: PrivateKey): string { | ||
const privateKeyBytes = privateKeyToBytes(privateKey); | ||
@@ -111,4 +112,4 @@ // for backwards compatibility we always return a compressed public key, regardless of private key mode | ||
*/ | ||
export function ecSign(messageHash: Uint8Array, hexPrivateKey: string | Uint8Array) { | ||
return signSync(messageHash, privateKeyToBytes(hexPrivateKey).slice(0, 32), { | ||
export function ecSign(messageHash: Uint8Array, privateKey: PrivateKey) { | ||
return signSync(messageHash, privateKeyToBytes(privateKey).slice(0, 32), { | ||
der: false, | ||
@@ -121,3 +122,3 @@ }); | ||
*/ | ||
export function isValidPrivateKey(privateKey: string | Uint8Array): boolean { | ||
export function isValidPrivateKey(privateKey: PrivateKey): boolean { | ||
return utils.isValidPrivateKey(privateKeyToBytes(privateKey)); | ||
@@ -129,3 +130,3 @@ } | ||
*/ | ||
export function compressPrivateKey(privateKey: string | Uint8Array): Uint8Array { | ||
export function compressPrivateKey(privateKey: PrivateKey): Uint8Array { | ||
const privateKeyBytes = privateKeyToBytes(privateKey); | ||
@@ -132,0 +133,0 @@ |
@@ -1,6 +0,4 @@ | ||
// 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 | ||
// Use default english world list similiar to bitcoinjs/bip39 | ||
// Use default english world list similar to bitcoinjs/bip39 | ||
// Backward compatible with bitcoinjs/bip39 dependency | ||
@@ -10,16 +8,8 @@ // Very small in size as compared to bitcoinjs/bip39 wordlist | ||
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 not supported yet
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
14
220
671374
3922
1
+ Added@types/node@18.19.66(transitive)
- Removed@types/node@18.19.67(transitive)