@tedcryptoorg/cosmos-signer
Version:
Cosmos Signer - A library for signing transactions for Cosmos SDK chains
172 lines (171 loc) • 7.1 kB
JavaScript
"use strict";
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 });
class BaseWallet {
constructor(provider, logger) {
this.provider = provider;
this.logger = logger;
this.key = null;
this.signer = null;
this.suggestChainSupport = true;
this.options = {};
}
available() {
return !!this.provider;
}
connected() {
return this.available();
}
isLedger() {
var _a, _b, _c, _d;
return (_d = (_b = (_a = this.key) === null || _a === void 0 ? void 0 : _a.isNanoLedger) !== null && _b !== void 0 ? _b : (_c = this.key) === null || _c === void 0 ? void 0 : _c.isHardware) !== null && _d !== void 0 ? _d : false;
}
signDirectSupport() {
var _a;
return !!((_a = this.signer) === null || _a === void 0 ? void 0 : _a.signDirect);
}
signAminoSupport() {
var _a;
return !!((_a = this.signer) === null || _a === void 0 ? void 0 : _a.signAmino);
}
getOptions() {
return this.options;
}
setOptions(options) {
this.options = options;
}
connect(network) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
this.key = null;
this.signer = null;
try {
yield this.enable(network);
yield this.getKey(network);
yield this.getSigner(network);
return this.key;
}
catch (e) {
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.log(e);
if (!this.suggestChainSupport) {
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.log('Suggest chain not supported', network, e);
throw new Error('Failed to connect wallet and suggest chain is not supported.');
}
try {
yield this.suggestChain(network);
yield this.getKey(network);
yield this.getSigner(network);
return this.key;
}
catch (s) {
(_c = this.logger) === null || _c === void 0 ? void 0 : _c.log('Failed to suggest the chain and connect', e, s);
throw new Error('Failed to connect wallet and suggest chain');
}
}
});
}
disconnect() {
var _a;
this.key = null;
this.signer = null;
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.log('Disconnecting wallet');
}
enable(network) {
return __awaiter(this, void 0, void 0, function* () {
const { chainId } = network;
yield this.provider.enable(chainId);
});
}
getKey(network) {
return __awaiter(this, void 0, void 0, function* () {
if (this.key === null) {
const { chainId } = network;
this.key = yield this.provider.getKey(chainId);
}
return this.key;
});
}
getSigner(network) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.signer) {
const { chainId } = network;
this.signer = yield this.provider.getOfflineSignerAuto(chainId);
}
return this.signer;
});
}
getAddress() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
if ((_a = this.signer) === null || _a === void 0 ? void 0 : _a.getAddress) {
return yield this.signer.getAddress();
}
else {
const accounts = yield this.getAccounts();
if (accounts === undefined || accounts.length === 0) {
throw new Error('No accounts found in wallet');
}
return accounts[0].address;
}
});
}
getAccounts() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
return yield ((_a = this.signer) === null || _a === void 0 ? void 0 : _a.getAccounts());
});
}
signDirect(...args) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
return yield ((_a = this.signer) === null || _a === void 0 ? void 0 : _a.signDirect(...args));
});
}
signAmino(...args) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
return yield ((_a = this.signer) === null || _a === void 0 ? void 0 : _a.signAmino(...args));
});
}
suggestChain(network) {
return __awaiter(this, void 0, void 0, function* () {
if (this.suggestChainSupport) {
yield this.provider.experimentalSuggestChain(this.suggestChainData(network));
}
else {
throw new Error(`${network.prettyName} (${network.chainId}) is not supported`);
}
});
}
suggestChainData(network) {
const currency = {
coinDenom: network.symbol,
coinMinimalDenom: network.denom,
coinDecimals: network.decimals
};
if (network.coinGeckoId) {
currency.coinGeckoId = network.coinGeckoId;
}
return Object.assign({ rpc: network.rpcUrl, rest: network.restUrl, chainId: network.chainId, chainName: network.prettyName, stakeCurrency: currency, bip44: { coinType: network.slip44 }, walletUrlForStaking: "https://restake.app/" + network.name, bech32Config: {
bech32PrefixAccAddr: network.prefix,
bech32PrefixAccPub: network.prefix + "pub",
bech32PrefixValAddr: network.prefix + "valoper",
bech32PrefixValPub: network.prefix + "valoperpub",
bech32PrefixConsAddr: network.prefix + "valcons",
bech32PrefixConsPub: network.prefix + "valconspub"
}, currencies: [currency], feeCurrencies: [Object.assign(Object.assign({}, currency), { gasPriceStep: network.gasPriceStep })] }, (network.data.keplrFeatures !== undefined
? { features: network.data.keplrFeatures }
: (network.slip44 === 60
? { features: ["ibc-transfer", "ibc-go", "eth-address-gen", "eth-key-sign"] }
: {})));
}
}
exports.default = BaseWallet;