@waffo/waffo-node 2.5.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -114,18 +114,18 @@ var WaffoUnknownStatusError = class _WaffoUnknownStatusError extends Error {
114
114
  // src/utils/rsa-utils.ts
115
115
  var ALGORITHM = "RSA-SHA256";
116
116
  var KEY_SIZE = 2048;
117
- function sign(data, base64PrivateKey) {
117
+ var PKCS8_PEM_HEADER = "-----BEGIN PRIVATE KEY-----";
118
+ var PEM_HEADER_PREFIX = "-----BEGIN ";
119
+ var INVALID_PRIVATE_KEY_FORMAT_MESSAGE = "Invalid private key format. Please provide an unencrypted PKCS#8 private key.";
120
+ function sign(data, privateKeyInput) {
118
121
  try {
119
- const privateKeyBuffer = Buffer.from(base64PrivateKey, "base64");
122
+ const privateKey = parsePrivateKey(privateKeyInput);
120
123
  const signer = crypto__namespace.createSign(ALGORITHM);
121
124
  signer.update(data, "utf8");
122
- const signature = signer.sign({
123
- key: privateKeyBuffer,
124
- format: "der",
125
- type: "pkcs8"
126
- });
125
+ const signature = signer.sign(privateKey);
127
126
  return signature.toString("base64");
128
127
  } catch (error) {
128
+ if (error instanceof WaffoError) throw error;
129
129
  throw new WaffoError(
130
130
  WaffoErrorCode.SIGN_FAILED,
131
131
  `Failed to sign data: ${error instanceof Error ? error.message : String(error)}`,
@@ -159,29 +159,43 @@ function validatePrivateKey(base64PrivateKey) {
159
159
  );
160
160
  }
161
161
  try {
162
- const keyBuffer = Buffer.from(base64PrivateKey, "base64");
163
- crypto__namespace.createPrivateKey({
164
- key: keyBuffer,
165
- format: "der",
166
- type: "pkcs8"
167
- });
162
+ parsePrivateKey(base64PrivateKey);
168
163
  } catch (error) {
169
164
  if (error instanceof WaffoError) throw error;
170
- const message = error instanceof Error ? error.message : String(error);
171
- if (message.includes("bad base64")) {
172
- throw new WaffoError(
173
- WaffoErrorCode.INVALID_PRIVATE_KEY,
174
- "Invalid private key: not valid Base64 encoding",
175
- error instanceof Error ? error : void 0
176
- );
165
+ throw new WaffoError(
166
+ WaffoErrorCode.INVALID_PRIVATE_KEY,
167
+ INVALID_PRIVATE_KEY_FORMAT_MESSAGE,
168
+ error instanceof Error ? error : void 0
169
+ );
170
+ }
171
+ }
172
+ function parsePrivateKey(privateKeyInput) {
173
+ try {
174
+ const trimmed = privateKeyInput.trim();
175
+ const key = trimmed.startsWith(PKCS8_PEM_HEADER) ? crypto__namespace.createPrivateKey(trimmed) : parseDerPrivateKey(trimmed);
176
+ if (key.asymmetricKeyType !== "rsa") {
177
+ throw new Error("private key is not RSA");
177
178
  }
179
+ return key;
180
+ } catch (error) {
181
+ if (error instanceof WaffoError) throw error;
178
182
  throw new WaffoError(
179
183
  WaffoErrorCode.INVALID_PRIVATE_KEY,
180
- `Invalid private key: ${message}`,
184
+ INVALID_PRIVATE_KEY_FORMAT_MESSAGE,
181
185
  error instanceof Error ? error : void 0
182
186
  );
183
187
  }
184
188
  }
189
+ function parseDerPrivateKey(trimmedPrivateKey) {
190
+ if (trimmedPrivateKey.startsWith(PEM_HEADER_PREFIX)) {
191
+ throw new Error("unsupported PEM private key");
192
+ }
193
+ return crypto__namespace.createPrivateKey({
194
+ key: Buffer.from(trimmedPrivateKey, "base64"),
195
+ format: "der",
196
+ type: "pkcs8"
197
+ });
198
+ }
185
199
  function validatePublicKey(base64PublicKey) {
186
200
  if (!base64PublicKey || base64PublicKey.trim() === "") {
187
201
  throw new WaffoError(
@@ -463,7 +477,7 @@ var HEADER_SDK_VERSION = "x-sdk-version";
463
477
  var HEADER_CONTENT_TYPE = "content-type";
464
478
  var CONTENT_TYPE_JSON = "application/json";
465
479
  var API_VERSION = "1.0.0";
466
- var SDK_VERSION = "waffo-node/2.5.0";
480
+ var SDK_VERSION = "waffo-node/2.6.0";
467
481
  var WaffoHttpClient = class {
468
482
  baseUrl;
469
483
  apiKey;
@@ -475,6 +489,8 @@ var WaffoHttpClient = class {
475
489
  logger;
476
490
  httpTransport;
477
491
  constructor(config) {
492
+ RsaUtils.validatePrivateKey(config.privateKey);
493
+ RsaUtils.validatePublicKey(config.waffoPublicKey);
478
494
  this.baseUrl = EnvironmentBaseUrl[config.environment];
479
495
  this.apiKey = config.apiKey;
480
496
  this.privateKey = config.privateKey;
@@ -613,7 +629,7 @@ var WaffoHttpClient = class {
613
629
  isNetworkError(error) {
614
630
  if (!(error instanceof Error)) return false;
615
631
  const message = error.message.toLowerCase();
616
- return error.name === "AbortError" || message.includes("timeout") || message.includes("econnrefused") || message.includes("enotfound") || message.includes("network") || message.includes("socket") || message.includes("connection");
632
+ return error.name === "AbortError" || message.includes("timeout") || message.includes("econnrefused") || message.includes("enotfound") || message.includes("fetch failed") || message.includes("network") || message.includes("socket") || message.includes("connection");
617
633
  }
618
634
  };
619
635
 
@@ -1156,6 +1172,26 @@ var PayMethodConfigResource = class _PayMethodConfigResource extends BaseResourc
1156
1172
  }
1157
1173
  };
1158
1174
 
1175
+ // src/resources/wallet-resource.ts
1176
+ var WalletResource = class _WalletResource extends BaseResource {
1177
+ static PATH_INQUIRY = "/inquiry";
1178
+ /**
1179
+ * Inquires the stablecoin receiving address for an x402 payment.
1180
+ *
1181
+ * @param params - Wallet inquiry parameters
1182
+ * @param options - Optional request options
1183
+ * @returns API response with wallet data
1184
+ * @throws WaffoUnknownStatusError for network errors
1185
+ */
1186
+ async inquiry(params, options) {
1187
+ return this.httpClient.post(
1188
+ this.getPath(_WalletResource.PATH_INQUIRY),
1189
+ params,
1190
+ options
1191
+ );
1192
+ }
1193
+ };
1194
+
1159
1195
  // src/waffo.ts
1160
1196
  var Waffo = class _Waffo {
1161
1197
  config;
@@ -1166,14 +1202,15 @@ var Waffo = class _Waffo {
1166
1202
  _refund;
1167
1203
  _merchantConfig;
1168
1204
  _payMethodConfig;
1205
+ _wallet;
1169
1206
  /**
1170
1207
  * Creates a new Waffo SDK instance.
1171
1208
  *
1172
1209
  * @param config - SDK configuration
1173
1210
  */
1174
1211
  constructor(config) {
1175
- this.config = config;
1176
- this.httpClient = new WaffoHttpClient(config);
1212
+ this.config = createWaffoConfig(config);
1213
+ this.httpClient = new WaffoHttpClient(this.config);
1177
1214
  }
1178
1215
  /**
1179
1216
  * Creates a Waffo SDK instance from environment variables.
@@ -1249,6 +1286,17 @@ var Waffo = class _Waffo {
1249
1286
  }
1250
1287
  return this._payMethodConfig;
1251
1288
  }
1289
+ /**
1290
+ * Returns the wallet resource for x402 stablecoin receiving-address inquiry.
1291
+ *
1292
+ * @returns Wallet resource
1293
+ */
1294
+ wallet() {
1295
+ if (!this._wallet) {
1296
+ this._wallet = new WalletResource(this.httpClient, "/wallet");
1297
+ }
1298
+ return this._wallet;
1299
+ }
1252
1300
  /**
1253
1301
  * Creates a webhook handler for processing Waffo webhook notifications.
1254
1302
  *
@@ -1307,6 +1355,7 @@ exports.WaffoError = WaffoError;
1307
1355
  exports.WaffoErrorCode = WaffoErrorCode;
1308
1356
  exports.WaffoHttpClient = WaffoHttpClient;
1309
1357
  exports.WaffoUnknownStatusError = WaffoUnknownStatusError;
1358
+ exports.WalletResource = WalletResource;
1310
1359
  exports.WebhookEventType = WebhookEventType;
1311
1360
  exports.WebhookHandler = WebhookHandler;
1312
1361
  //# sourceMappingURL=index.js.map