@multiversx/sdk-wallet
Advanced tools
Comparing version 3.0.0-alpha.3 to 3.0.0
/// <reference types="node" /> | ||
import { EncryptedData } from "./encryptedData"; | ||
import { Randomness } from "./randomness"; | ||
interface IRandomness { | ||
id: string; | ||
iv: Buffer; | ||
salt: Buffer; | ||
} | ||
export declare enum EncryptorVersion { | ||
@@ -8,3 +12,4 @@ V4 = 4 | ||
export declare class Encryptor { | ||
static encrypt(version: EncryptorVersion, data: Buffer, password: string, randomness?: Randomness): EncryptedData; | ||
static encrypt(data: Buffer, password: string, randomness?: IRandomness): EncryptedData; | ||
} | ||
export {}; |
@@ -8,3 +8,2 @@ "use strict"; | ||
const crypto_1 = __importDefault(require("crypto")); | ||
const errors_1 = require("../errors"); | ||
const constants_1 = require("./constants"); | ||
@@ -19,6 +18,3 @@ const derivationParams_1 = require("./derivationParams"); | ||
class Encryptor { | ||
static encrypt(version, data, password, randomness = new randomness_1.Randomness()) { | ||
if (version != EncryptorVersion.V4) { | ||
throw new errors_1.Err(`Encryptor: unsupported version ${version}`); | ||
} | ||
static encrypt(data, password, randomness = new randomness_1.Randomness()) { | ||
const kdParams = new derivationParams_1.ScryptKeyDerivationParams(); | ||
@@ -32,3 +28,3 @@ const derivedKey = kdParams.generateDerivedKey(Buffer.from(password), randomness.salt); | ||
return new encryptedData_1.EncryptedData({ | ||
version: version, | ||
version: EncryptorVersion.V4, | ||
id: randomness.id, | ||
@@ -35,0 +31,0 @@ ciphertext: ciphertext.toString('hex'), |
@@ -7,3 +7,3 @@ import { UserSecretKey } from "./userKeys"; | ||
static fromString(text: string): Mnemonic; | ||
private static assertTextIsValid; | ||
static assertTextIsValid(text: string): void; | ||
deriveKey(addressIndex?: number, password?: string): UserSecretKey; | ||
@@ -10,0 +10,0 @@ getWords(): string[]; |
@@ -5,5 +5,5 @@ "use strict"; | ||
const bip39_1 = require("bip39"); | ||
const userKeys_1 = require("./userKeys"); | ||
const ed25519_hd_key_1 = require("ed25519-hd-key"); | ||
const errors_1 = require("./errors"); | ||
const userKeys_1 = require("./userKeys"); | ||
const MNEMONIC_STRENGTH = 256; | ||
@@ -10,0 +10,0 @@ const BIP44_DERIVATION_PREFIX = "m/44'/508'/0'/0'"; |
/// <reference types="node" /> | ||
import { EncryptedData, EncryptorVersion, Randomness } from "./crypto"; | ||
import { EncryptedData } from "./crypto"; | ||
import { Mnemonic } from "./mnemonic"; | ||
import { UserSecretKey } from "./userKeys"; | ||
export declare enum EnvelopeVersion { | ||
V4 = 4, | ||
V5 = 5 | ||
interface IRandomness { | ||
id: string; | ||
iv: Buffer; | ||
salt: Buffer; | ||
} | ||
export declare enum UserWalletKind { | ||
SecretKey = "secretKey", | ||
Mnemonic = "mnemonic", | ||
Arbitrary = "arbitrary" | ||
Mnemonic = "mnemonic" | ||
} | ||
export declare class UserWallet { | ||
private readonly envelopeVersion; | ||
private readonly kind; | ||
@@ -19,23 +19,12 @@ private readonly encryptedData; | ||
private constructor(); | ||
static fromSecretKey({ envelopeVersion, encryptorVersion, secretKey, password, randomness, }: { | ||
envelopeVersion?: EnvelopeVersion; | ||
encryptorVersion?: EncryptorVersion; | ||
static fromSecretKey({ secretKey, password, randomness }: { | ||
secretKey: UserSecretKey; | ||
password: string; | ||
randomness?: Randomness; | ||
randomness?: IRandomness; | ||
}): UserWallet; | ||
static fromMnemonic({ envelopeVersion, encryptorVersion, mnemonic, password, randomness, }: { | ||
envelopeVersion?: EnvelopeVersion; | ||
encryptorVersion?: EncryptorVersion; | ||
static fromMnemonic({ mnemonic, password, randomness }: { | ||
mnemonic: string; | ||
password: string; | ||
randomness?: Randomness; | ||
randomness?: IRandomness; | ||
}): UserWallet; | ||
static fromArbitrary({ envelopeVersion, encryptorVersion, arbitraryData, password, randomness, }: { | ||
envelopeVersion?: EnvelopeVersion; | ||
encryptorVersion?: EncryptorVersion; | ||
arbitraryData: Buffer; | ||
password: string; | ||
randomness?: Randomness; | ||
}): UserWallet; | ||
/** | ||
@@ -52,4 +41,3 @@ * Copied from: https://github.com/multiversx/mx-deprecated-core-js/blob/v1.28.0/src/account.js#L42 | ||
static decryptSecretKey(keyFileObject: any, password: string): UserSecretKey; | ||
static decryptMnemonic(keyFileObject: any, password: string): string; | ||
static decryptArbitrary(keyFileObject: any, password: string): Buffer; | ||
static decryptMnemonic(keyFileObject: any, password: string): Mnemonic; | ||
static edFromJSON(keyfileObject: any): EncryptedData; | ||
@@ -60,5 +48,6 @@ /** | ||
toJSON(): any; | ||
getEnvelopeWhenKindIsSecretKey(): any; | ||
private toJSONWhenKindIsSecretKey; | ||
getCryptoSectionAsJSON(): any; | ||
getEnvelopeWhenKindIsMnemonicOrArbitrary(): any; | ||
toJSONWhenKindIsMnemonic(): any; | ||
} | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.UserWallet = exports.UserWalletKind = exports.EnvelopeVersion = void 0; | ||
exports.UserWallet = exports.UserWalletKind = void 0; | ||
const crypto_1 = require("./crypto"); | ||
const derivationParams_1 = require("./crypto/derivationParams"); | ||
const errors_1 = require("./errors"); | ||
const mnemonic_1 = require("./mnemonic"); | ||
const userKeys_1 = require("./userKeys"); | ||
var EnvelopeVersion; | ||
(function (EnvelopeVersion) { | ||
// Does not have the "kind" field, and is meant to hold the **secret key**. | ||
// The "crypto" section is not versioned. | ||
EnvelopeVersion[EnvelopeVersion["V4"] = 4] = "V4"; | ||
// Has the "kind" field, and is meant to hold the **secret key** or **the mnemonic** (or any other secret payload). | ||
// Furthermore, the "crypto" section is versioned separately. | ||
EnvelopeVersion[EnvelopeVersion["V5"] = 5] = "V5"; | ||
})(EnvelopeVersion = exports.EnvelopeVersion || (exports.EnvelopeVersion = {})); | ||
var UserWalletKind; | ||
@@ -21,7 +13,5 @@ (function (UserWalletKind) { | ||
UserWalletKind["Mnemonic"] = "mnemonic"; | ||
UserWalletKind["Arbitrary"] = "arbitrary"; | ||
})(UserWalletKind = exports.UserWalletKind || (exports.UserWalletKind = {})); | ||
class UserWallet { | ||
constructor({ envelopeVersion: envelopeVersion, kind, encryptedData, publicKeyWhenKindIsSecretKey }) { | ||
this.envelopeVersion = envelopeVersion; | ||
constructor({ kind, encryptedData, publicKeyWhenKindIsSecretKey }) { | ||
this.kind = kind; | ||
@@ -31,12 +21,8 @@ this.encryptedData = encryptedData; | ||
} | ||
static fromSecretKey({ envelopeVersion, encryptorVersion, secretKey, password, randomness, }) { | ||
envelopeVersion = envelopeVersion || EnvelopeVersion.V4; | ||
encryptorVersion = encryptorVersion || crypto_1.EncryptorVersion.V4; | ||
static fromSecretKey({ secretKey, password, randomness }) { | ||
randomness = randomness || new crypto_1.Randomness(); | ||
requireVersion(envelopeVersion, [EnvelopeVersion.V4, EnvelopeVersion.V5]); | ||
const publicKey = secretKey.generatePublicKey(); | ||
const text = Buffer.concat([secretKey.valueOf(), publicKey.valueOf()]); | ||
const encryptedData = crypto_1.Encryptor.encrypt(encryptorVersion, text, password, randomness); | ||
const data = Buffer.concat([secretKey.valueOf(), publicKey.valueOf()]); | ||
const encryptedData = crypto_1.Encryptor.encrypt(data, password, randomness); | ||
return new UserWallet({ | ||
envelopeVersion: envelopeVersion, | ||
kind: UserWalletKind.SecretKey, | ||
@@ -47,10 +33,8 @@ encryptedData, | ||
} | ||
static fromMnemonic({ envelopeVersion, encryptorVersion, mnemonic, password, randomness, }) { | ||
envelopeVersion = envelopeVersion || EnvelopeVersion.V5; | ||
encryptorVersion = encryptorVersion || crypto_1.EncryptorVersion.V4; | ||
static fromMnemonic({ mnemonic, password, randomness }) { | ||
randomness = randomness || new crypto_1.Randomness(); | ||
requireVersion(envelopeVersion, [EnvelopeVersion.V5]); | ||
const encryptedData = crypto_1.Encryptor.encrypt(encryptorVersion, Buffer.from(mnemonic), password, randomness); | ||
mnemonic_1.Mnemonic.assertTextIsValid(mnemonic); | ||
const data = Buffer.from(mnemonic); | ||
const encryptedData = crypto_1.Encryptor.encrypt(data, password, randomness); | ||
return new UserWallet({ | ||
envelopeVersion: envelopeVersion, | ||
kind: UserWalletKind.Mnemonic, | ||
@@ -60,14 +44,2 @@ encryptedData | ||
} | ||
static fromArbitrary({ envelopeVersion, encryptorVersion, arbitraryData, password, randomness, }) { | ||
envelopeVersion = envelopeVersion || EnvelopeVersion.V5; | ||
encryptorVersion = encryptorVersion || crypto_1.EncryptorVersion.V4; | ||
randomness = randomness || new crypto_1.Randomness(); | ||
requireVersion(envelopeVersion, [EnvelopeVersion.V5]); | ||
const encryptedData = crypto_1.Encryptor.encrypt(encryptorVersion, arbitraryData, password, randomness); | ||
return new UserWallet({ | ||
envelopeVersion: envelopeVersion, | ||
kind: UserWalletKind.Arbitrary, | ||
encryptedData | ||
}); | ||
} | ||
/** | ||
@@ -84,6 +56,3 @@ * Copied from: https://github.com/multiversx/mx-deprecated-core-js/blob/v1.28.0/src/account.js#L42 | ||
static decryptSecretKey(keyFileObject, password) { | ||
requireVersion(keyFileObject.version, [EnvelopeVersion.V4, EnvelopeVersion.V5]); | ||
if (keyFileObject.version >= EnvelopeVersion.V5) { | ||
requireKind(keyFileObject.kind, UserWalletKind.SecretKey); | ||
} | ||
// Here, we do not check the "kind" field. Older keystore files (holding only secret keys) do not have this field. | ||
const encryptedData = UserWallet.edFromJSON(keyFileObject); | ||
@@ -99,23 +68,13 @@ let text = crypto_1.Decryptor.decrypt(encryptedData, password); | ||
static decryptMnemonic(keyFileObject, password) { | ||
requireVersion(keyFileObject.version, [EnvelopeVersion.V5]); | ||
requireKind(keyFileObject.kind, UserWalletKind.Mnemonic); | ||
if (keyFileObject.kind != UserWalletKind.Mnemonic) { | ||
throw new errors_1.Err(`Expected kind to be ${UserWalletKind.Mnemonic}, but it was ${keyFileObject.kind}.`); | ||
} | ||
const encryptedData = UserWallet.edFromJSON(keyFileObject); | ||
const text = crypto_1.Decryptor.decrypt(encryptedData, password); | ||
return text.toString(); | ||
} | ||
static decryptArbitrary(keyFileObject, password) { | ||
requireVersion(keyFileObject.version, [EnvelopeVersion.V5]); | ||
requireKind(keyFileObject.kind, UserWalletKind.Arbitrary); | ||
const encryptedData = UserWallet.edFromJSON(keyFileObject); | ||
const data = crypto_1.Decryptor.decrypt(encryptedData, password); | ||
return data; | ||
const mnemonic = mnemonic_1.Mnemonic.fromString(data.toString()); | ||
return mnemonic; | ||
} | ||
static edFromJSON(keyfileObject) { | ||
const encryptorVersion = (keyfileObject.version == EnvelopeVersion.V4) ? | ||
// In V4, the "crypto" section inherits the version from the envelope. | ||
crypto_1.EncryptorVersion.V4 : | ||
// In V5, the "crypto" section has its own version. | ||
keyfileObject.crypto.version; | ||
return new crypto_1.EncryptedData({ | ||
version: encryptorVersion, | ||
version: keyfileObject.version, | ||
id: keyfileObject.id, | ||
@@ -136,7 +95,7 @@ cipher: keyfileObject.crypto.cipher, | ||
if (this.kind == UserWalletKind.SecretKey) { | ||
return this.getEnvelopeWhenKindIsSecretKey(); | ||
return this.toJSONWhenKindIsSecretKey(); | ||
} | ||
return this.getEnvelopeWhenKindIsMnemonicOrArbitrary(); | ||
return this.toJSONWhenKindIsMnemonic(); | ||
} | ||
getEnvelopeWhenKindIsSecretKey() { | ||
toJSONWhenKindIsSecretKey() { | ||
if (!this.publicKeyWhenKindIsSecretKey) { | ||
@@ -146,7 +105,19 @@ throw new errors_1.Err("Public key isn't available"); | ||
const cryptoSection = this.getCryptoSectionAsJSON(); | ||
const envelope = Object.assign(Object.assign({ version: this.envelopeVersion }, (this.envelopeVersion >= 5 ? { kind: UserWalletKind.SecretKey } : {})), { id: this.encryptedData.id, address: this.publicKeyWhenKindIsSecretKey.hex(), bech32: this.publicKeyWhenKindIsSecretKey.toAddress().toString(), crypto: cryptoSection }); | ||
const envelope = { | ||
version: this.encryptedData.version, | ||
kind: this.kind, | ||
id: this.encryptedData.id, | ||
address: this.publicKeyWhenKindIsSecretKey.hex(), | ||
bech32: this.publicKeyWhenKindIsSecretKey.toAddress().toString(), | ||
crypto: cryptoSection | ||
}; | ||
return envelope; | ||
} | ||
getCryptoSectionAsJSON() { | ||
const cryptoSection = Object.assign(Object.assign({}, (this.envelopeVersion >= 5 ? { version: this.encryptedData.version } : {})), { ciphertext: this.encryptedData.ciphertext, cipherparams: { iv: this.encryptedData.iv }, cipher: crypto_1.CipherAlgorithm, kdf: crypto_1.KeyDerivationFunction, kdfparams: { | ||
const cryptoSection = { | ||
ciphertext: this.encryptedData.ciphertext, | ||
cipherparams: { iv: this.encryptedData.iv }, | ||
cipher: crypto_1.CipherAlgorithm, | ||
kdf: crypto_1.KeyDerivationFunction, | ||
kdfparams: { | ||
dklen: this.encryptedData.kdfparams.dklen, | ||
@@ -157,9 +128,11 @@ salt: this.encryptedData.salt, | ||
p: this.encryptedData.kdfparams.p | ||
}, mac: this.encryptedData.mac }); | ||
}, | ||
mac: this.encryptedData.mac, | ||
}; | ||
return cryptoSection; | ||
} | ||
getEnvelopeWhenKindIsMnemonicOrArbitrary() { | ||
toJSONWhenKindIsMnemonic() { | ||
const cryptoSection = this.getCryptoSectionAsJSON(); | ||
return { | ||
version: this.envelopeVersion, | ||
version: this.encryptedData.version, | ||
id: this.encryptedData.id, | ||
@@ -172,13 +145,2 @@ kind: this.kind, | ||
exports.UserWallet = UserWallet; | ||
function requireKind(kind, expectedKind) { | ||
if (kind != expectedKind) { | ||
throw new errors_1.Err(`Expected kind to be ${expectedKind}, but it was ${kind}.`); | ||
} | ||
} | ||
function requireVersion(version, allowedVersions) { | ||
const isAllowed = allowedVersions.includes(version); | ||
if (!isAllowed) { | ||
throw new errors_1.Err(`Envelope version must be one of: [${allowedVersions.join(", ")}].`); | ||
} | ||
} | ||
//# sourceMappingURL=userWallet.js.map |
{ | ||
"name": "@multiversx/sdk-wallet", | ||
"version": "3.0.0-alpha.3", | ||
"version": "3.0.0", | ||
"description": "Wallet components for MultiversX", | ||
@@ -5,0 +5,0 @@ "main": "out/index.js", |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
84483
1433