@vaam-apps/vpay-sdk 0.15.0 → 0.17.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.
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env bash
2
+ # Builds the sealing module the Node SDK loads: `sdks/rust/wasm`, compiled to
3
+ # WebAssembly and wrapped for Node by `wasm-bindgen --target nodejs`.
4
+ #
5
+ # scripts/build-wasm.sh -> sdks/nodejs/wasm (ships in the npm package)
6
+ # scripts/build-wasm.sh --responder -> sdks/nodejs/wasm-test (the server half, for vitest only)
7
+ #
8
+ # Both are build outputs and gitignored. `wasm-bindgen`'s command-line tool must
9
+ # be exactly the version of the `wasm-bindgen` crate in Cargo.lock: the two
10
+ # agree on a private interface, and a mismatch fails at load time with an error
11
+ # that names neither (`just wasm-build` installs nothing; CI installs the
12
+ # version this script prints when it disagrees).
13
+ set -euo pipefail
14
+
15
+ here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
16
+ root="$(cd "$here/../../.." && pwd)"
17
+ cd "$root"
18
+
19
+ features=()
20
+ out="$root/sdks/nodejs/wasm"
21
+ if [ "${1:-}" = "--responder" ]; then
22
+ features=(--features responder)
23
+ out="$root/sdks/nodejs/wasm-test"
24
+ fi
25
+
26
+ wanted="$(awk '/^name = "wasm-bindgen"$/ {getline; gsub(/version = |"/, ""); print; exit}' Cargo.lock)"
27
+ if ! command -v wasm-bindgen >/dev/null 2>&1; then
28
+ echo "build-wasm: FAIL — wasm-bindgen is not on PATH." >&2
29
+ echo "build-wasm: install the version Cargo.lock pins: cargo install wasm-bindgen-cli --locked --version $wanted" >&2
30
+ exit 1
31
+ fi
32
+ found="$(wasm-bindgen --version | awk '{print $2}')"
33
+ if [ "$found" != "$wanted" ]; then
34
+ echo "build-wasm: FAIL — wasm-bindgen $found is installed, Cargo.lock pins the crate at $wanted." >&2
35
+ echo "build-wasm: cargo install wasm-bindgen-cli --locked --version $wanted --force" >&2
36
+ exit 1
37
+ fi
38
+ rustup target add wasm32-unknown-unknown >/dev/null 2>&1 || true
39
+
40
+ # Size over speed: this is a signing call, not a hot loop, and the module is
41
+ # embedded in the glue and parsed on every `require`.
42
+ CARGO_PROFILE_RELEASE_OPT_LEVEL=s \
43
+ cargo build --release --locked --target wasm32-unknown-unknown -p vpay-sdk-wasm "${features[@]}"
44
+
45
+ rm -rf "$out"
46
+ mkdir -p "$out"
47
+ wasm-bindgen --target nodejs --out-dir "$out" --out-name vpay_sdk_wasm \
48
+ "${CARGO_TARGET_DIR:-$root/target}/wasm32-unknown-unknown/release/vpay_sdk_wasm.wasm"
49
+ # wasm-bindgen's Node glue reads its `.wasm` from `${__dirname}` at load time.
50
+ # A bundler that inlines the glue (Next's Turbopack does, for a workspace
51
+ # package) rewrites `__dirname` to a build-time path that does not exist where
52
+ # the bundle runs, and every call to vpay then fails with ENOENT. So the module
53
+ # carries its own bytes: the read becomes a base64 literal, and the glue needs
54
+ # no file, no path and no `fs` wherever it ends up. Each of the two lines must
55
+ # be found exactly once, so a wasm-bindgen that words them differently fails
56
+ # this build rather than shipping a module that still reads from disk.
57
+ glue="$out/vpay_sdk_wasm.js"
58
+ b64="$out/.wasm.b64"
59
+ base64 <"$out/vpay_sdk_wasm_bg.wasm" | tr -d '\n' >"$b64"
60
+ awk -v b64file="$b64" '
61
+ $0 == "const wasmPath = `${__dirname}/vpay_sdk_wasm_bg.wasm`;" { path++; next }
62
+ $0 == "const wasmBytes = require('"'"'fs'"'"').readFileSync(wasmPath);" {
63
+ bytes++
64
+ getline literal <b64file
65
+ print "const wasmBytes = Buffer.from(\"" literal "\", \"base64\");"
66
+ next
67
+ }
68
+ { print }
69
+ END { if (path != 1 || bytes != 1) exit 1 }
70
+ ' "$glue" >"$glue.embedded" || {
71
+ echo "build-wasm: FAIL — wasm-bindgen's glue no longer reads its module the way this script rewrites." >&2
72
+ exit 1
73
+ }
74
+ mv "$glue.embedded" "$glue"
75
+ rm -f "$b64"
76
+ # The SDK is ESM ("type": "module"); wasm-bindgen emits CommonJS. A nested
77
+ # package.json is what tells Node which one this directory is.
78
+ printf '{ "type": "commonjs" }\n' >"$out/package.json"
79
+ echo "build-wasm: wrote $out"
@@ -0,0 +1,113 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * One signed `/v1` call from a shell: what `curl` cannot do, because the
4
+ * request is a COSE_Sign1 message and not a header on an ordinary one.
5
+ *
6
+ * node scripts/vpay-call.mjs GET /payment_intents --query 'limit=3'
7
+ * node scripts/vpay-call.mjs POST /payment_intents \
8
+ * --data 'amount=5000&currency=xaf&payment_method_types[0]=mtn_momo' \
9
+ * --key order-42
10
+ * node scripts/vpay-call.mjs POST /payment_intents/pi_1/confirm --data '…'
11
+ *
12
+ * The path is **under `/v1`**. The answer's JSON is printed to stdout, verified
13
+ * (a body that did not verify is an error, never printed); the HTTP status goes
14
+ * to stderr as `status: <n>`; the exit code is 0 for a 2xx and 1 otherwise.
15
+ * `--data` is the form body exactly as `curl -d` would send it, brackets bare;
16
+ * a write without `--key` gets a fresh UUID, printed to stderr as
17
+ * `idempotency-key: <uuid>` so a retry can reuse it.
18
+ *
19
+ * Reads from the environment (the names `sdks/rust/tests/live_*.rs` and the
20
+ * Node live suite read, so one `just sdk-live` environment serves all three):
21
+ * VPAY_BASE_URL required — e.g. http://localhost:18080
22
+ * VPAY_MERCHANT_SIGNING_SEED_PATH required — the merchant's Ed25519 seed file
23
+ * VPAY_SERVER_KEY_CURRENT_PATH required — vpay's current response key (public)
24
+ * VPAY_SERVER_KEY_NEXT_PATH optional — the next one
25
+ * VPAY_AUDIENCE optional — vpay's public audience, if it differs from the base URL
26
+ *
27
+ * Run after `pnpm --filter @vaam-apps/vpay-sdk build` (imports from ../dist).
28
+ */
29
+ import { randomUUID } from "node:crypto";
30
+ import { readFileSync } from "node:fs";
31
+ import { parseArgs } from "node:util";
32
+ import { performSealedCall } from "../dist/sealed-call.js";
33
+ import { resolveMerchantAuth } from "../dist/sealer.js";
34
+
35
+ function fail(message) {
36
+ console.error(`vpay-call: ${message}`);
37
+ process.exit(2);
38
+ }
39
+
40
+ function requireEnv(name) {
41
+ const value = process.env[name];
42
+ if (!value) fail(`missing required environment variable: ${name}`);
43
+ return value;
44
+ }
45
+
46
+ function readKey(path, what) {
47
+ try {
48
+ return readFileSync(path, "utf8").trim();
49
+ } catch (err) {
50
+ return fail(`cannot read ${what} at ${path}: ${err.message}`);
51
+ }
52
+ }
53
+
54
+ const { values, positionals } = parseArgs({
55
+ allowPositionals: true,
56
+ options: {
57
+ data: { type: "string", short: "d" },
58
+ query: { type: "string", short: "q" },
59
+ key: { type: "string", short: "k" },
60
+ },
61
+ });
62
+ const [method, path] = positionals;
63
+ if (!method || !path || positionals.length > 2) {
64
+ fail(
65
+ "usage: vpay-call.mjs <GET|POST|DELETE> </path-under-/v1> [--data form] [--query q] [--key idempotency-key]",
66
+ );
67
+ }
68
+ const verb = method.toUpperCase();
69
+ if (!["GET", "POST", "DELETE"].includes(verb))
70
+ fail(`no ${verb} operation under /v1`);
71
+ if (!path.startsWith("/"))
72
+ fail("the path is under /v1 and starts with /, e.g. /payment_intents");
73
+
74
+ const nextPath = process.env.VPAY_SERVER_KEY_NEXT_PATH;
75
+ const auth = resolveMerchantAuth({
76
+ baseUrl: requireEnv("VPAY_BASE_URL"),
77
+ clientId: process.env.VPAY_MERCHANT_CLIENT_ID || "vpay-call",
78
+ privateKey: readKey(
79
+ requireEnv("VPAY_MERCHANT_SIGNING_SEED_PATH"),
80
+ "the merchant signing seed",
81
+ ),
82
+ serverKeys: {
83
+ current: readKey(
84
+ requireEnv("VPAY_SERVER_KEY_CURRENT_PATH"),
85
+ "vpay's current response key",
86
+ ),
87
+ ...(nextPath
88
+ ? { next: readKey(nextPath, "vpay's next response key") }
89
+ : {}),
90
+ },
91
+ ...(process.env.VPAY_AUDIENCE ? { audience: process.env.VPAY_AUDIENCE } : {}),
92
+ });
93
+
94
+ const write = verb !== "GET";
95
+ const idempotencyKey = write ? (values.key ?? randomUUID()) : undefined;
96
+ if (write && values.key === undefined)
97
+ console.error(`idempotency-key: ${idempotencyKey}`);
98
+
99
+ try {
100
+ const { status, text } = await performSealedCall(auth, {
101
+ method: verb,
102
+ path,
103
+ body: verb === "POST" ? (values.data ?? "") : undefined,
104
+ query: verb === "GET" ? values.query : undefined,
105
+ idempotencyKey,
106
+ });
107
+ console.error(`status: ${status}`);
108
+ console.log(text);
109
+ process.exit(status >= 200 && status < 300 ? 0 : 1);
110
+ } catch (err) {
111
+ console.error(`vpay-call: ${err.name}: ${err.message}`);
112
+ process.exit(1);
113
+ }
@@ -0,0 +1 @@
1
+ { "type": "commonjs" }
@@ -0,0 +1,94 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ /**
4
+ * The `ReadableStreamType` enum.
5
+ *
6
+ * *This API requires the following crate features to be activated: `ReadableStreamType`*
7
+ */
8
+
9
+ export type ReadableStreamType = "bytes";
10
+
11
+ export class IntoUnderlyingByteSource {
12
+ private constructor();
13
+ free(): void;
14
+ [Symbol.dispose](): void;
15
+ cancel(): void;
16
+ pull(controller: ReadableByteStreamController): Promise<any>;
17
+ start(controller: ReadableByteStreamController): void;
18
+ readonly autoAllocateChunkSize: number;
19
+ readonly type: ReadableStreamType;
20
+ }
21
+
22
+ export class IntoUnderlyingSink {
23
+ private constructor();
24
+ free(): void;
25
+ [Symbol.dispose](): void;
26
+ abort(reason: any): Promise<any>;
27
+ close(): Promise<any>;
28
+ write(chunk: any): Promise<any>;
29
+ }
30
+
31
+ export class IntoUnderlyingSource {
32
+ private constructor();
33
+ free(): void;
34
+ [Symbol.dispose](): void;
35
+ cancel(): void;
36
+ pull(controller: ReadableStreamDefaultController): Promise<any>;
37
+ }
38
+
39
+ /**
40
+ * One sealed call: send `body` with `headers`, then `openResponse` the answer.
41
+ */
42
+ export class SealedCall {
43
+ private constructor();
44
+ free(): void;
45
+ [Symbol.dispose](): void;
46
+ /**
47
+ * Verifies the answer and returns its payload as text. `headers` is the
48
+ * response's header list as `[name, value]` pairs
49
+ * (`[...response.headers]`).
50
+ */
51
+ openResponse(status: number, headers: any, body: Uint8Array): Promise<string>;
52
+ /**
53
+ * The request body: the COSE message.
54
+ */
55
+ readonly body: Uint8Array;
56
+ /**
57
+ * The headers to send as given, as `[name, value]` pairs.
58
+ */
59
+ readonly headers: any;
60
+ }
61
+
62
+ /**
63
+ * A merchant's sealing identity. See the crate docs for the flow.
64
+ */
65
+ export class Sealer {
66
+ private constructor();
67
+ free(): void;
68
+ [Symbol.dispose](): void;
69
+ /**
70
+ * Signs through `sign`, a callback `(tbs: Uint8Array) => Uint8Array |
71
+ * Promise<Uint8Array>` returning the 64-byte pure-Ed25519 signature over
72
+ * exactly `tbs` (`crypto.sign(null, tbs, key)`). The key never enters this
73
+ * module. A signature that does not verify under `public_key_base64url` is
74
+ * refused here.
75
+ */
76
+ static fromCallback(public_key_base64url: string, sign: Function, server_current: string, server_next: string | null | undefined, audience: string): Sealer;
77
+ /**
78
+ * Signs with the Ed25519 key whose seed is `seed_base64url` (32 bytes,
79
+ * unpadded base64url). `server_current` and `server_next` are the pinned
80
+ * response keys in the same spelling; `server_next` may be `undefined`.
81
+ * `audience` is the server's public base URL.
82
+ */
83
+ static fromSeed(seed_base64url: string, server_current: string, server_next: string | null | undefined, audience: string): Sealer;
84
+ /**
85
+ * Seals one call. `path` is under `/v1`; `body` is the form text of a
86
+ * write; `query` the canonical query of a read.
87
+ */
88
+ sealCall(method: string, path: string, body?: string | null, query?: string | null, idempotency_key?: string | null): Promise<SealedCall>;
89
+ /**
90
+ * The public half, unpadded base64url: what `merchant_clients[].cose_keys`
91
+ * holds.
92
+ */
93
+ readonly publicKey: string;
94
+ }