UNPKG

@openweb3-io/waas

Version:

WaaS API client and WaaS verification library

348 lines 12.8 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); }; 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.WebhookClient = exports.ApiClient = void 0; const index_1 = require("./openapi/index"); __exportStar(require("./openapi/models/all"), exports); __exportStar(require("./openapi/apis/exception"), exports); const crypto_1 = require("crypto"); const nacl = require("tweetnacl"); const VERSION = "0.2.0"; class UserAgentMiddleware { pre(context) { context.setHeaderParam("User-Agent", `WaaS-libs/${VERSION}/javascript`); return Promise.resolve(context); } post(context) { return Promise.resolve(context); } } function signEd25519(data, privateKey) { const hash = crypto_1.createHash("sha256"); hash.update(Buffer.from(data, "utf-8")); const hashBuffer = hash.digest(); const keyPair = nacl.sign.keyPair.fromSeed(Buffer.from(privateKey, "hex")); const signedData = nacl.sign.detached(hashBuffer, keyPair.secretKey); const sign = Buffer.from(signedData).toString("hex"); return sign; } class SignatureMiddleware { constructor(privateKey) { this.privateKey = privateKey; } pre(context) { const timestamp = new Date().getTime().toString(); context.setHeaderParam("x-request-time", timestamp); let source = ""; const body = context.getBody(); let bodyStr = ""; if (body) { if (typeof body === "object") { bodyStr = JSON.stringify(body); } else { bodyStr = String(body); } } source += bodyStr; const url = new URL(context.getUrl()); source += url.pathname + url.search; source += timestamp; const sign = signEd25519(source, this.privateKey); context.setHeaderParam("x-signature", sign); return Promise.resolve(context); } post(context) { return Promise.resolve(context); } } class ApiClient { constructor(apikey, privateKey, options = {}) { var _a; if (apikey === "") { throw new Error("API key is required"); } if (privateKey === "") { throw new Error("Private key is required"); } const baseUrl = (_a = options.serverUrl) !== null && _a !== void 0 ? _a : "https://api.waas.openweb3.io"; const baseServer = new index_1.ServerConfiguration(baseUrl, {}); const config = index_1.createConfiguration({ baseServer, promiseMiddleware: [new UserAgentMiddleware(), new SignatureMiddleware(privateKey)], authMethods: { ApiKeyAuth: apikey, }, }); this._configuration = config; this.addresses = new Addresses(config); this.chains = new Chains(config); this.sweeps = new Sweeps(config); this.tokens = new Tokens(config); this.transactions = new Transactions(config); this.wallets = new Wallets(config); this.webhookEndpoints = new WebhookEndpoints(config); this.webhookEvents = new WebhookEvents(config); this.gasStations = new GasStations(config); } } exports.ApiClient = ApiClient; class Addresses { constructor(config) { this.api = new index_1.AddressesApi(config); } list(options) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1AddressesList(Object.assign({}, options)); }); } listByWallet(walletId, options) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1WalletsListAddresses(Object.assign({ walletId }, options)); }); } validateAddresses(chainId, addresses) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1AddressesValidate({ chainId, addresses }); }); } create(walletId, createAddressRequest) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1WalletsCreateAddress({ walletId, createAddressRequest }); }); } getAddress(walletId, address) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1WalletsGetAddress({ walletId, address }); }); } } class Chains { constructor(config) { this.api = new index_1.ChainsApi(config); } list(options) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1ChainsList(Object.assign({}, options)); }); } retrieve(chainId) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1ChainsRetrieve({ chainId }); }); } } class Sweeps { constructor(config) { this.api = new index_1.SweepsApi(config); } sweepAddress(address, sweepAddressRequest) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1SweepsAddress({ address, sweepAddressRequest }); }); } } class Tokens { constructor(config) { this.api = new index_1.TokensApi(config); } list(options) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1TokensList(Object.assign({}, options)); }); } create(createTokenRequest) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1TokensCreate({ createTokenRequest }); }); } retrieve(tokenId) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1TokensRetrieve({ tokenId }); }); } update(tokenId, updateTokenRequest) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1TokensUpdate({ tokenId, updateTokenRequest }); }); } } class Transactions { constructor(config) { this.api = new index_1.TransactionsApi(config); } list(options) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1TransactionsList(Object.assign({}, options)); }); } estimateFee(estimateFeeRequest) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1TransactionsEstimateFee({ estimateFeeRequest }); }); } transfer(createTransferRequest) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1TransactionsTransfer({ createTransferRequest }); }); } getTransaction(transactionId) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1TransactionsRetrieve({ transactionId }); }); } signMessage(signMessageRequest) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1TransactionsSignMessage({ signMessageRequest }); }); } } class Wallets { constructor(config) { this.api = new index_1.WalletsApi(config); } list(options) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1WalletsList(Object.assign({}, options)); }); } create(createWalletRequest) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1WalletsCreate({ createWalletRequest }); }); } delete(walletId) { return __awaiter(this, void 0, void 0, function* () { yield this.api.v1WalletsDelete({ walletId }); }); } get(walletId) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1WalletsRetrieve({ walletId }); }); } update(walletId, updateWalletRequest) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1WalletsUpdate({ walletId, updateWalletRequest }); }); } } class WebhookEndpoints { constructor(config) { this.api = new index_1.WebhookEndpointsApi(config); } list(options) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1WebhooksEndpointList(Object.assign({}, options)); }); } create(createEndpoint) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1WebhooksEndpointCreate({ createEndpoint }); }); } delete(endpointId) { return __awaiter(this, void 0, void 0, function* () { yield this.api.v1WebhooksEndpointDelete({ endpointId }); }); } get(endpointId) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1WebhooksEndpointRetrieve({ endpointId }); }); } update(endpointId, updateEndpoint) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1WebhooksEndpointUpdate({ endpointId, updateEndpoint }); }); } } class WebhookEvents { constructor(config) { this.api = new index_1.WebhookEventsApi(config); } list(options) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1WebhooksEventsList(Object.assign({}, options)); }); } } class GasStations { constructor(config) { this.api = new index_1.GasStationsApi(config); } list(options) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1GasStationsList(Object.assign({}, options)); }); } create(createGasStationRequest) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1GasStationsCreate({ createGasStationRequest }); }); } get(gasStationId) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1GasStationsRetrieve({ gasStationId }); }); } update(gasStationId, updateGasStationRequest) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1GasStationsUpdate({ gasStationId, updateGasStationRequest }); }); } delete(gasStationId) { return __awaiter(this, void 0, void 0, function* () { yield this.api.v1GasStationsDelete({ gasStationId }); }); } getOrCreateDepositAddress(getGasStationDepositAddressRequest) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1GasStationsGetOrCreateDepositAddress(getGasStationDepositAddressRequest); }); } } class WebhookClient { constructor(publicKey) { this.publicKey = publicKey; } verify(payload, signature) { return __awaiter(this, void 0, void 0, function* () { try { const payloadBuffer = Buffer.from(payload); const signatureBuffer = Buffer.from(signature, "base64"); const verify = crypto_1.createVerify("sha256"); verify.update(payloadBuffer); const isValid = verify.verify({ key: this.publicKey, padding: crypto_1.constants.RSA_PKCS1_PADDING, }, signatureBuffer); return isValid; } catch (error) { console.error("verify signature error:", error); return false; } }); } } exports.WebhookClient = WebhookClient; //# sourceMappingURL=index.js.map