cache-typescript-sdk
Version:
Blockstart NIS1 SDK
154 lines • 6.08 kB
JavaScript
"use strict";
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 NEM
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const CryptoJS = require("crypto-js");
const nemSdk = require("nem-sdk");
const Address_1 = require("../models/account/Address");
const XEM_1 = require("../models/mosaic/XEM");
const NetworkTypes_1 = require("../models/node/NetworkTypes");
const PlainMessage_1 = require("../models/transaction/PlainMessage");
const TransferTransaction_1 = require("../models/transaction/TransferTransaction");
const get_random_values = require("get-random-values");
class QRService {
/**
* Generates the QR text from the wallet
* @returns {string}
*/
generateWalletQRText(password, wallet) {
// Decrypt/generate private key and check it. Returned private key is contained into this.common
const privateKey = wallet.unlockPrivateKey(password);
const mobileKeys = this.AES_PBKF2_encryption(password, privateKey);
const QR = {
v: wallet.network == NetworkTypes_1.NetworkTypes.TEST_NET ? 1 : 2,
type: 3,
data: {
name: wallet.name,
priv_key: mobileKeys.encrypted,
salt: mobileKeys.salt,
},
};
return JSON.stringify(QR);
}
/**
* Decrypt the private key from the QR text
* @param password password
* @param qrWalletText Object generated by generateWalletQRText method
* @return Decrypted private key
*/
decryptWalletQRText(password, qrWalletText) {
const encryptedData = qrWalletText.data;
const salt = CryptoJS.enc.Hex.parse(encryptedData.salt);
const encrypted = encryptedData.priv_key;
//generate key
const key = CryptoJS.PBKDF2(password.value, salt, {
keySize: 256 / 32,
iterations: 2000,
}).toString();
//separated from priv_key iv and cipherdata
const iv = encrypted.substring(0, 32);
const encryptedPrvKey = encrypted.substring(32, 128);
//separated vh from priv_key iv and cipherdata
const obj = {
ciphertext: CryptoJS.enc.Hex.parse(encryptedPrvKey),
iv: nemSdk.default.utils.convert.hex2ua(iv),
key: nemSdk.default.utils.convert.hex2ua(key.toString()),
};
const decrypt = nemSdk.default.crypto.helpers.decrypt(obj);
if (decrypt === "" || (decrypt.length != 64 && decrypt.length != 66))
throw new Error("invalid password");
return decrypt;
}
/**
* Generates the QR text from an address
* @returns {string}
*/
generateAddressQRText(address) {
const QR = {
v: address.network() == NetworkTypes_1.NetworkTypes.TEST_NET ? 1 : 2,
type: 1,
data: {
addr: address.plain(),
name: "wallet",
},
};
return JSON.stringify(QR);
}
/**
* Decrypt the address from the QR text
* @param qrAddressText Object generated by generateAddressQRText method
* @return Address
*/
decryptAddressQRText(qrAddressText) {
return new Address_1.Address(qrAddressText.data.addr);
}
/**
* Generates the QR text from a transaction
* @returns {string}
*/
generateTransactionQRText(recipientAddress, amount, msg) {
const QR = {
v: recipientAddress.network() == NetworkTypes_1.NetworkTypes.TEST_NET ? 1 : 2,
type: 2,
data: {
addr: recipientAddress.plain(),
amount: amount,
msg: msg,
},
};
return JSON.stringify(QR);
}
/**
* Decrypt the transaction from the QR text
* @param qrTransactionText Object generated by generateTransactionQRText method
* @return TransferTransaction
*/
decryptTrasactionQRText(qrTransactionText) {
return TransferTransaction_1.TransferTransaction.create(new Address_1.Address(qrTransactionText.data.addr), new XEM_1.XEM(qrTransactionText.data.amount), PlainMessage_1.PlainMessage.create(qrTransactionText.data.msg));
}
/**
* @internal
* @param password
* @param privateKey
*/
AES_PBKF2_encryption(password, privateKey) {
const salt = CryptoJS.lib.WordArray.random(256 / 8);
const key = CryptoJS.PBKDF2(password.value, salt, {
keySize: 256 / 32,
iterations: 2000,
});
const iv = new Uint8Array(16);
get_random_values(iv);
const encIv = {
iv: nemSdk.default.utils.convert.ua2words(iv, 16),
};
const encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Hex.parse(privateKey), key, encIv);
return {
encrypted: nemSdk.default.utils.convert.ua2hex(iv) + encrypted.ciphertext,
salt: salt.toString(),
};
}
}
exports.QRService = QRService;
//# sourceMappingURL=QRService.js.map