evm-blockchain-tools
Version:
This is a collection of resuseable tools to support development for EVM-powered blockchains
181 lines • 7.86 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CryptoWalletBank = void 0;
const interfaces_1 = require("../common/interfaces");
const constants_1 = require("../common/constants");
class CryptoWalletBank {
constructor(storageEngine, evmWalletEngine) {
this.storageEngine = storageEngine;
this.evmWalletEngine = evmWalletEngine;
}
importWallet(username_1, walletName_1, secret_1, data_1, type_1) {
return __awaiter(this, arguments, void 0, function* (username, walletName, secret, data, type, options = {
userKeepsOwnPrivate: false,
recoverable: true,
}) {
let createdWallet;
switch (type) {
case interfaces_1.WALLET_TYPE.EVM:
const walletData = yield this.evmWalletEngine.importWallet(data, secret);
if (options === null || options === void 0 ? void 0 : options.userKeepsOwnPrivate) {
delete walletData.userSecretPart;
}
if (!(options === null || options === void 0 ? void 0 : options.recoverable)) {
delete walletData.serverSecretPart;
}
createdWallet = yield this.storageEngine.create({
address: walletData.address,
data: walletData,
name: walletName,
ownershipType: interfaces_1.WALLET_OWNERSHIP_TYPE.MASTER,
username,
type,
metadata: options,
});
return {
success: true,
data: {
walletId: createdWallet.id,
address: createdWallet.address,
userPrivate: (options === null || options === void 0 ? void 0 : options.userKeepsOwnPrivate)
? walletData.userSecretPart
: null,
},
};
default:
throw new Error("wallet type not supported");
}
});
}
createNewWallet(username_1, walletName_1, secret_1, type_1) {
return __awaiter(this, arguments, void 0, function* (username, walletName, secret, type, options = {
userKeepsOwnPrivate: false,
recoverable: true,
}) {
let createdWallet;
switch (type) {
case interfaces_1.WALLET_TYPE.EVM:
const walletData = yield this.evmWalletEngine.createWallet(secret);
if (options === null || options === void 0 ? void 0 : options.userKeepsOwnPrivate) {
delete walletData.userSecretPart;
}
if (!(options === null || options === void 0 ? void 0 : options.recoverable)) {
delete walletData.serverSecretPart;
}
createdWallet = yield this.storageEngine.create({
address: walletData.address,
data: walletData,
name: walletName,
ownershipType: interfaces_1.WALLET_OWNERSHIP_TYPE.MASTER,
username,
type,
metadata: options,
});
return {
success: true,
data: {
walletId: createdWallet.id,
address: createdWallet.address,
userPrivate: (options === null || options === void 0 ? void 0 : options.userKeepsOwnPrivate)
? walletData.userSecretPart
: null,
},
};
default:
throw new Error("wallet type not supported");
}
});
}
deleteWallet(username, walletName) {
return __awaiter(this, void 0, void 0, function* () {
const foundWallet = yield this.storageEngine.getOne({
username,
name: walletName,
});
if (!foundWallet) {
return {
success: false,
message: "wallet not found",
code: constants_1.ERR_CODE.WALLET_NOT_FOUND,
};
}
yield this.storageEngine.deleteById(foundWallet.id);
return {
success: true,
};
});
}
getWalletInfo(username, walletName) {
return __awaiter(this, void 0, void 0, function* () {
const foundWallet = yield this.storageEngine.getOne({
username,
name: walletName,
});
if (!foundWallet) {
return {
success: false,
message: "wallet not found",
code: constants_1.ERR_CODE.WALLET_NOT_FOUND,
};
}
return {
success: true,
data: {
address: foundWallet.address,
walletId: foundWallet.id,
},
};
});
}
recoverWallet(username, walletName, secret, options) {
return __awaiter(this, void 0, void 0, function* () {
const foundWallet = yield this.storageEngine.getOne({
username,
name: walletName,
});
if (!foundWallet) {
return {
success: false,
message: "wallet not found",
code: constants_1.ERR_CODE.WALLET_NOT_FOUND,
};
}
const { type, data } = foundWallet;
const userSecret = options.userSecret || data.userSecretPart;
if (!userSecret && !data.recoverySecretPart) {
return {
success: false,
message: "missing recovery secret and user secret for recover flow",
code: constants_1.ERR_CODE.RECOVERY_SECRET_NOT_STORED,
};
}
switch (type) {
case interfaces_1.WALLET_TYPE.EVM:
const privKey = yield this.evmWalletEngine.recoverPrivateKey({
nonce: data.nonce,
userPin: secret,
userSecret,
serverSecret: data.serverSecretPart,
recoverySecret: data.recoverySecretPart,
});
return {
success: true,
data: privKey,
};
default:
throw new Error("wallet type not supported");
}
});
}
}
exports.CryptoWalletBank = CryptoWalletBank;
//# sourceMappingURL=crypto-wallet-bank.js.map