m2m-ads 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 M2M Classified Service
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,63 @@
1
+ // src/crypto.ts
2
+ import crypto from "crypto";
3
+ function signMessage(message, privateKey) {
4
+ const sign = crypto.createSign("SHA256");
5
+ sign.update(message);
6
+ sign.end();
7
+ return sign.sign(privateKey, "hex");
8
+ }
9
+ function verifyMessage(message, signature, publicKey) {
10
+ const verify = crypto.createVerify("SHA256");
11
+ verify.update(message);
12
+ verify.end();
13
+ return verify.verify(publicKey, signature, "hex");
14
+ }
15
+
16
+ // src/client.ts
17
+ var M2MClient = class {
18
+ constructor(config) {
19
+ this.config = config;
20
+ }
21
+ async register() {
22
+ const initRes = await fetch(`${this.config.baseUrl}/v1/register/init`, { method: "POST" });
23
+ if (!initRes.ok)
24
+ throw new Error("register/init failed");
25
+ const { id } = await initRes.json();
26
+ const completeRes = await fetch(`${this.config.baseUrl}/v1/register/complete`, {
27
+ method: "POST",
28
+ headers: { "content-type": "application/json" },
29
+ body: JSON.stringify({
30
+ id,
31
+ nonce: 0,
32
+ public_sign_key: this.config.public_sign_key ?? "default",
33
+ country: "IT"
34
+ })
35
+ });
36
+ if (!completeRes.ok)
37
+ throw new Error("register/complete failed");
38
+ return completeRes.json();
39
+ }
40
+ async publish(ad) {
41
+ const res = await fetch(`${this.config.baseUrl}/v1/ads`, {
42
+ method: "POST",
43
+ headers: {
44
+ "content-type": "application/json",
45
+ authorization: `Bearer ${this.config.access_token}`
46
+ },
47
+ body: JSON.stringify(ad)
48
+ });
49
+ if (!res.ok)
50
+ throw new Error(`publish failed: ${res.status}`);
51
+ return res.json();
52
+ }
53
+ signMessage(message, privateKey) {
54
+ return signMessage(message, privateKey);
55
+ }
56
+ verifyMessage(message, signature, publicKey) {
57
+ return verifyMessage(message, signature, publicKey);
58
+ }
59
+ };
60
+
61
+ export {
62
+ M2MClient
63
+ };
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
package/dist/cli.js ADDED
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ M2MClient
4
+ } from "./chunk-R2E4NVOU.js";
5
+
6
+ // src/cli.ts
7
+ import { program } from "commander";
8
+
9
+ // src/config.ts
10
+ import os from "os";
11
+ import path from "path";
12
+ import fs from "fs/promises";
13
+ function getBase() {
14
+ return process.env.M2M_ADS_HOME ?? path.join(os.homedir(), ".m2m-ads");
15
+ }
16
+ async function loadConfig() {
17
+ const file = path.join(getBase(), "config.json");
18
+ const data = await fs.readFile(file, "utf-8");
19
+ return JSON.parse(data);
20
+ }
21
+ async function ensureConfigDir() {
22
+ await fs.mkdir(getBase(), { recursive: true });
23
+ }
24
+ async function saveConfig(config) {
25
+ await ensureConfigDir();
26
+ const file = path.join(getBase(), "config.json");
27
+ await fs.writeFile(file, JSON.stringify(config, null, 2));
28
+ }
29
+
30
+ // src/commands/register.ts
31
+ async function register() {
32
+ const config = await loadConfig();
33
+ const client = new M2MClient(config);
34
+ const result = await client.register();
35
+ await saveConfig(result);
36
+ console.log("Registered:", result.machine_id);
37
+ }
38
+
39
+ // src/commands/publish.ts
40
+ async function publish(ad) {
41
+ const config = await loadConfig();
42
+ const client = new M2MClient(config);
43
+ const result = await client.publish(ad);
44
+ console.log("Ad published:", result.ad_id);
45
+ }
46
+
47
+ // src/cli.ts
48
+ program.command("register").description("Register this machine").action(register);
49
+ program.command("publish <ad>").description("Publish a new ad (JSON string)").action((ad) => publish(JSON.parse(ad)));
50
+ program.parse();
@@ -0,0 +1,35 @@
1
+ interface Config {
2
+ baseUrl: string;
3
+ machine_id?: string;
4
+ access_token?: string;
5
+ public_sign_key?: string;
6
+ }
7
+ interface AdInput {
8
+ op: 'buy' | 'sell' | 'exchange' | 'gift';
9
+ title: string;
10
+ description: string;
11
+ price?: number;
12
+ currency?: string;
13
+ coord: {
14
+ lat: number;
15
+ lon: number;
16
+ };
17
+ embedding: number[];
18
+ radius_m?: number;
19
+ }
20
+ declare class M2MClient {
21
+ private config;
22
+ constructor(config: Config);
23
+ register(): Promise<{
24
+ machine_id: string;
25
+ access_token: string;
26
+ }>;
27
+ publish(ad: AdInput): Promise<{
28
+ ad_id: number;
29
+ status: string;
30
+ }>;
31
+ signMessage(message: string, privateKey: string): string;
32
+ verifyMessage(message: string, signature: string, publicKey: string): boolean;
33
+ }
34
+
35
+ export { M2MClient };
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ import {
2
+ M2MClient
3
+ } from "./chunk-R2E4NVOU.js";
4
+ export {
5
+ M2MClient
6
+ };
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "m2m-ads",
3
+ "version": "0.1.0",
4
+ "description": "M2M classified service client — library and CLI for machine-to-machine ad exchange",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.js",
12
+ "types": "./dist/index.d.ts"
13
+ }
14
+ },
15
+ "bin": {
16
+ "m2m-ads": "dist/cli.js"
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "engines": {
22
+ "node": ">=18.0.0"
23
+ },
24
+ "keywords": [
25
+ "m2m",
26
+ "classified",
27
+ "ads",
28
+ "machine-to-machine",
29
+ "cli"
30
+ ],
31
+ "dependencies": {
32
+ "commander": "^12.0.0"
33
+ },
34
+ "devDependencies": {
35
+ "@types/mocha": "^10.0.10",
36
+ "@types/node": "^25.3.3",
37
+ "mocha": "^11.0.0",
38
+ "tsup": "^6.0.0",
39
+ "tsx": "^4.0.0",
40
+ "typescript": "^5.9.3"
41
+ },
42
+ "scripts": {
43
+ "build": "tsup src/index.ts src/cli.ts --format esm --dts",
44
+ "test": "node --import tsx/esm node_modules/.bin/mocha --exit test/client.test.ts --timeout 15000"
45
+ }
46
+ }