diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..661ad18 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/dist +/node_modules +package-lock.json \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..28e2095 --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "@ideallabs/murmur.js", + "version": "0.1.0", + "description": "A wrapper to interact with the Murmur API", + "license": "GPL-3.0", + "repository": "https://github.com/ideal-lab5/murmur.js", + "main": "dist/index.js", + "scripts": { + "build": "tsc", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Ideal Labs ", + "dependencies": { + "axios": "^1.7.7" + }, + "devDependencies": { + "@types/node": "^22.7.4", + "typescript": "^5.6.2" + } +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..5a5af97 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,81 @@ +import axios, { AxiosInstance } from "axios"; + +class MurmurClient { + private client: AxiosInstance; + + constructor(baseURL: string) { + this.client = axios.create({ + baseURL, + headers: { + "Content-Type": "application/json", + }, + }); + } + + /** + * Creates a new wallet with a specified validity period. + * + * @param validity - The number of blocks in which the wallet will be valid. + * @returns A promise that resolves to a string indicating the result of the wallet creation. + */ + async login(username: string, password: string): Promise { + try { + const response = await this.client.post("/login", { username, password }); + return response.data; + } catch (error) { + throw new Error(`Login failed: ${error}`); + } + } + + /** + * Creates a new wallet with a specified validity period. + * + * @param validity - The number of blocks in which the wallet will be valid. + * @returns A promise that resolves to a string indicating the result of the wallet creation. + */ + async new(validity: number): Promise { + const MAX_U32 = 2 ** 32 - 1; + if (!Number.isInteger(validity)) { + throw new Error("The validity parameter must be an integer."); + } + + if (validity < 0 || validity > MAX_U32) { + throw new Error( + `The validity parameter must be within the range of 0 to ${MAX_U32}.` + ); + } + try { + const response = await this.client.post("/new", { validity }); + return response.data; + } catch (error) { + throw new Error(`New failed: ${error}`); + } + } + + /** + * Executes a transaction to send a specified amount of tokens to a destination account. + * + * @param amount - The amount of tokens to send. + * @param to - The destination account. + * @returns A promise that resolves to a string indicating the result of the transaction. + */ + async execute(amount: bigint, to: string): Promise { + const MAX_U128 = BigInt(2 ** 128 - 1); + if (!Number.isInteger(amount)) { + throw new Error("The amount parameter must be an integer."); + } + if (amount < 0 || amount > MAX_U128) { + throw new Error( + `The amount parameter must be within the range of 0 to ${MAX_U128}.` + ); + } + try { + const response = await this.client.post("/execute", { amount, to }); + return response.data; + } catch (error) { + throw new Error(`Execute failed: ${error}`); + } + } +} + +export default MurmurClient; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..fb695da --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "ES6", + "module": "commonjs", + "outDir": "./dist", + "rootDir": "./src", + "strict": true, + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true + }, + "include": ["src"] +} \ No newline at end of file