UNPKG

@openweb3-io/wallet-pay

Version:

Wallet Pay API client and wallet pay verification library

229 lines 8.42 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", `walletpay-libs/${VERSION}/javascript`); return Promise.resolve(context); } post(context) { return Promise.resolve(context); } } function signEd25519(data, secret) { 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(secret, "hex")); const signedData = nacl.sign.detached(hashBuffer, keyPair.secretKey); const sign = Buffer.from(signedData).toString("hex"); return sign; } class SignatureMiddleware { constructor(secret) { this.secret = secret; } 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.secret); context.setHeaderParam("x-signature", sign); return Promise.resolve(context); } post(context) { return Promise.resolve(context); } } class ApiClient { constructor(apiKey, secret, options) { var _a; const baseUrl = (_a = options === null || options === void 0 ? void 0 : options.serverUrl) !== null && _a !== void 0 ? _a : "https://api.wallet-pay.openweb3.io"; const baseServer = new index_1.ServerConfiguration(baseUrl, {}); const config = index_1.createConfiguration({ baseServer, promiseMiddleware: [new UserAgentMiddleware(), new SignatureMiddleware(secret)], authMethods: { ApiKeyAuth: apiKey, }, }); this._configuration = config; this.orders = new Orders(config); this.endpoints = new Endpoints(config); this.currencies = new Currencies(config); this.transfers = new Transfers(config); this.rates = new Rates(config); this.refunds = new Refunds(config); } } exports.ApiClient = ApiClient; class Orders { constructor(config) { this.api = new index_1.OrdersApi(config); } list(options) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1OrdersList(Object.assign({}, options)); }); } retrieve(idOrUid) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1OrdersRetrieve({ idOrUid }); }); } create(createOrderRequest, options) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1OrdersCreate(Object.assign({ createOrderRequest }, options)); }); } } class Currencies { constructor(config) { this.api = new index_1.CurrenciesApi(config); } list(options) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1CurrenciesList(Object.assign({}, options)); }); } findByCode(code) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1CurrenciesRetrieve({ code }); }); } } class Rates { constructor(config) { this.api = new index_1.RatesApi(config); } estimate(options) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1RatesEstimate(Object.assign({}, options)); }); } getRates(getRatesRequest) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1RatesList({ getRatesRequest }); }); } } class Transfers { constructor(config) { this.api = new index_1.TransfersApi(config); } transfer(transferRequest, options) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1TransfersTransfer(Object.assign({ transferRequest }, options)); }); } } class Endpoints { constructor(config) { this.api = new index_1.WebhookEndpointsApi(config); } create(createEndpointRequest, options) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1WebhookEndpointsCreate(Object.assign({ createEndpointRequest }, options)); }); } delete(endpointId) { return __awaiter(this, void 0, void 0, function* () { yield this.api.v1WebhookEndpointsDelete({ endpointId }); }); } retrieve(endpointId) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1WebhookEndpointsRetrieve({ endpointId }); }); } list(options) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1WebhookEndpointsList(Object.assign({}, options)); }); } } class Refunds { constructor(config) { this.api = new index_1.RefundsApi(config); } list(options) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1RefundsList(Object.assign({}, options)); }); } create(createRefundRequest, options) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1RefundsCreate(Object.assign({ createRefundRequest }, options)); }); } retrieve(idOrUid) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.v1RefundsRetrieve({ idOrUid }); }); } } 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