@tangany/waas
Version:
node.js SDK for Tangany Wallet as a Service API
271 lines • 12.9 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Wallet = void 0;
var t = require("typeforce");
var btc_wallet_1 = require("./btc-wallet");
var eth_wallet_1 = require("./eth-wallet");
var wallet_iterable_1 = require("./iterables/auto-pagination/wallet-iterable");
var wallet_page_iterable_1 = require("./iterables/pagewise/wallet-page-iterable");
var xtz_wallet_1 = require("./xtz-wallet");
/**
* Instantiates a new wallet interface
* @param instance - axios instance created by {@link Waas}
* @param limiter - Bottleneck limiter instance
* @param [wallet) - wallet name
*/
var Wallet = /** @class */ (function () {
function Wallet(waas, name) {
this.waas = waas;
this.name = name;
t("?String", name);
}
/**
* Converts SDK-specific objects for wallet tags into objects compatible with the API.
* @param tags - SDK-side wallet tags to be converted
*/
Wallet.convertTags = function (tags) {
// Ideally, we would have used typeforce to validate the type here as well, but for this somewhat more complex type, that doesn't seem to be so easy
return tags === null || tags === void 0 ? void 0 : tags.map(function (x) {
var _a;
return (_a = {}, _a[x.name] = x.value, _a);
});
};
Object.defineProperty(Wallet.prototype, "wallet", {
get: function () {
t("String", this.name);
return this.name;
},
enumerable: false,
configurable: true
});
Wallet.prototype.list = function (arg, options) {
var _this = this;
// The first and second overload continue to use the deprecated endpoint to avoid breaking changes
if (arg === undefined || typeof arg === "string") {
var url_1 = "wallet".concat(arg ? "?skiptoken=".concat(arg) : "");
return this.waas.wrap(function () { return _this.waas.instance.get(url_1); });
}
// All other overloads expect an object as first argument
else if (arg !== null && typeof arg === "object") {
var initialRequest = { url: "wallets", params: arg };
if (options === null || options === void 0 ? void 0 : options.autoPagination) {
return new wallet_iterable_1.WalletIterable(this.waas, initialRequest);
}
else {
return new wallet_page_iterable_1.WalletPageIterable(this.waas, initialRequest);
}
}
throw new Error("The passed argument for wallet search is invalid");
};
Wallet.prototype.create = function (wallet, useHsm) {
return __awaiter(this, void 0, void 0, function () {
var reqBody;
var _this = this;
return __generator(this, function (_a) {
if (wallet === undefined) {
reqBody = {};
}
else if (typeof wallet === "string") {
t("?Boolean", useHsm);
reqBody = { wallet: wallet, useHsm: useHsm };
}
else if (typeof wallet === "object" && !Array.isArray(wallet)) {
reqBody = {
wallet: wallet.wallet,
useHsm: wallet.useHsm,
tags: Wallet.convertTags(wallet.tags),
};
}
else {
throw new Error("The passed arguments does not match a valid method overload");
}
return [2 /*return*/, this.waas.wrap(function () { return _this.waas.instance.post("wallets", reqBody); })];
});
});
};
/**
* Updates the given wallet without regenerating the cryptographic keys.
* @param newValues - Subset of wallet properties that are allowed to be updated. Non-primitive properties like arrays or objects replace the previous value and therefore need to contain all desired values.
* @see [docs]{@link https://docs.tangany.com/#05339c0f-8cc9-48d2-91e0-c9240764dc53}
*/
Wallet.prototype.update = function (newValues) {
return __awaiter(this, void 0, void 0, function () {
var reqBody;
var _this = this;
return __generator(this, function (_a) {
t("Object", newValues);
reqBody = __assign(__assign({}, newValues), { tags: Wallet.convertTags(newValues.tags) });
return [2 /*return*/, this.waas.wrap(function () { return _this.waas.instance.patch("wallet/".concat(_this.wallet), reqBody); })];
});
});
};
Wallet.prototype.replace = function (arg) {
return __awaiter(this, void 0, void 0, function () {
var reqBody;
var _this = this;
return __generator(this, function (_a) {
if (arg === undefined) {
reqBody = {};
}
else if (typeof arg === "boolean") {
reqBody = { useHsm: arg };
}
else if (typeof arg === "object" && !Array.isArray(arg)) {
reqBody = {
useHsm: arg.useHsm,
tags: Wallet.convertTags(arg.tags),
};
}
else {
throw new Error("The passed arguments does not match a valid method overload");
}
return [2 /*return*/, this.waas.wrap(function () { return _this.waas.instance.put("wallet/".concat(_this.wallet), reqBody); })];
});
});
};
/**
* Soft-deletes a wallet so not writing operations cannot be executed for the associated blockchain account. Wallet recovery endpoints are not yet implemented into the API. Contact the support team to recover soft-deleted wallets during the retention period.
* @see [docs]{@link https://docs.tangany.com/#e0b207c8-5cdc-4dce-af6d-a6a655a1cf20}
*/
Wallet.prototype.delete = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.waas.wrap(function () { return _this.waas.instance
.delete("wallet/".concat(_this.wallet)); })];
});
});
};
/**
* Returns information for given wallet name
* @see [docs]{@link https://docs.tangany.com/#f95ba7a2-5526-4eef-b0da-7d7a13be34d2}
*/
Wallet.prototype.get = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.waas.wrap(function () { return _this.waas.instance
.get("wallet/".concat(_this.wallet)); })];
});
});
};
/**
* Signs the SHA2-256 hash of the given payload string using the ES256K algorithm.
* If a format is specified, the signature is encoded with it, otherwise DER is used by default.
* The result is then returned as base64 text.
* @param payload - Payload string to be signed
* @param [encoding] - Signature encoding to be used (`der` or `ieee-p1363`), where `der` is the default
*/
Wallet.prototype.sign = function (payload, encoding) {
return __awaiter(this, void 0, void 0, function () {
var body, signature;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
body = __assign({ payload: payload }, encoding && { encoding: encoding });
return [4 /*yield*/, this.waas.wrap(function () { return _this.waas.instance
.post("wallet/".concat(_this.wallet, "/sign"), body); })];
case 1:
signature = (_a.sent()).signature;
return [2 /*return*/, signature];
}
});
});
};
/**
* Verifies the SHA2-256 hash of the passed payload string against the given signature.
* By default, the signature is expected in DER format, but the encoding used can also be passed explicitly.
* @param payload - Payload to be compared against the passed signature
* @param signature - Signature to be verified
* @param [encoding] - Encoding used for the passed signature (`der` by default)
*/
Wallet.prototype.verifySignature = function (payload, signature, encoding) {
return __awaiter(this, void 0, void 0, function () {
var body, isValid;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
body = __assign({ payload: payload, signature: signature }, encoding && { encoding: encoding });
return [4 /*yield*/, this.waas.wrap(function () { return _this.waas.instance
.post("wallet/".concat(_this.wallet, "/verify"), body); })];
case 1:
isValid = (_a.sent()).isValid;
return [2 /*return*/, isValid];
}
});
});
};
/**
* Returns wallet calls for the Ethereum blockchain
*/
Wallet.prototype.eth = function () {
var ew = new eth_wallet_1.EthWallet(this.waas, this);
return ew;
};
/**
* Returns wallet calls for the Bitcoin blockchain
*/
Wallet.prototype.btc = function () {
var btc = new btc_wallet_1.BtcWallet(this.waas, this);
return btc;
};
/**
* Returns wallet calls for Tezos blockchain
*/
Wallet.prototype.xtz = function () {
var xtz = new xtz_wallet_1.TezosWallet(this.waas, this);
return xtz;
};
return Wallet;
}());
exports.Wallet = Wallet;
//# sourceMappingURL=wallet.js.map