@sohopay/agent-signer 0.0.0-stage → 0.3.1

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 (54) hide show
  1. package/README.md +239 -2
  2. package/dist/cli/args.d.ts +18 -0
  3. package/dist/cli/args.js +97 -0
  4. package/dist/cli/commands.d.ts +37 -0
  5. package/dist/cli/commands.js +228 -0
  6. package/dist/cli/index.d.ts +2 -0
  7. package/dist/cli/index.js +32 -0
  8. package/dist/cli/io-schema.d.ts +2 -0
  9. package/dist/cli/io-schema.js +55 -0
  10. package/dist/cli/io.d.ts +18 -0
  11. package/dist/cli/io.js +56 -0
  12. package/dist/cli/key-generate.d.ts +8 -0
  13. package/dist/cli/key-generate.js +77 -0
  14. package/dist/cli/key-path.d.ts +5 -0
  15. package/dist/cli/key-path.js +156 -0
  16. package/dist/cli/node-floor.d.ts +4 -0
  17. package/dist/cli/node-floor.js +11 -0
  18. package/dist/cli/run.d.ts +11 -0
  19. package/dist/cli/run.js +118 -0
  20. package/dist/cli/signer-config.d.ts +5 -0
  21. package/dist/cli/signer-config.js +84 -0
  22. package/dist/cli/verify-vectors.d.ts +8 -0
  23. package/dist/cli/verify-vectors.js +116 -0
  24. package/dist/constants.d.ts +24 -0
  25. package/dist/constants.js +34 -0
  26. package/dist/encoding.d.ts +5 -0
  27. package/dist/encoding.js +5 -0
  28. package/dist/envelope.d.ts +11 -0
  29. package/dist/envelope.js +20 -0
  30. package/dist/errors.d.ts +7 -0
  31. package/dist/errors.js +9 -0
  32. package/dist/flow.d.ts +70 -0
  33. package/dist/flow.js +62 -0
  34. package/dist/idempotency.d.ts +32 -0
  35. package/dist/idempotency.js +45 -0
  36. package/dist/index.d.ts +12 -0
  37. package/dist/index.js +12 -0
  38. package/dist/jcs.d.ts +8 -0
  39. package/dist/jcs.js +15 -0
  40. package/dist/keys.d.ts +28 -0
  41. package/dist/keys.js +64 -0
  42. package/dist/operations.d.ts +107 -0
  43. package/dist/operations.js +151 -0
  44. package/dist/pop.d.ts +19 -0
  45. package/dist/pop.js +27 -0
  46. package/dist/rotation.d.ts +41 -0
  47. package/dist/rotation.js +33 -0
  48. package/dist/storage.d.ts +38 -0
  49. package/dist/storage.js +116 -0
  50. package/dist/terminal.d.ts +18 -0
  51. package/dist/terminal.js +90 -0
  52. package/dist/voucher.d.ts +39 -0
  53. package/dist/voucher.js +86 -0
  54. package/package.json +42 -4
@@ -0,0 +1,39 @@
1
+ import { type Ed25519PublicJwk } from "./keys.js";
2
+ export interface AgentPaymentVoucherCore {
3
+ agentId: string;
4
+ merchantId: string;
5
+ asset: string;
6
+ chainId: string;
7
+ amount: string;
8
+ feeAmount: string;
9
+ orderRef: string;
10
+ nonce: string;
11
+ deadline: string;
12
+ }
13
+ export interface AgentPaymentVoucher extends AgentPaymentVoucherCore {
14
+ paymentId: `0x${string}`;
15
+ agentKeyJkt: string;
16
+ }
17
+ /** The server's advertised signing block, validated against {@link SUPPORTED_SIGNING}. */
18
+ export interface ServerSigningBlock {
19
+ algorithm?: string;
20
+ domain_tag?: string;
21
+ canonicalization?: string;
22
+ signature_encoding?: string;
23
+ }
24
+ /** `paymentId = keccak256(utf8(tag) || 0x00 || JCS(core))`. */
25
+ export declare function computePaymentId(core: AgentPaymentVoucherCore): `0x${string}`;
26
+ /** The exact bytes the agent signs: tagged preimage over the full 11-field voucher. */
27
+ export declare function buildVoucherSignedBytes(voucher: AgentPaymentVoucher): Uint8Array;
28
+ export interface SignVoucherArgs {
29
+ voucher: AgentPaymentVoucher;
30
+ privateKeyBase64Url: string;
31
+ /** The server `signing` block from prepare — validated, never substituted. */
32
+ signing?: ServerSigningBlock;
33
+ /** When given, asserts the voucher's `agentKeyJkt` matches this signing key. */
34
+ publicJwk?: Ed25519PublicJwk;
35
+ }
36
+ /** Signs a voucher after fail-closed guards (scheme allowlist, paymentId, jkt). */
37
+ export declare function signVoucher(args: SignVoucherArgs): {
38
+ signature: string;
39
+ };
@@ -0,0 +1,86 @@
1
+ import { ed25519 } from "@noble/curves/ed25519";
2
+ import { keccak_256 } from "@noble/hashes/sha3";
3
+ import { DOMAIN_SEPARATOR_BYTE, SUPPORTED_SIGNING, VOUCHER_CORE_FIELDS, VOUCHER_DOMAIN_TAG, } from "./constants.js";
4
+ import { fromBase64Url, toBase64Url, toHex, utf8Bytes } from "./encoding.js";
5
+ import { SignerError } from "./errors.js";
6
+ import { jcs } from "./jcs.js";
7
+ import { computeJkt } from "./keys.js";
8
+ /** Every core field must be a string — no JSON number enters a signed preimage. */
9
+ function assertCoreStrings(core) {
10
+ for (const field of VOUCHER_CORE_FIELDS) {
11
+ if (typeof core[field] !== "string") {
12
+ throw new SignerError("VOUCHER_FIELD_NOT_STRING", `voucher field "${field}" must be a string, got ${typeof core[field]}`);
13
+ }
14
+ }
15
+ }
16
+ /** Explicit 9-field projection — an unknown wire key never enters the preimage. */
17
+ function voucherCoreOf(core) {
18
+ return {
19
+ agentId: core.agentId,
20
+ merchantId: core.merchantId,
21
+ asset: core.asset,
22
+ chainId: core.chainId,
23
+ amount: core.amount,
24
+ feeAmount: core.feeAmount,
25
+ orderRef: core.orderRef,
26
+ nonce: core.nonce,
27
+ deadline: core.deadline,
28
+ };
29
+ }
30
+ /** `utf8(tag) || 0x00 || JCS(value)`. */
31
+ function taggedPreimage(value) {
32
+ const tag = utf8Bytes(VOUCHER_DOMAIN_TAG);
33
+ const body = utf8Bytes(jcs(value));
34
+ const out = new Uint8Array(tag.length + 1 + body.length);
35
+ out.set(tag, 0);
36
+ out[tag.length] = DOMAIN_SEPARATOR_BYTE;
37
+ out.set(body, tag.length + 1);
38
+ return out;
39
+ }
40
+ /** `paymentId = keccak256(utf8(tag) || 0x00 || JCS(core))`. */
41
+ export function computePaymentId(core) {
42
+ assertCoreStrings(core);
43
+ return `0x${toHex(keccak_256(taggedPreimage(voucherCoreOf(core))))}`;
44
+ }
45
+ /** The exact bytes the agent signs: tagged preimage over the full 11-field voucher. */
46
+ export function buildVoucherSignedBytes(voucher) {
47
+ assertCoreStrings(voucher);
48
+ return taggedPreimage({
49
+ ...voucherCoreOf(voucher),
50
+ paymentId: voucher.paymentId,
51
+ agentKeyJkt: voucher.agentKeyJkt,
52
+ });
53
+ }
54
+ function assertSigningAllowed(signing) {
55
+ if (!signing) {
56
+ return;
57
+ }
58
+ const checks = [
59
+ ["algorithm", SUPPORTED_SIGNING.algorithm],
60
+ ["domain_tag", SUPPORTED_SIGNING.domain_tag],
61
+ ["canonicalization", SUPPORTED_SIGNING.canonicalization],
62
+ ["signature_encoding", SUPPORTED_SIGNING.signature_encoding],
63
+ ];
64
+ for (const [key, expected] of checks) {
65
+ const actual = signing[key];
66
+ if (actual !== undefined && actual !== expected) {
67
+ throw new SignerError("SIGNING_SCHEME_NOT_ALLOWED", `server proposed ${key}="${actual}"; this SDK signs only "${expected}"`);
68
+ }
69
+ }
70
+ }
71
+ /** Signs a voucher after fail-closed guards (scheme allowlist, paymentId, jkt). */
72
+ export function signVoucher(args) {
73
+ assertSigningAllowed(args.signing);
74
+ const recomputed = computePaymentId(args.voucher);
75
+ if (recomputed.toLowerCase() !== args.voucher.paymentId.toLowerCase()) {
76
+ throw new SignerError("PAYMENT_ID_MISMATCH", "voucher.paymentId does not match the hash of its core");
77
+ }
78
+ if (args.publicJwk) {
79
+ const jkt = computeJkt(args.publicJwk);
80
+ if (jkt !== args.voucher.agentKeyJkt) {
81
+ throw new SignerError("AGENT_KEY_JKT_MISMATCH", "voucher.agentKeyJkt does not match the signing key's thumbprint");
82
+ }
83
+ }
84
+ const signature = ed25519.sign(buildVoucherSignedBytes(args.voucher), fromBase64Url(args.privateKeyBase64Url));
85
+ return { signature: toBase64Url(signature) };
86
+ }
package/package.json CHANGED
@@ -1,6 +1,44 @@
1
1
  {
2
2
  "name": "@sohopay/agent-signer",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.3.1",
4
+ "description": "Deterministic client-side signer for SohoPay Protocol V2 agent payments: Ed25519 vouchers, PoP challenges, RFC 7638 jkt. Holds agent workload keys only — never borrower keys.",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./dist/index.js"
10
+ }
11
+ },
12
+ "bin": {
13
+ "sohopay-signer": "dist/cli/index.js"
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsc -p tsconfig.json",
20
+ "test": "node --import tsx --test test/*.test.ts",
21
+ "check": "tsc --noEmit && npm run test",
22
+ "bundle": "node scripts/bundle.mjs"
23
+ },
24
+ "dependencies": {
25
+ "@noble/curves": "^1.9.7",
26
+ "@noble/hashes": "^1.4.0",
27
+ "@sohopay/signer-vectors": "^0.2.0",
28
+ "canonicalize": "2.1.0"
29
+ },
30
+ "devDependencies": {
31
+ "@types/node": "^22.7.0",
32
+ "esbuild": "0.28.2",
33
+ "tsx": "^4.20.5",
34
+ "typescript": "^5.6.0"
35
+ },
36
+ "engines": {
37
+ "node": ">=18"
38
+ },
39
+ "publishConfig": {
40
+ "registry": "https://registry.npmjs.org",
41
+ "access": "public"
42
+ },
43
+ "license": "Apache-2.0"
44
+ }