stellarium-ts 0.6.0 → 0.8.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.
@@ -12,7 +12,7 @@ export class OperatorClient {
12
12
  },
13
13
  });
14
14
  if (!response.ok) {
15
- throw new Error(`Failed to settle payment: ${response.statusText}`);
15
+ console.error(`Failed to settle payment: ${response.statusText} body: ${await response.text()}`);
16
16
  }
17
17
  }
18
18
  }
@@ -9,6 +9,22 @@ export interface PaymentVoucher {
9
9
  expiresAt: string;
10
10
  publicKey: string;
11
11
  }
12
+ /**
13
+ * The object `createPaymentVoucher` signs. `signature` is excluded, and
14
+ * `resourcePath`, `resourceName`, and `parameters` come from the invocation,
15
+ * not from the voucher itself. Key order is part of the signed bytes.
16
+ */
17
+ export declare function paymentVoucherPayload(voucher: Omit<PaymentVoucher, "signature">, resourcePath: string, resourceName: string, parameters: ResourceFunctionCallParameter[]): {
18
+ requestId: string;
19
+ invocationId: string;
20
+ amount: number;
21
+ recipientAddress: string;
22
+ expiresAt: string;
23
+ resourcePath: string;
24
+ resourceName: string;
25
+ parameters: ResourceFunctionCallParameter[];
26
+ publicKey: string;
27
+ };
12
28
  export declare class CombinationResourceInvoker implements ResourceInvoker {
13
29
  private readonly resourceRepository;
14
30
  private readonly costLimitLamports;
@@ -1 +1 @@
1
- {"version":3,"file":"invoker.d.ts","sourceRoot":"","sources":["../../src/resource/invoker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,6BAA6B,EAA6B,eAAe,EAAE,MAAM,eAAe,CAAC;AACpH,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAGrD,MAAM,WAAW,cAAc;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,gBAAgB,EAAE,MAAM,CAAC;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MA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AY,EAAE,MAAM,EACpB,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,6BAA6B,EAAE;;;;;;;;;;EAI9C;AAED,qBAAa,0BAA2B,YAAW,eAAe;IAElD,OAAO,CAAC,QAAQ,CAAC,kBAAkB;IAAsB,OAAO,CAAC,QAAQ,CAAC,iBAAiB;IAAU,OAAO,CAAC,QAAQ,CAAC,SAAS;IAAU,OAAO,CAAC,QAAQ,CAAC,UAAU;IAAhL,YAA6B,kBAAkB,EAAE,kBAAkB,EAAmB,iBAAiB,EAAE,MAAM,EAAmB,SAAS,EAAE,MAAM,EAAmB,UAAU,EAAE,MAAM,EAAI;IAC5L;;;;;;;;;;OAUG;IACG,MAAM,CAAC,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,6BAA6B,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,CAkDhH;IAED,oBAAoB,CAAC,gBAAgB,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,6BAA6B,EAAE,GAAG,cAAc,CAOtK;CACJ"}
@@ -1,4 +1,13 @@
1
1
  import { signData } from "../crypto/utils.js";
2
+ /**
3
+ * The object `createPaymentVoucher` signs. `signature` is excluded, and
4
+ * `resourcePath`, `resourceName`, and `parameters` come from the invocation,
5
+ * not from the voucher itself. Key order is part of the signed bytes.
6
+ */
7
+ export function paymentVoucherPayload(voucher, resourcePath, resourceName, parameters) {
8
+ const { requestId, invocationId, amount, recipientAddress, expiresAt, publicKey } = voucher;
9
+ return { requestId, invocationId, amount, recipientAddress, expiresAt, resourcePath, resourceName, parameters, publicKey };
10
+ }
2
11
  export class CombinationResourceInvoker {
3
12
  resourceRepository;
4
13
  costLimitLamports;
@@ -39,6 +48,7 @@ export class CombinationResourceInvoker {
39
48
  }
40
49
  let paymentVoucher;
41
50
  if (resourceFunction.paymentManifest) {
51
+ console.log(`Resource function ${resource.name}.${functionName} has a payment manifest, creating payment voucher...`);
42
52
  if (resourceFunction.paymentManifest.costLamports > this.costLimitLamports) {
43
53
  throw new Error(`Function ${functionName} cost ${resourceFunction.paymentManifest.costLamports} lamports, which exceeds the cost limit of ${this.costLimitLamports} lamports`);
44
54
  }
@@ -71,7 +81,7 @@ export class CombinationResourceInvoker {
71
81
  const invocationId = crypto.randomUUID();
72
82
  // Expire in 2 minutes
73
83
  const expiresAt = new Date(Date.now() + 1000 * 60 * 2).toISOString();
74
- const signature = signData({ requestId, invocationId, amount, recipientAddress, expiresAt, resourcePath, resourceName, parameters, publicKey: this.publicKey }, this.privateKey);
84
+ const signature = signData(paymentVoucherPayload({ requestId, invocationId, amount, recipientAddress, expiresAt, publicKey: this.publicKey }, resourcePath, resourceName, parameters), this.privateKey);
75
85
  return { requestId, invocationId, amount, recipientAddress, signature, expiresAt, publicKey: this.publicKey };
76
86
  }
77
87
  }
@@ -1 +1 @@
1
- {"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAKlD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAG5C,UAAU,YAAY;IAClB,OAAO,EAAE,OAAO,CAAC;CACpB;AA+BD;;;;GAIG;AACH,QAAA,MAAM,MAAM,EAAE,kBAAkB,CAAC,YAAY,CAoI5C,CAAC;eAGa,MAAM"}
1
+ {"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAKlD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAG5C,UAAU,YAAY;IAClB,OAAO,EAAE,OAAO,CAAC;CACpB;AA+BD;;;;GAIG;AACH,QAAA,MAAM,MAAM,EAAE,kBAAkB,CAAC,YAAY,CAyI5C,CAAC;eAGa,MAAM"}
package/dist/routes.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { interpretParsed, parseScript } from 'requestscript';
2
+ import { paymentVoucherPayload } from './resource/invoker.js';
2
3
  import { verifyData } from './crypto/utils.js';
3
4
  /**
4
5
  * Encapsulates the routes
@@ -88,7 +89,7 @@ const routes = async (fastify, options) => {
88
89
  const resourceRequest = request.body;
89
90
  const paymentVoucher = resourceRequest.paymentVoucher;
90
91
  if (paymentVoucher) {
91
- const isValid = verifyData(paymentVoucher, paymentVoucher.publicKey, paymentVoucher.signature);
92
+ const isValid = verifyData(paymentVoucherPayload(paymentVoucher, resourceRequest.resourcePath, resourceRequest.resourceName, resourceRequest.parameters), paymentVoucher.publicKey, paymentVoucher.signature);
92
93
  if (!isValid) {
93
94
  return reply.status(400).send({ error: 'Invalid payment voucher' });
94
95
  }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=invoker.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"invoker.test.d.ts","sourceRoot":"","sources":["../../../src/tests/resource/invoker.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,24 @@
1
+ import { generateKeyPairSync } from "node:crypto";
2
+ import { describe, expect, test } from "vitest";
3
+ import { verifyData } from "../../crypto/utils.js";
4
+ import { CombinationResourceInvoker, paymentVoucherPayload } from "../../resource/invoker.js";
5
+ function generateBase64KeyPair() {
6
+ const { publicKey, privateKey } = generateKeyPairSync("ed25519", {
7
+ publicKeyEncoding: { type: "spki", format: "der" },
8
+ privateKeyEncoding: { type: "pkcs8", format: "der" },
9
+ });
10
+ return {
11
+ publicKey: publicKey.toString("base64url"),
12
+ privateKey: privateKey.toString("base64url"),
13
+ };
14
+ }
15
+ describe("payment voucher", () => {
16
+ test("verifies the signed invocation payload, not the voucher object", () => {
17
+ const { publicKey, privateKey } = generateBase64KeyPair();
18
+ const invoker = new CombinationResourceInvoker({}, 100_000, publicKey, privateKey);
19
+ const parameters = [{ name: "city", value: "Oslo" }];
20
+ const voucher = invoker.createPaymentVoucher("4HkmXgXCJ8tw1ih5VFHetRt4kWdo1VLBVmV7B5p6ctK2", 10000, "com.example", "Weather", parameters);
21
+ expect(verifyData(paymentVoucherPayload(voucher, "com.example", "Weather", parameters), voucher.publicKey, voucher.signature)).toBe(true);
22
+ expect(verifyData(voucher, voucher.publicKey, voucher.signature)).toBe(false);
23
+ });
24
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stellarium-ts",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "description": "A Stellarium Node in TypeScript.",
5
5
  "homepage": "https://github.com/qualletio/stellarium-ts#readme",
6
6
  "bugs": {