@lukadriel/wave-sdk 0.0.0-stage → 0.1.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.
Files changed (185) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/LICENSE +201 -0
  3. package/NOTICE +2 -0
  4. package/README.md +523 -2
  5. package/dist/client/wave-client-options.d.ts +29 -0
  6. package/dist/client/wave-client-options.d.ts.map +1 -0
  7. package/dist/client/wave-client-options.js +58 -0
  8. package/dist/client/wave-client-options.js.map +1 -0
  9. package/dist/client/wave-client.d.ts +47 -0
  10. package/dist/client/wave-client.d.ts.map +1 -0
  11. package/dist/client/wave-client.js +66 -0
  12. package/dist/client/wave-client.js.map +1 -0
  13. package/dist/core/auth/authentication.d.ts +8 -0
  14. package/dist/core/auth/authentication.d.ts.map +1 -0
  15. package/dist/core/auth/authentication.js +10 -0
  16. package/dist/core/auth/authentication.js.map +1 -0
  17. package/dist/core/auth/request-signer.d.ts +26 -0
  18. package/dist/core/auth/request-signer.d.ts.map +1 -0
  19. package/dist/core/auth/request-signer.js +39 -0
  20. package/dist/core/auth/request-signer.js.map +1 -0
  21. package/dist/core/errors/error-parser.d.ts +25 -0
  22. package/dist/core/errors/error-parser.d.ts.map +1 -0
  23. package/dist/core/errors/error-parser.js +198 -0
  24. package/dist/core/errors/error-parser.js.map +1 -0
  25. package/dist/core/errors/redact.d.ts +11 -0
  26. package/dist/core/errors/redact.d.ts.map +1 -0
  27. package/dist/core/errors/redact.js +20 -0
  28. package/dist/core/errors/redact.js.map +1 -0
  29. package/dist/core/errors/wave-api-error.d.ts +116 -0
  30. package/dist/core/errors/wave-api-error.d.ts.map +1 -0
  31. package/dist/core/errors/wave-api-error.js +114 -0
  32. package/dist/core/errors/wave-api-error.js.map +1 -0
  33. package/dist/core/errors/wave-error.d.ts +27 -0
  34. package/dist/core/errors/wave-error.d.ts.map +1 -0
  35. package/dist/core/errors/wave-error.js +22 -0
  36. package/dist/core/errors/wave-error.js.map +1 -0
  37. package/dist/core/errors/wave-network-error.d.ts +24 -0
  38. package/dist/core/errors/wave-network-error.d.ts.map +1 -0
  39. package/dist/core/errors/wave-network-error.js +24 -0
  40. package/dist/core/errors/wave-network-error.js.map +1 -0
  41. package/dist/core/errors/wave-webhook-verification-error.d.ts +18 -0
  42. package/dist/core/errors/wave-webhook-verification-error.d.ts.map +1 -0
  43. package/dist/core/errors/wave-webhook-verification-error.js +17 -0
  44. package/dist/core/errors/wave-webhook-verification-error.js.map +1 -0
  45. package/dist/core/http/fetch-transport.d.ts +23 -0
  46. package/dist/core/http/fetch-transport.d.ts.map +1 -0
  47. package/dist/core/http/fetch-transport.js +34 -0
  48. package/dist/core/http/fetch-transport.js.map +1 -0
  49. package/dist/core/http/http-client.d.ts +57 -0
  50. package/dist/core/http/http-client.d.ts.map +1 -0
  51. package/dist/core/http/http-client.js +254 -0
  52. package/dist/core/http/http-client.js.map +1 -0
  53. package/dist/core/http/http-request.d.ts +35 -0
  54. package/dist/core/http/http-request.d.ts.map +1 -0
  55. package/dist/core/http/http-request.js +2 -0
  56. package/dist/core/http/http-request.js.map +1 -0
  57. package/dist/core/http/http-response.d.ts +26 -0
  58. package/dist/core/http/http-response.d.ts.map +1 -0
  59. package/dist/core/http/http-response.js +23 -0
  60. package/dist/core/http/http-response.js.map +1 -0
  61. package/dist/core/http/query.d.ts +14 -0
  62. package/dist/core/http/query.d.ts.map +1 -0
  63. package/dist/core/http/query.js +23 -0
  64. package/dist/core/http/query.js.map +1 -0
  65. package/dist/core/http/transport.d.ts +44 -0
  66. package/dist/core/http/transport.d.ts.map +1 -0
  67. package/dist/core/http/transport.js +2 -0
  68. package/dist/core/http/transport.js.map +1 -0
  69. package/dist/core/idempotency/idempotency.d.ts +16 -0
  70. package/dist/core/idempotency/idempotency.d.ts.map +1 -0
  71. package/dist/core/idempotency/idempotency.js +27 -0
  72. package/dist/core/idempotency/idempotency.js.map +1 -0
  73. package/dist/core/retry/backoff.d.ts +18 -0
  74. package/dist/core/retry/backoff.d.ts.map +1 -0
  75. package/dist/core/retry/backoff.js +46 -0
  76. package/dist/core/retry/backoff.js.map +1 -0
  77. package/dist/core/retry/retry-policy.d.ts +43 -0
  78. package/dist/core/retry/retry-policy.d.ts.map +1 -0
  79. package/dist/core/retry/retry-policy.js +90 -0
  80. package/dist/core/retry/retry-policy.js.map +1 -0
  81. package/dist/core/types/common.d.ts +59 -0
  82. package/dist/core/types/common.d.ts.map +1 -0
  83. package/dist/core/types/common.js +2 -0
  84. package/dist/core/types/common.js.map +1 -0
  85. package/dist/core/types/configuration.d.ts +82 -0
  86. package/dist/core/types/configuration.d.ts.map +1 -0
  87. package/dist/core/types/configuration.js +2 -0
  88. package/dist/core/types/configuration.js.map +1 -0
  89. package/dist/core/types/json.d.ts +15 -0
  90. package/dist/core/types/json.d.ts.map +1 -0
  91. package/dist/core/types/json.js +2 -0
  92. package/dist/core/types/json.js.map +1 -0
  93. package/dist/index.d.ts +39 -0
  94. package/dist/index.d.ts.map +1 -0
  95. package/dist/index.js +24 -0
  96. package/dist/index.js.map +1 -0
  97. package/dist/resources/aggregated-merchants/aggregated-merchant-types.d.ts +82 -0
  98. package/dist/resources/aggregated-merchants/aggregated-merchant-types.d.ts.map +1 -0
  99. package/dist/resources/aggregated-merchants/aggregated-merchant-types.js +2 -0
  100. package/dist/resources/aggregated-merchants/aggregated-merchant-types.js.map +1 -0
  101. package/dist/resources/aggregated-merchants/aggregated-merchants.d.ts +67 -0
  102. package/dist/resources/aggregated-merchants/aggregated-merchants.d.ts.map +1 -0
  103. package/dist/resources/aggregated-merchants/aggregated-merchants.js +111 -0
  104. package/dist/resources/aggregated-merchants/aggregated-merchants.js.map +1 -0
  105. package/dist/resources/balance/balance-types.d.ts +18 -0
  106. package/dist/resources/balance/balance-types.d.ts.map +1 -0
  107. package/dist/resources/balance/balance-types.js +2 -0
  108. package/dist/resources/balance/balance-types.js.map +1 -0
  109. package/dist/resources/balance/balance.d.ts +20 -0
  110. package/dist/resources/balance/balance.d.ts.map +1 -0
  111. package/dist/resources/balance/balance.js +28 -0
  112. package/dist/resources/balance/balance.js.map +1 -0
  113. package/dist/resources/checkout/checkout-types.d.ts +96 -0
  114. package/dist/resources/checkout/checkout-types.d.ts.map +1 -0
  115. package/dist/resources/checkout/checkout-types.js +2 -0
  116. package/dist/resources/checkout/checkout-types.js.map +1 -0
  117. package/dist/resources/checkout/checkout.d.ts +66 -0
  118. package/dist/resources/checkout/checkout.d.ts.map +1 -0
  119. package/dist/resources/checkout/checkout.js +95 -0
  120. package/dist/resources/checkout/checkout.js.map +1 -0
  121. package/dist/resources/payout-batches/payout-batches.d.ts +31 -0
  122. package/dist/resources/payout-batches/payout-batches.d.ts.map +1 -0
  123. package/dist/resources/payout-batches/payout-batches.js +36 -0
  124. package/dist/resources/payout-batches/payout-batches.js.map +1 -0
  125. package/dist/resources/payouts/payout-types.d.ts +158 -0
  126. package/dist/resources/payouts/payout-types.d.ts.map +1 -0
  127. package/dist/resources/payouts/payout-types.js +2 -0
  128. package/dist/resources/payouts/payout-types.js.map +1 -0
  129. package/dist/resources/payouts/payouts.d.ts +63 -0
  130. package/dist/resources/payouts/payouts.d.ts.map +1 -0
  131. package/dist/resources/payouts/payouts.js +80 -0
  132. package/dist/resources/payouts/payouts.js.map +1 -0
  133. package/dist/resources/reconciliation/reconciliation-types.d.ts +86 -0
  134. package/dist/resources/reconciliation/reconciliation-types.d.ts.map +1 -0
  135. package/dist/resources/reconciliation/reconciliation-types.js +2 -0
  136. package/dist/resources/reconciliation/reconciliation-types.js.map +1 -0
  137. package/dist/resources/reconciliation/reconciliation.d.ts +41 -0
  138. package/dist/resources/reconciliation/reconciliation.d.ts.map +1 -0
  139. package/dist/resources/reconciliation/reconciliation.js +81 -0
  140. package/dist/resources/reconciliation/reconciliation.js.map +1 -0
  141. package/dist/resources/webhooks/webhook-types.d.ts +159 -0
  142. package/dist/resources/webhooks/webhook-types.d.ts.map +1 -0
  143. package/dist/resources/webhooks/webhook-types.js +36 -0
  144. package/dist/resources/webhooks/webhook-types.js.map +1 -0
  145. package/dist/resources/webhooks/webhook-verifier.d.ts +104 -0
  146. package/dist/resources/webhooks/webhook-verifier.d.ts.map +1 -0
  147. package/dist/resources/webhooks/webhook-verifier.js +188 -0
  148. package/dist/resources/webhooks/webhook-verifier.js.map +1 -0
  149. package/package.json +70 -4
  150. package/src/client/wave-client-options.ts +91 -0
  151. package/src/client/wave-client.ts +70 -0
  152. package/src/core/auth/authentication.ts +9 -0
  153. package/src/core/auth/request-signer.ts +42 -0
  154. package/src/core/errors/error-parser.ts +258 -0
  155. package/src/core/errors/redact.ts +20 -0
  156. package/src/core/errors/wave-api-error.ts +148 -0
  157. package/src/core/errors/wave-error.ts +32 -0
  158. package/src/core/errors/wave-network-error.ts +24 -0
  159. package/src/core/errors/wave-webhook-verification-error.ts +28 -0
  160. package/src/core/http/fetch-transport.ts +48 -0
  161. package/src/core/http/http-client.ts +352 -0
  162. package/src/core/http/http-request.ts +37 -0
  163. package/src/core/http/http-response.ts +37 -0
  164. package/src/core/http/query.ts +31 -0
  165. package/src/core/http/transport.ts +45 -0
  166. package/src/core/idempotency/idempotency.ts +31 -0
  167. package/src/core/retry/backoff.ts +61 -0
  168. package/src/core/retry/retry-policy.ts +106 -0
  169. package/src/core/types/common.ts +65 -0
  170. package/src/core/types/configuration.ts +85 -0
  171. package/src/core/types/json.ts +16 -0
  172. package/src/index.ts +146 -0
  173. package/src/resources/aggregated-merchants/aggregated-merchant-types.ts +87 -0
  174. package/src/resources/aggregated-merchants/aggregated-merchants.ts +171 -0
  175. package/src/resources/balance/balance-types.ts +19 -0
  176. package/src/resources/balance/balance.ts +39 -0
  177. package/src/resources/checkout/checkout-types.ts +102 -0
  178. package/src/resources/checkout/checkout.ts +144 -0
  179. package/src/resources/payout-batches/payout-batches.ts +58 -0
  180. package/src/resources/payouts/payout-types.ts +171 -0
  181. package/src/resources/payouts/payouts.ts +120 -0
  182. package/src/resources/reconciliation/reconciliation-types.ts +102 -0
  183. package/src/resources/reconciliation/reconciliation.ts +116 -0
  184. package/src/resources/webhooks/webhook-types.ts +222 -0
  185. package/src/resources/webhooks/webhook-verifier.ts +297 -0
@@ -0,0 +1,159 @@
1
+ import type { Amount, CurrencyCode, PhoneNumber, Timestamp } from '../../core/types/common.js';
2
+ import type { JsonObject } from '../../core/types/json.js';
3
+ /**
4
+ * The event envelope Wave sends to a webhook endpoint.
5
+ *
6
+ * @typeParam TData - Event-specific data payload.
7
+ * @typeParam TType - Event type string.
8
+ */
9
+ export interface WebhookEvent<TData = unknown, TType extends string = string> {
10
+ /** Unique event identifier; use it to detect duplicate deliveries. */
11
+ readonly id: string;
12
+ /** Event type, for example `checkout.session.completed`. */
13
+ readonly type: TType;
14
+ /** Event-specific data containing the changed state. */
15
+ readonly data: TData;
16
+ }
17
+ /**
18
+ * Event types documented by Wave.
19
+ */
20
+ export type KnownWebhookEventType = 'checkout.session.completed' | 'checkout.session.payment_failed' | 'b2b.payment_received' | 'b2b.payment_failed' | 'merchant.payment_received' | 'remittance.delivered' | 'remittance.reversed' | 'test.test_event';
21
+ /**
22
+ * Data payload of `checkout.session.completed`.
23
+ */
24
+ export interface CheckoutSessionCompletedEventData {
25
+ /** Checkout session identifier. */
26
+ readonly id: string;
27
+ /** Amount collected. */
28
+ readonly amount: Amount;
29
+ /** Currency of the amount. */
30
+ readonly currency: CurrencyCode;
31
+ /** Payment state. */
32
+ readonly payment_status: 'succeeded';
33
+ /** Checkout state. */
34
+ readonly checkout_status: 'complete';
35
+ /** Caller correlation string, when provided. */
36
+ readonly client_reference?: string | null;
37
+ /** Wave transaction identifier. */
38
+ readonly transaction_id: string;
39
+ /** When the payment completed. */
40
+ readonly when_completed: Timestamp;
41
+ }
42
+ /**
43
+ * Data payload of `checkout.session.payment_failed`.
44
+ *
45
+ * Wave's example returns `payment_status: "failed"` and `checkout_status: "failed"`, which are not
46
+ * part of the documented Checkout Session enums; those literal values are preserved here.
47
+ */
48
+ export interface CheckoutSessionPaymentFailedEventData {
49
+ /** Checkout session identifier. */
50
+ readonly id: string;
51
+ /** Payment state as delivered by the event. */
52
+ readonly payment_status: 'failed';
53
+ /** Checkout state as delivered by the event. */
54
+ readonly checkout_status: 'failed';
55
+ /** Reason for the failed attempt. */
56
+ readonly last_payment_error: {
57
+ /** Short machine-readable error code. */
58
+ readonly code: string;
59
+ /** Human-readable description. */
60
+ readonly message: string;
61
+ };
62
+ }
63
+ /**
64
+ * Data payload of `b2b.payment_received`.
65
+ */
66
+ export interface B2BPaymentReceivedEventData {
67
+ /** B2B payment identifier. */
68
+ readonly id: string;
69
+ /** Amount received. */
70
+ readonly amount: Amount;
71
+ /** Currency of the amount. */
72
+ readonly currency: CurrencyCode;
73
+ /** Identifier of the sending business. */
74
+ readonly sender_id: string;
75
+ /** Caller correlation string, when provided. */
76
+ readonly client_reference?: string | null;
77
+ /** When the payment was created. */
78
+ readonly when_created: Timestamp;
79
+ }
80
+ /**
81
+ * Data payload of `merchant.payment_received`.
82
+ */
83
+ export interface MerchantPaymentReceivedEventData {
84
+ /** Transaction identifier. */
85
+ readonly id: string;
86
+ /** Amount received. */
87
+ readonly amount: Amount;
88
+ /** Fee charged for the payment. */
89
+ readonly fee: Amount;
90
+ /** Currency of the amount. */
91
+ readonly currency: CurrencyCode;
92
+ /** Mobile number of the payer. */
93
+ readonly sender_mobile: PhoneNumber;
94
+ /** Name of the receiving business. */
95
+ readonly merchant_name: string;
96
+ /** Custom key-value fields, when provided. */
97
+ readonly custom_fields?: JsonObject;
98
+ /** When the payment was created. */
99
+ readonly when_created: Timestamp;
100
+ }
101
+ /**
102
+ * Event payload for `checkout.session.completed`.
103
+ */
104
+ export type CheckoutSessionCompletedEvent = WebhookEvent<CheckoutSessionCompletedEventData, 'checkout.session.completed'>;
105
+ /**
106
+ * Event payload for `checkout.session.payment_failed`.
107
+ */
108
+ export type CheckoutSessionPaymentFailedEvent = WebhookEvent<CheckoutSessionPaymentFailedEventData, 'checkout.session.payment_failed'>;
109
+ /**
110
+ * Event payload for `b2b.payment_received`.
111
+ */
112
+ export type B2BPaymentReceivedEvent = WebhookEvent<B2BPaymentReceivedEventData, 'b2b.payment_received'>;
113
+ /**
114
+ * Event payload for `b2b.payment_failed`.
115
+ *
116
+ * Wave does not document the data schema for this event.
117
+ */
118
+ export type B2BPaymentFailedEvent = WebhookEvent<unknown, 'b2b.payment_failed'>;
119
+ /**
120
+ * Event payload for `merchant.payment_received`.
121
+ */
122
+ export type MerchantPaymentReceivedEvent = WebhookEvent<MerchantPaymentReceivedEventData, 'merchant.payment_received'>;
123
+ /**
124
+ * Event payload for `remittance.delivered`.
125
+ *
126
+ * Wave does not document the data schema for this event.
127
+ */
128
+ export type RemittanceDeliveredEvent = WebhookEvent<unknown, 'remittance.delivered'>;
129
+ /**
130
+ * Event payload for `remittance.reversed`.
131
+ *
132
+ * Wave does not document the data schema for this event.
133
+ */
134
+ export type RemittanceReversedEvent = WebhookEvent<unknown, 'remittance.reversed'>;
135
+ /**
136
+ * Event payload for the portal-triggered test event.
137
+ */
138
+ export type TestWebhookEvent = WebhookEvent<unknown, 'test.test_event'>;
139
+ /**
140
+ * Any documented Wave webhook event.
141
+ */
142
+ export type KnownWebhookEvent = CheckoutSessionCompletedEvent | CheckoutSessionPaymentFailedEvent | B2BPaymentReceivedEvent | B2BPaymentFailedEvent | MerchantPaymentReceivedEvent | RemittanceDeliveredEvent | RemittanceReversedEvent | TestWebhookEvent;
143
+ /**
144
+ * Narrows an arbitrary type string to a documented event type.
145
+ *
146
+ * @param type - Event type string from a webhook payload.
147
+ */
148
+ export declare function isKnownWebhookEventType(type: string): type is KnownWebhookEventType;
149
+ /**
150
+ * Narrows a parsed event to the documented discriminated union.
151
+ *
152
+ * Event payloads are not validated at runtime because Wave does not publish JSON schemas for them;
153
+ * the returned object keeps the parsed `data` value.
154
+ *
155
+ * @param event - Event returned by `constructEvent` or `constructWaveWebhookEvent`.
156
+ * @returns The typed event, or `undefined` for an undocumented event type.
157
+ */
158
+ export declare function toKnownWebhookEvent(event: WebhookEvent): KnownWebhookEvent | undefined;
159
+ //# sourceMappingURL=webhook-types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webhook-types.d.ts","sourceRoot":"","sources":["../../../src/resources/webhooks/webhook-types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAC/F,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAE3D;;;;;GAKG;AACH,MAAM,WAAW,YAAY,CAAC,KAAK,GAAG,OAAO,EAAE,KAAK,SAAS,MAAM,GAAG,MAAM;IAC1E,sEAAsE;IACtE,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,4DAA4D;IAC5D,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC;IACrB,wDAAwD;IACxD,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC;CACtB;AAED;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAC7B,4BAA4B,GAC5B,iCAAiC,GACjC,sBAAsB,GACtB,oBAAoB,GACpB,2BAA2B,GAC3B,sBAAsB,GACtB,qBAAqB,GACrB,iBAAiB,CAAC;AAEtB;;GAEG;AACH,MAAM,WAAW,iCAAiC;IAChD,mCAAmC;IACnC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,wBAAwB;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,8BAA8B;IAC9B,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;IAChC,qBAAqB;IACrB,QAAQ,CAAC,cAAc,EAAE,WAAW,CAAC;IACrC,sBAAsB;IACtB,QAAQ,CAAC,eAAe,EAAE,UAAU,CAAC;IACrC,gDAAgD;IAChD,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1C,mCAAmC;IACnC,QAAQ,CAAC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C,KAAK,EAAE,YAAY,GAAG,iBAAiB,GAAG,SAAS,CAKtF"}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Narrows an arbitrary type string to a documented event type.
3
+ *
4
+ * @param type - Event type string from a webhook payload.
5
+ */
6
+ export function isKnownWebhookEventType(type) {
7
+ switch (type) {
8
+ case 'checkout.session.completed':
9
+ case 'checkout.session.payment_failed':
10
+ case 'b2b.payment_received':
11
+ case 'b2b.payment_failed':
12
+ case 'merchant.payment_received':
13
+ case 'remittance.delivered':
14
+ case 'remittance.reversed':
15
+ case 'test.test_event':
16
+ return true;
17
+ default:
18
+ return false;
19
+ }
20
+ }
21
+ /**
22
+ * Narrows a parsed event to the documented discriminated union.
23
+ *
24
+ * Event payloads are not validated at runtime because Wave does not publish JSON schemas for them;
25
+ * the returned object keeps the parsed `data` value.
26
+ *
27
+ * @param event - Event returned by `constructEvent` or `constructWaveWebhookEvent`.
28
+ * @returns The typed event, or `undefined` for an undocumented event type.
29
+ */
30
+ export function toKnownWebhookEvent(event) {
31
+ if (!isKnownWebhookEventType(event.type)) {
32
+ return undefined;
33
+ }
34
+ return { id: event.id, type: event.type, data: event.data };
35
+ }
36
+ //# sourceMappingURL=webhook-types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webhook-types.js","sourceRoot":"","sources":["../../../src/resources/webhooks/webhook-types.ts"],"names":[],"mappings":"AA0LA;;;;GAIG;AACH,MAAM,UAAU,uBAAuB,CAAC,IAAY;IAClD,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,4BAA4B,CAAC;QAClC,KAAK,iCAAiC,CAAC;QACvC,KAAK,sBAAsB,CAAC;QAC5B,KAAK,oBAAoB,CAAC;QAC1B,KAAK,2BAA2B,CAAC;QACjC,KAAK,sBAAsB,CAAC;QAC5B,KAAK,qBAAqB,CAAC;QAC3B,KAAK,iBAAiB;YACpB,OAAO,IAAI,CAAC;QACd;YACE,OAAO,KAAK,CAAC;IACjB,CAAC;AACH,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,mBAAmB,CAAC,KAAmB;IACrD,IAAI,CAAC,uBAAuB,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;QACzC,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,EAAE,EAAE,EAAE,KAAK,CAAC,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAuB,CAAC;AACnF,CAAC","sourcesContent":["import type { Amount, CurrencyCode, PhoneNumber, Timestamp } from '../../core/types/common.js';\nimport type { JsonObject } from '../../core/types/json.js';\n\n/**\n * The event envelope Wave sends to a webhook endpoint.\n *\n * @typeParam TData - Event-specific data payload.\n * @typeParam TType - Event type string.\n */\nexport interface WebhookEvent<TData = unknown, TType extends string = string> {\n /** Unique event identifier; use it to detect duplicate deliveries. */\n readonly id: string;\n /** Event type, for example `checkout.session.completed`. */\n readonly type: TType;\n /** Event-specific data containing the changed state. */\n readonly data: TData;\n}\n\n/**\n * Event types documented by Wave.\n */\nexport type KnownWebhookEventType =\n | 'checkout.session.completed'\n | 'checkout.session.payment_failed'\n | 'b2b.payment_received'\n | 'b2b.payment_failed'\n | 'merchant.payment_received'\n | 'remittance.delivered'\n | 'remittance.reversed'\n | 'test.test_event';\n\n/**\n * Data payload of `checkout.session.completed`.\n */\nexport interface CheckoutSessionCompletedEventData {\n /** Checkout session identifier. */\n readonly id: string;\n /** Amount collected. */\n readonly amount: Amount;\n /** Currency of the amount. */\n readonly currency: CurrencyCode;\n /** Payment state. */\n readonly payment_status: 'succeeded';\n /** Checkout state. */\n readonly checkout_status: 'complete';\n /** Caller correlation string, when provided. */\n readonly client_reference?: string | null;\n /** Wave transaction identifier. */\n readonly transaction_id: string;\n /** When the payment completed. */\n readonly when_completed: Timestamp;\n}\n\n/**\n * Data payload of `checkout.session.payment_failed`.\n *\n * Wave's example returns `payment_status: \"failed\"` and `checkout_status: \"failed\"`, which are not\n * part of the documented Checkout Session enums; those literal values are preserved here.\n */\nexport interface CheckoutSessionPaymentFailedEventData {\n /** Checkout session identifier. */\n readonly id: string;\n /** Payment state as delivered by the event. */\n readonly payment_status: 'failed';\n /** Checkout state as delivered by the event. */\n readonly checkout_status: 'failed';\n /** Reason for the failed attempt. */\n readonly last_payment_error: {\n /** Short machine-readable error code. */\n readonly code: string;\n /** Human-readable description. */\n readonly message: string;\n };\n}\n\n/**\n * Data payload of `b2b.payment_received`.\n */\nexport interface B2BPaymentReceivedEventData {\n /** B2B payment identifier. */\n readonly id: string;\n /** Amount received. */\n readonly amount: Amount;\n /** Currency of the amount. */\n readonly currency: CurrencyCode;\n /** Identifier of the sending business. */\n readonly sender_id: string;\n /** Caller correlation string, when provided. */\n readonly client_reference?: string | null;\n /** When the payment was created. */\n readonly when_created: Timestamp;\n}\n\n/**\n * Data payload of `merchant.payment_received`.\n */\nexport interface MerchantPaymentReceivedEventData {\n /** Transaction identifier. */\n readonly id: string;\n /** Amount received. */\n readonly amount: Amount;\n /** Fee charged for the payment. */\n readonly fee: Amount;\n /** Currency of the amount. */\n readonly currency: CurrencyCode;\n /** Mobile number of the payer. */\n readonly sender_mobile: PhoneNumber;\n /** Name of the receiving business. */\n readonly merchant_name: string;\n /** Custom key-value fields, when provided. */\n readonly custom_fields?: JsonObject;\n /** When the payment was created. */\n readonly when_created: Timestamp;\n}\n\n/**\n * Event payload for `checkout.session.completed`.\n */\nexport type CheckoutSessionCompletedEvent = WebhookEvent<\n CheckoutSessionCompletedEventData,\n 'checkout.session.completed'\n>;\n\n/**\n * Event payload for `checkout.session.payment_failed`.\n */\nexport type CheckoutSessionPaymentFailedEvent = WebhookEvent<\n CheckoutSessionPaymentFailedEventData,\n 'checkout.session.payment_failed'\n>;\n\n/**\n * Event payload for `b2b.payment_received`.\n */\nexport type B2BPaymentReceivedEvent = WebhookEvent<\n B2BPaymentReceivedEventData,\n 'b2b.payment_received'\n>;\n\n/**\n * Event payload for `b2b.payment_failed`.\n *\n * Wave does not document the data schema for this event.\n */\nexport type B2BPaymentFailedEvent = WebhookEvent<unknown, 'b2b.payment_failed'>;\n\n/**\n * Event payload for `merchant.payment_received`.\n */\nexport type MerchantPaymentReceivedEvent = WebhookEvent<\n MerchantPaymentReceivedEventData,\n 'merchant.payment_received'\n>;\n\n/**\n * Event payload for `remittance.delivered`.\n *\n * Wave does not document the data schema for this event.\n */\nexport type RemittanceDeliveredEvent = WebhookEvent<unknown, 'remittance.delivered'>;\n\n/**\n * Event payload for `remittance.reversed`.\n *\n * Wave does not document the data schema for this event.\n */\nexport type RemittanceReversedEvent = WebhookEvent<unknown, 'remittance.reversed'>;\n\n/**\n * Event payload for the portal-triggered test event.\n */\nexport type TestWebhookEvent = WebhookEvent<unknown, 'test.test_event'>;\n\n/**\n * Any documented Wave webhook event.\n */\nexport type KnownWebhookEvent =\n | CheckoutSessionCompletedEvent\n | CheckoutSessionPaymentFailedEvent\n | B2BPaymentReceivedEvent\n | B2BPaymentFailedEvent\n | MerchantPaymentReceivedEvent\n | RemittanceDeliveredEvent\n | RemittanceReversedEvent\n | TestWebhookEvent;\n\n/**\n * Narrows an arbitrary type string to a documented event type.\n *\n * @param type - Event type string from a webhook payload.\n */\nexport function isKnownWebhookEventType(type: string): type is KnownWebhookEventType {\n switch (type) {\n case 'checkout.session.completed':\n case 'checkout.session.payment_failed':\n case 'b2b.payment_received':\n case 'b2b.payment_failed':\n case 'merchant.payment_received':\n case 'remittance.delivered':\n case 'remittance.reversed':\n case 'test.test_event':\n return true;\n default:\n return false;\n }\n}\n\n/**\n * Narrows a parsed event to the documented discriminated union.\n *\n * Event payloads are not validated at runtime because Wave does not publish JSON schemas for them;\n * the returned object keeps the parsed `data` value.\n *\n * @param event - Event returned by `constructEvent` or `constructWaveWebhookEvent`.\n * @returns The typed event, or `undefined` for an undocumented event type.\n */\nexport function toKnownWebhookEvent(event: WebhookEvent): KnownWebhookEvent | undefined {\n if (!isKnownWebhookEventType(event.type)) {\n return undefined;\n }\n return { id: event.id, type: event.type, data: event.data } as KnownWebhookEvent;\n}\n"]}
@@ -0,0 +1,104 @@
1
+ import type { WebhookEvent } from './webhook-types.js';
2
+ /** Default replay window for webhook timestamps, matching Wave's five-minute guidance. */
3
+ export declare const DEFAULT_WEBHOOK_TOLERANCE_SECONDS = 300;
4
+ /**
5
+ * Input for verifying a signed webhook request.
6
+ */
7
+ export interface VerifyWebhookSignatureInput {
8
+ /**
9
+ * Exact request body bytes as received.
10
+ *
11
+ * The signature is only valid for the original bytes; a parsed and re-serialized body will not
12
+ * verify.
13
+ */
14
+ readonly rawBody: string | Uint8Array;
15
+ /** Value of the `Wave-Signature` header. */
16
+ readonly signatureHeader: string | null | undefined;
17
+ /** Webhook signing secret provided by Wave when the endpoint was registered. */
18
+ readonly secret: string;
19
+ /** Allowed age of the timestamp in seconds; defaults to 300. */
20
+ readonly toleranceSeconds?: number;
21
+ /** Current Unix time in seconds; injectable for tests. */
22
+ readonly nowSeconds?: number;
23
+ }
24
+ /**
25
+ * Input for verifying the shared-secret authentication strategy.
26
+ */
27
+ export interface VerifySharedSecretInput {
28
+ /** Value of the `Authorization` header, expected to be `Bearer <secret>`. */
29
+ readonly authorizationHeader: string | null | undefined;
30
+ /** Webhook shared secret provided by Wave when the endpoint was registered. */
31
+ readonly secret: string;
32
+ }
33
+ /**
34
+ * Verifies incoming Wave webhook requests.
35
+ *
36
+ * This is local cryptography only; it never calls the Wave API. Use the signing-secret strategy for
37
+ * production, and always verify against the raw request body.
38
+ */
39
+ export declare class WaveWebhooks {
40
+ /**
41
+ * Verifies a `Wave-Signature` header.
42
+ *
43
+ * @param input - Raw body, header, secret and optional clock controls.
44
+ * @throws WaveWebhookVerificationError when the header is missing, malformed, stale, or does not
45
+ * match the raw body.
46
+ */
47
+ verifySignature(input: VerifyWebhookSignatureInput): void;
48
+ /**
49
+ * Verifies a webhook and parses its event envelope.
50
+ *
51
+ * @typeParam TData - Expected event data type; defaults to `unknown`.
52
+ * @param input - Raw body, header, secret and optional clock controls.
53
+ * @returns The parsed event envelope.
54
+ * @throws WaveWebhookVerificationError when verification or parsing fails.
55
+ */
56
+ constructEvent<TData = unknown>(input: VerifyWebhookSignatureInput): WebhookEvent<TData>;
57
+ /**
58
+ * Verifies the shared-secret authentication strategy.
59
+ *
60
+ * @param input - Authorization header and expected secret.
61
+ * @throws WaveWebhookVerificationError when the header is missing, malformed, or does not match.
62
+ */
63
+ verifySharedSecret(input: VerifySharedSecretInput): void;
64
+ }
65
+ /**
66
+ * Computes the documented webhook signature over `timestamp + rawBody`.
67
+ *
68
+ * @param secret - Webhook signing secret.
69
+ * @param timestampSeconds - Timestamp from the `Wave-Signature` header.
70
+ * @param rawBody - Raw request body bytes.
71
+ * @returns Lowercase hexadecimal HMAC-SHA256 digest.
72
+ */
73
+ export declare function computeWebhookSignature(secret: string, timestampSeconds: number, rawBody: string | Uint8Array): string;
74
+ /**
75
+ * Parses the documented `t={timestamp},v1={signature}` header.
76
+ *
77
+ * Multiple `v1` signatures are accepted to support Wave's secret-rotation flow.
78
+ *
79
+ * @param header - Header value.
80
+ */
81
+ export declare function parseWebhookSignatureHeader(header: string | null | undefined): {
82
+ readonly timestamp: number;
83
+ readonly signatures: readonly string[];
84
+ };
85
+ /**
86
+ * Verifies a webhook signature using the default verifier.
87
+ *
88
+ * @param input - Raw body, header, secret and optional clock controls.
89
+ */
90
+ export declare function verifyWaveWebhookSignature(input: VerifyWebhookSignatureInput): void;
91
+ /**
92
+ * Verifies and parses a webhook event using the default verifier.
93
+ *
94
+ * @typeParam TData - Expected event data type; defaults to `unknown`.
95
+ * @param input - Raw body, header, secret and optional clock controls.
96
+ */
97
+ export declare function constructWaveWebhookEvent<TData = unknown>(input: VerifyWebhookSignatureInput): WebhookEvent<TData>;
98
+ /**
99
+ * Verifies the shared-secret strategy using the default verifier.
100
+ *
101
+ * @param input - Authorization header and expected secret.
102
+ */
103
+ export declare function verifyWaveWebhookSharedSecret(input: VerifySharedSecretInput): void;
104
+ //# sourceMappingURL=webhook-verifier.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webhook-verifier.d.ts","sourceRoot":"","sources":["../../../src/resources/webhooks/webhook-verifier.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD,0FAA0F;AAC1F,eAAO,MAAM,iCAAiC,MAAM,CAAC;AAErD;;GAEG;AACH,MAAM,WAAW,2BAA2B;IAC1C;;;;;OAKG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,UAAU,CAAC;IACtC,4CAA4C;IAC5C,QAAQ,CAAC,eAAe,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;IACpD,gFAAgF;IAChF,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,gEAAgE;IAChE,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IACnC,0DAA0D;IAC1D,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;CAC9B;AAED;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC,6EAA6E;IAC7E,QAAQ,CAAC,mBAAmB,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;IACxD,+EAA+E;IAC/E,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAOD;;;;;GAKG;AACH,qBAAa,YAAY;IACvB;;;;;;OAMG;IACH,eAAe,CAAC,KAAK,EAAE,2BAA2B,GAAG,IAAI;IA8BzD;;;;;;;OAOG;IACH,cAAc,CAAC,KAAK,GAAG,OAAO,EAAE,KAAK,EAAE,2BAA2B,GAAG,YAAY,CAAC,KAAK,CAAC;IAkCxF;;;;;OAKG;IACH,kBAAkB,CAAC,KAAK,EAAE,uBAAuB,GAAG,IAAI;CA2BzD;AAED;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,MAAM,EACd,gBAAgB,EAAE,MAAM,EACxB,OAAO,EAAE,MAAM,GAAG,UAAU,GAC3B,MAAM,CAOR;AAED;;;;;;GAMG;AACH,wBAAgB,2BAA2B,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG;IAC9E,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,CAAC;CACxC,CAGA;AAED;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,2BAA2B,GAAG,IAAI,CAEnF;AAED;;;;;GAKG;AACH,wBAAgB,yBAAyB,CAAC,KAAK,GAAG,OAAO,EACvD,KAAK,EAAE,2BAA2B,GACjC,YAAY,CAAC,KAAK,CAAC,CAErB;AAED;;;;GAIG;AACH,wBAAgB,6BAA6B,CAAC,KAAK,EAAE,uBAAuB,GAAG,IAAI,CAElF"}
@@ -0,0 +1,188 @@
1
+ import { createHmac, timingSafeEqual } from 'node:crypto';
2
+ import { WaveConfigurationError } from '../../core/errors/wave-error.js';
3
+ import { WaveWebhookVerificationError } from '../../core/errors/wave-webhook-verification-error.js';
4
+ /** Default replay window for webhook timestamps, matching Wave's five-minute guidance. */
5
+ export const DEFAULT_WEBHOOK_TOLERANCE_SECONDS = 300;
6
+ /**
7
+ * Verifies incoming Wave webhook requests.
8
+ *
9
+ * This is local cryptography only; it never calls the Wave API. Use the signing-secret strategy for
10
+ * production, and always verify against the raw request body.
11
+ */
12
+ export class WaveWebhooks {
13
+ /**
14
+ * Verifies a `Wave-Signature` header.
15
+ *
16
+ * @param input - Raw body, header, secret and optional clock controls.
17
+ * @throws WaveWebhookVerificationError when the header is missing, malformed, stale, or does not
18
+ * match the raw body.
19
+ */
20
+ verifySignature(input) {
21
+ const secret = assertSecret(input.secret);
22
+ const toleranceSeconds = input.toleranceSeconds ?? DEFAULT_WEBHOOK_TOLERANCE_SECONDS;
23
+ if (!Number.isFinite(toleranceSeconds) || toleranceSeconds < 0) {
24
+ throw new WaveConfigurationError('toleranceSeconds must be a non-negative finite number.');
25
+ }
26
+ const { timestamp, signatures } = parseSignatureHeader(input.signatureHeader);
27
+ const nowSeconds = input.nowSeconds ?? Math.floor(Date.now() / 1000);
28
+ if (timestamp < nowSeconds - toleranceSeconds) {
29
+ throw new WaveWebhookVerificationError('timestamp-too-old', `Webhook timestamp is older than the ${toleranceSeconds}-second tolerance.`);
30
+ }
31
+ const expected = computeWebhookSignature(secret, timestamp, input.rawBody);
32
+ for (const signature of signatures) {
33
+ if (timingSafeEqualStrings(signature, expected)) {
34
+ return;
35
+ }
36
+ }
37
+ throw new WaveWebhookVerificationError('no-matching-signature', 'Webhook signature does not match the raw request body.');
38
+ }
39
+ /**
40
+ * Verifies a webhook and parses its event envelope.
41
+ *
42
+ * @typeParam TData - Expected event data type; defaults to `unknown`.
43
+ * @param input - Raw body, header, secret and optional clock controls.
44
+ * @returns The parsed event envelope.
45
+ * @throws WaveWebhookVerificationError when verification or parsing fails.
46
+ */
47
+ constructEvent(input) {
48
+ this.verifySignature(input);
49
+ const text = typeof input.rawBody === 'string'
50
+ ? input.rawBody
51
+ : Buffer.from(input.rawBody).toString('utf8');
52
+ let parsed;
53
+ try {
54
+ parsed = JSON.parse(text);
55
+ }
56
+ catch {
57
+ throw new WaveWebhookVerificationError('invalid-payload', 'Webhook body is not valid JSON.');
58
+ }
59
+ if (!isRecord(parsed)) {
60
+ throw new WaveWebhookVerificationError('invalid-payload', 'Webhook body must be a JSON object.');
61
+ }
62
+ const id = parsed['id'];
63
+ const type = parsed['type'];
64
+ if (typeof id !== 'string' || typeof type !== 'string' || !('data' in parsed)) {
65
+ throw new WaveWebhookVerificationError('invalid-payload', 'Webhook body must contain string "id", string "type" and a "data" field.');
66
+ }
67
+ return { id, type, data: parsed['data'] };
68
+ }
69
+ /**
70
+ * Verifies the shared-secret authentication strategy.
71
+ *
72
+ * @param input - Authorization header and expected secret.
73
+ * @throws WaveWebhookVerificationError when the header is missing, malformed, or does not match.
74
+ */
75
+ verifySharedSecret(input) {
76
+ const secret = assertSecret(input.secret);
77
+ const header = input.authorizationHeader;
78
+ if (header === null || header === undefined || header.trim().length === 0) {
79
+ throw new WaveWebhookVerificationError('missing-header', 'Missing Authorization header on webhook request.');
80
+ }
81
+ const match = /^Bearer\s+(.+)$/iu.exec(header.trim());
82
+ const token = match?.[1];
83
+ if (token === undefined) {
84
+ throw new WaveWebhookVerificationError('invalid-format', 'Webhook Authorization header must use the Bearer scheme.');
85
+ }
86
+ if (!timingSafeEqualStrings(token, secret)) {
87
+ throw new WaveWebhookVerificationError('no-matching-signature', 'Webhook shared secret does not match.');
88
+ }
89
+ }
90
+ }
91
+ /**
92
+ * Computes the documented webhook signature over `timestamp + rawBody`.
93
+ *
94
+ * @param secret - Webhook signing secret.
95
+ * @param timestampSeconds - Timestamp from the `Wave-Signature` header.
96
+ * @param rawBody - Raw request body bytes.
97
+ * @returns Lowercase hexadecimal HMAC-SHA256 digest.
98
+ */
99
+ export function computeWebhookSignature(secret, timestampSeconds, rawBody) {
100
+ const bodyBytes = typeof rawBody === 'string' ? Buffer.from(rawBody, 'utf8') : Buffer.from(rawBody);
101
+ return createHmac('sha256', secret)
102
+ .update(String(timestampSeconds), 'utf8')
103
+ .update(bodyBytes)
104
+ .digest('hex');
105
+ }
106
+ /**
107
+ * Parses the documented `t={timestamp},v1={signature}` header.
108
+ *
109
+ * Multiple `v1` signatures are accepted to support Wave's secret-rotation flow.
110
+ *
111
+ * @param header - Header value.
112
+ */
113
+ export function parseWebhookSignatureHeader(header) {
114
+ const parsed = parseSignatureHeader(header);
115
+ return { timestamp: parsed.timestamp, signatures: parsed.signatures };
116
+ }
117
+ /**
118
+ * Verifies a webhook signature using the default verifier.
119
+ *
120
+ * @param input - Raw body, header, secret and optional clock controls.
121
+ */
122
+ export function verifyWaveWebhookSignature(input) {
123
+ new WaveWebhooks().verifySignature(input);
124
+ }
125
+ /**
126
+ * Verifies and parses a webhook event using the default verifier.
127
+ *
128
+ * @typeParam TData - Expected event data type; defaults to `unknown`.
129
+ * @param input - Raw body, header, secret and optional clock controls.
130
+ */
131
+ export function constructWaveWebhookEvent(input) {
132
+ return new WaveWebhooks().constructEvent(input);
133
+ }
134
+ /**
135
+ * Verifies the shared-secret strategy using the default verifier.
136
+ *
137
+ * @param input - Authorization header and expected secret.
138
+ */
139
+ export function verifyWaveWebhookSharedSecret(input) {
140
+ new WaveWebhooks().verifySharedSecret(input);
141
+ }
142
+ function parseSignatureHeader(header) {
143
+ if (header === null || header === undefined || header.trim().length === 0) {
144
+ throw new WaveWebhookVerificationError('missing-header', 'Missing Wave-Signature header on webhook request.');
145
+ }
146
+ let timestamp;
147
+ const signatures = [];
148
+ for (const rawPart of header.split(',')) {
149
+ const part = rawPart.trim();
150
+ if (part.startsWith('t=')) {
151
+ const value = part.slice(2);
152
+ if (!/^\d+$/u.test(value)) {
153
+ throw new WaveWebhookVerificationError('invalid-format', 'Wave-Signature timestamp must be an integer number of seconds.');
154
+ }
155
+ timestamp = Number(value);
156
+ continue;
157
+ }
158
+ if (part.startsWith('v1=')) {
159
+ const value = part.slice(3);
160
+ if (!/^[0-9a-fA-F]{64}$/u.test(value)) {
161
+ throw new WaveWebhookVerificationError('invalid-format', 'Wave-Signature v1 value must be a 64-character hexadecimal HMAC-SHA256 digest.');
162
+ }
163
+ signatures.push(value.toLowerCase());
164
+ }
165
+ }
166
+ if (timestamp === undefined || signatures.length === 0) {
167
+ throw new WaveWebhookVerificationError('invalid-format', 'Wave-Signature header must contain a timestamp and at least one v1 signature.');
168
+ }
169
+ return { timestamp, signatures };
170
+ }
171
+ function timingSafeEqualStrings(left, right) {
172
+ const leftBytes = Buffer.from(left, 'utf8');
173
+ const rightBytes = Buffer.from(right, 'utf8');
174
+ if (leftBytes.length !== rightBytes.length) {
175
+ return false;
176
+ }
177
+ return timingSafeEqual(leftBytes, rightBytes);
178
+ }
179
+ function assertSecret(secret) {
180
+ if (typeof secret !== 'string' || secret.trim().length === 0) {
181
+ throw new WaveConfigurationError('Webhook secret must be a non-empty string.');
182
+ }
183
+ return secret;
184
+ }
185
+ function isRecord(value) {
186
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
187
+ }
188
+ //# sourceMappingURL=webhook-verifier.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webhook-verifier.js","sourceRoot":"","sources":["../../../src/resources/webhooks/webhook-verifier.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,sBAAsB,EAAE,MAAM,iCAAiC,CAAC;AACzE,OAAO,EAAE,4BAA4B,EAAE,MAAM,sDAAsD,CAAC;AAGpG,0FAA0F;AAC1F,MAAM,CAAC,MAAM,iCAAiC,GAAG,GAAG,CAAC;AAsCrD;;;;;GAKG;AACH,MAAM,OAAO,YAAY;IACvB;;;;;;OAMG;IACH,eAAe,CAAC,KAAkC;QAChD,MAAM,MAAM,GAAG,YAAY,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QAC1C,MAAM,gBAAgB,GAAG,KAAK,CAAC,gBAAgB,IAAI,iCAAiC,CAAC;QACrF,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,gBAAgB,CAAC,IAAI,gBAAgB,GAAG,CAAC,EAAE,CAAC;YAC/D,MAAM,IAAI,sBAAsB,CAAC,wDAAwD,CAAC,CAAC;QAC7F,CAAC;QAED,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,oBAAoB,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;QAC9E,MAAM,UAAU,GAAG,KAAK,CAAC,UAAU,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC;QAErE,IAAI,SAAS,GAAG,UAAU,GAAG,gBAAgB,EAAE,CAAC;YAC9C,MAAM,IAAI,4BAA4B,CACpC,mBAAmB,EACnB,uCAAuC,gBAAgB,oBAAoB,CAC5E,CAAC;QACJ,CAAC;QAED,MAAM,QAAQ,GAAG,uBAA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,SAAS,YAAY,CAAC,MAAc;IAClC,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC7D,MAAM,IAAI,sBAAsB,CAAC,4CAA4C,CAAC,CAAC;IACjF,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC","sourcesContent":["import { createHmac, timingSafeEqual } from 'node:crypto';\nimport { WaveConfigurationError } from '../../core/errors/wave-error.js';\nimport { WaveWebhookVerificationError } from '../../core/errors/wave-webhook-verification-error.js';\nimport type { WebhookEvent } from './webhook-types.js';\n\n/** Default replay window for webhook timestamps, matching Wave's five-minute guidance. */\nexport const DEFAULT_WEBHOOK_TOLERANCE_SECONDS = 300;\n\n/**\n * Input for verifying a signed webhook request.\n */\nexport interface VerifyWebhookSignatureInput {\n /**\n * Exact request body bytes as received.\n *\n * The signature is only valid for the original bytes; a parsed and re-serialized body will not\n * verify.\n */\n readonly rawBody: string | Uint8Array;\n /** Value of the `Wave-Signature` header. */\n readonly signatureHeader: string | null | undefined;\n /** Webhook signing secret provided by Wave when the endpoint was registered. */\n readonly secret: string;\n /** Allowed age of the timestamp in seconds; defaults to 300. */\n readonly toleranceSeconds?: number;\n /** Current Unix time in seconds; injectable for tests. */\n readonly nowSeconds?: number;\n}\n\n/**\n * Input for verifying the shared-secret authentication strategy.\n */\nexport interface VerifySharedSecretInput {\n /** Value of the `Authorization` header, expected to be `Bearer <secret>`. */\n readonly authorizationHeader: string | null | undefined;\n /** Webhook shared secret provided by Wave when the endpoint was registered. */\n readonly secret: string;\n}\n\ninterface ParsedSignatureHeader {\n readonly timestamp: number;\n readonly signatures: readonly string[];\n}\n\n/**\n * Verifies incoming Wave webhook requests.\n *\n * This is local cryptography only; it never calls the Wave API. Use the signing-secret strategy for\n * production, and always verify against the raw request body.\n */\nexport class WaveWebhooks {\n /**\n * Verifies a `Wave-Signature` header.\n *\n * @param input - Raw body, header, secret and optional clock controls.\n * @throws WaveWebhookVerificationError when the header is missing, malformed, stale, or does not\n * match the raw body.\n */\n verifySignature(input: VerifyWebhookSignatureInput): void {\n const secret = assertSecret(input.secret);\n const toleranceSeconds = input.toleranceSeconds ?? DEFAULT_WEBHOOK_TOLERANCE_SECONDS;\n if (!Number.isFinite(toleranceSeconds) || toleranceSeconds < 0) {\n throw new WaveConfigurationError('toleranceSeconds must be a non-negative finite number.');\n }\n\n const { timestamp, signatures } = parseSignatureHeader(input.signatureHeader);\n const nowSeconds = input.nowSeconds ?? Math.floor(Date.now() / 1000);\n\n if (timestamp < nowSeconds - toleranceSeconds) {\n throw new WaveWebhookVerificationError(\n 'timestamp-too-old',\n `Webhook timestamp is older than the ${toleranceSeconds}-second tolerance.`,\n );\n }\n\n const expected = computeWebhookSignature(secret, timestamp, input.rawBody);\n for (const signature of signatures) {\n if (timingSafeEqualStrings(signature, expected)) {\n return;\n }\n }\n\n throw new WaveWebhookVerificationError(\n 'no-matching-signature',\n 'Webhook signature does not match the raw request body.',\n );\n }\n\n /**\n * Verifies a webhook and parses its event envelope.\n *\n * @typeParam TData - Expected event data type; defaults to `unknown`.\n * @param input - Raw body, header, secret and optional clock controls.\n * @returns The parsed event envelope.\n * @throws WaveWebhookVerificationError when verification or parsing fails.\n */\n constructEvent<TData = unknown>(input: VerifyWebhookSignatureInput): WebhookEvent<TData> {\n this.verifySignature(input);\n\n const text =\n typeof input.rawBody === 'string'\n ? input.rawBody\n : Buffer.from(input.rawBody).toString('utf8');\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(text) as unknown;\n } catch {\n throw new WaveWebhookVerificationError('invalid-payload', 'Webhook body is not valid JSON.');\n }\n\n if (!isRecord(parsed)) {\n throw new WaveWebhookVerificationError(\n 'invalid-payload',\n 'Webhook body must be a JSON object.',\n );\n }\n\n const id = parsed['id'];\n const type = parsed['type'];\n if (typeof id !== 'string' || typeof type !== 'string' || !('data' in parsed)) {\n throw new WaveWebhookVerificationError(\n 'invalid-payload',\n 'Webhook body must contain string \"id\", string \"type\" and a \"data\" field.',\n );\n }\n\n return { id, type, data: parsed['data'] as TData };\n }\n\n /**\n * Verifies the shared-secret authentication strategy.\n *\n * @param input - Authorization header and expected secret.\n * @throws WaveWebhookVerificationError when the header is missing, malformed, or does not match.\n */\n verifySharedSecret(input: VerifySharedSecretInput): void {\n const secret = assertSecret(input.secret);\n const header = input.authorizationHeader;\n\n if (header === null || header === undefined || header.trim().length === 0) {\n throw new WaveWebhookVerificationError(\n 'missing-header',\n 'Missing Authorization header on webhook request.',\n );\n }\n\n const match = /^Bearer\\s+(.+)$/iu.exec(header.trim());\n const token = match?.[1];\n if (token === undefined) {\n throw new WaveWebhookVerificationError(\n 'invalid-format',\n 'Webhook Authorization header must use the Bearer scheme.',\n );\n }\n\n if (!timingSafeEqualStrings(token, secret)) {\n throw new WaveWebhookVerificationError(\n 'no-matching-signature',\n 'Webhook shared secret does not match.',\n );\n }\n }\n}\n\n/**\n * Computes the documented webhook signature over `timestamp + rawBody`.\n *\n * @param secret - Webhook signing secret.\n * @param timestampSeconds - Timestamp from the `Wave-Signature` header.\n * @param rawBody - Raw request body bytes.\n * @returns Lowercase hexadecimal HMAC-SHA256 digest.\n */\nexport function computeWebhookSignature(\n secret: string,\n timestampSeconds: number,\n rawBody: string | Uint8Array,\n): string {\n const bodyBytes =\n typeof rawBody === 'string' ? Buffer.from(rawBody, 'utf8') : Buffer.from(rawBody);\n return createHmac('sha256', secret)\n .update(String(timestampSeconds), 'utf8')\n .update(bodyBytes)\n .digest('hex');\n}\n\n/**\n * Parses the documented `t={timestamp},v1={signature}` header.\n *\n * Multiple `v1` signatures are accepted to support Wave's secret-rotation flow.\n *\n * @param header - Header value.\n */\nexport function parseWebhookSignatureHeader(header: string | null | undefined): {\n readonly timestamp: number;\n readonly signatures: readonly string[];\n} {\n const parsed = parseSignatureHeader(header);\n return { timestamp: parsed.timestamp, signatures: parsed.signatures };\n}\n\n/**\n * Verifies a webhook signature using the default verifier.\n *\n * @param input - Raw body, header, secret and optional clock controls.\n */\nexport function verifyWaveWebhookSignature(input: VerifyWebhookSignatureInput): void {\n new WaveWebhooks().verifySignature(input);\n}\n\n/**\n * Verifies and parses a webhook event using the default verifier.\n *\n * @typeParam TData - Expected event data type; defaults to `unknown`.\n * @param input - Raw body, header, secret and optional clock controls.\n */\nexport function constructWaveWebhookEvent<TData = unknown>(\n input: VerifyWebhookSignatureInput,\n): WebhookEvent<TData> {\n return new WaveWebhooks().constructEvent<TData>(input);\n}\n\n/**\n * Verifies the shared-secret strategy using the default verifier.\n *\n * @param input - Authorization header and expected secret.\n */\nexport function verifyWaveWebhookSharedSecret(input: VerifySharedSecretInput): void {\n new WaveWebhooks().verifySharedSecret(input);\n}\n\nfunction parseSignatureHeader(header: string | null | undefined): ParsedSignatureHeader {\n if (header === null || header === undefined || header.trim().length === 0) {\n throw new WaveWebhookVerificationError(\n 'missing-header',\n 'Missing Wave-Signature header on webhook request.',\n );\n }\n\n let timestamp: number | undefined;\n const signatures: string[] = [];\n\n for (const rawPart of header.split(',')) {\n const part = rawPart.trim();\n if (part.startsWith('t=')) {\n const value = part.slice(2);\n if (!/^\\d+$/u.test(value)) {\n throw new WaveWebhookVerificationError(\n 'invalid-format',\n 'Wave-Signature timestamp must be an integer number of seconds.',\n );\n }\n timestamp = Number(value);\n continue;\n }\n\n if (part.startsWith('v1=')) {\n const value = part.slice(3);\n if (!/^[0-9a-fA-F]{64}$/u.test(value)) {\n throw new WaveWebhookVerificationError(\n 'invalid-format',\n 'Wave-Signature v1 value must be a 64-character hexadecimal HMAC-SHA256 digest.',\n );\n }\n signatures.push(value.toLowerCase());\n }\n }\n\n if (timestamp === undefined || signatures.length === 0) {\n throw new WaveWebhookVerificationError(\n 'invalid-format',\n 'Wave-Signature header must contain a timestamp and at least one v1 signature.',\n );\n }\n\n return { timestamp, signatures };\n}\n\nfunction timingSafeEqualStrings(left: string, right: string): boolean {\n const leftBytes = Buffer.from(left, 'utf8');\n const rightBytes = Buffer.from(right, 'utf8');\n if (leftBytes.length !== rightBytes.length) {\n return false;\n }\n return timingSafeEqual(leftBytes, rightBytes);\n}\n\nfunction assertSecret(secret: string): string {\n if (typeof secret !== 'string' || secret.trim().length === 0) {\n throw new WaveConfigurationError('Webhook secret must be a non-empty string.');\n }\n return secret;\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,72 @@
1
1
  {
2
2
  "name": "@lukadriel/wave-sdk",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Unified, strongly typed TypeScript SDK for the Wave Business APIs",
5
+ "keywords": [
6
+ "wave",
7
+ "wave-business",
8
+ "payouts",
9
+ "checkout",
10
+ "mobile-money",
11
+ "sdk",
12
+ "typescript"
13
+ ],
14
+ "homepage": "https://github.com/lukadriel7/wave#readme",
15
+ "bugs": {
16
+ "url": "https://github.com/lukadriel7/wave/issues"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/lukadriel7/wave.git"
21
+ },
22
+ "license": "Apache-2.0",
23
+ "author": "Adriel",
24
+ "type": "module",
25
+ "main": "./dist/index.js",
26
+ "types": "./dist/index.d.ts",
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "default": "./dist/index.js"
31
+ },
32
+ "./package.json": "./package.json"
33
+ },
34
+ "files": [
35
+ "dist",
36
+ "src",
37
+ "README.md",
38
+ "CHANGELOG.md",
39
+ "LICENSE",
40
+ "NOTICE"
41
+ ],
42
+ "sideEffects": false,
43
+ "engines": {
44
+ "node": ">=22",
45
+ "bun": ">=1.4"
46
+ },
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "scripts": {
51
+ "build": "tsc -p tsconfig.build.json",
52
+ "typecheck": "tsc -p tsconfig.json --noEmit",
53
+ "test": "vitest run",
54
+ "test:watch": "vitest",
55
+ "test:coverage": "vitest run --coverage",
56
+ "smoke:bun": "bun run tests/bun/smoke.mjs",
57
+ "lint": "oxlint .",
58
+ "format": "oxfmt --write .",
59
+ "format:check": "oxfmt --check .",
60
+ "prepack": "npm run build",
61
+ "prepublishOnly": "npm run typecheck && npm run lint && npm test"
62
+ },
63
+ "devDependencies": {
64
+ "@types/node": "^22.0.0",
65
+ "@vitest/coverage-v8": "^3.0.0",
66
+ "oxfmt": "^0.80.0",
67
+ "oxlint": "^1.95.0",
68
+ "tsx": "^4.0.0",
69
+ "typescript": "^5.7.0",
70
+ "vitest": "^3.0.0"
71
+ }
72
+ }