Skip to content

Development registrar client #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
381fc7c
implement account, farm, node services for registrar
SalmaElsoly Feb 17, 2025
3010a97
refactor to take private key in the intilization of the client
SalmaElsoly Feb 17, 2025
56136ee
fix return type of farm creation + add error handling in config
SalmaElsoly Feb 17, 2025
41a10fb
add integration tests
SalmaElsoly Feb 17, 2025
5f5f17f
add intial README
SalmaElsoly Feb 17, 2025
b737d50
configure eslint
SalmaElsoly Feb 18, 2025
a546fa6
add utils for signature and auth header + add keyed parameters to client
SalmaElsoly Feb 18, 2025
809aa51
fix json writing convention
SalmaElsoly Feb 19, 2025
397ab7f
update repo README
SalmaElsoly Feb 24, 2025
10f5896
adjust package exports
SalmaElsoly Feb 25, 2025
fc8e8a0
update dirs structure + use latest js + update package.json
SalmaElsoly Feb 25, 2025
232d833
add more tests and validation for node data
SalmaElsoly Feb 26, 2025
db36618
add scripts for usage
SalmaElsoly Feb 27, 2025
bec1da1
update README
SalmaElsoly Feb 27, 2025
4664bac
skip failed tests because of server bug
SalmaElsoly Feb 27, 2025
0d30268
update scripts to use config.json + update yarn version
SalmaElsoly Mar 3, 2025
0443d0c
update declarations in tsconfig
SalmaElsoly Mar 4, 2025
1b6ab88
updates tests to config.json + fix build module types
SalmaElsoly Mar 4, 2025
79df76d
fix uri
SalmaElsoly Mar 5, 2025
1e321b4
support stellar address in farm creation and update + fix tests
SalmaElsoly Mar 10, 2025
142087d
update scripts + fix package.json
SalmaElsoly Mar 10, 2025
6b287e9
disable workspace in pkg.json
SalmaElsoly Mar 10, 2025
f4d8f84
update readme
SalmaElsoly Mar 10, 2025
1e4e107
fix scripts + update readme
SalmaElsoly Mar 11, 2025
0bab692
fix requests timeout
SalmaElsoly Mar 11, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
13 changes: 13 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"printWidth": 120,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": false,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"trailingComma": "all",
"bracketSpacing": true,
"arrowParens": "avoid",
"endOfLine": "auto"
}
Binary file added .yarn/install-state.gz
Binary file not shown.
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
24 changes: 24 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
# tfgridv4-sdk-ts

This repo contains the typescript clients and projects for Threefold gridV4.

## Packages

- [registrar_client](./packages/registrar_client/README.md)

## Requirements

- [Node.js](https://nodejs.org/) (version 20 or higher)
- [lerna](https://lerna.js.org/) (version 8.1.9 or higher)
- [yarn](https://yarnpkg.com/) (version 4.7.0)

## Install

```bash
yarn install
```

## Build

```bash
yarn lerna run build
```
30 changes: 30 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// eslint.config.mjs
import js from "@eslint/js";
import ts from "@typescript-eslint/eslint-plugin";
import tsParser from "@typescript-eslint/parser";
import prettier from "eslint-plugin-prettier";
import prettierConfig from "eslint-config-prettier";
import globals from "globals";


export default [
js.configs.recommended,
{
files : ["**/*.ts"],
languageOptions: {
parser: tsParser,
ecmaVersion: "latest",
sourceType: "module",
globals: {
...globals.node
}
},
plugins: {
"@typescript-eslint": ts,
prettier,
},
rules: {
},
},
prettierConfig,
];
5 changes: 5 additions & 0 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "independent",
"npmClient": "yarn"
}
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@threefold/root",
"private": true,
"workspaces": [
"packages/*"
],
"engines": {
"node": ">=20.10.0"
},
"dependencies": {
"cosmiconfig": "^9.0.0",
"globals": "^15.15.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^8.24.0",
"@typescript-eslint/parser": "^8.24.0",
"eslint": "^9.20.1",
"eslint-config-prettier": "^10.0.1",
"eslint-plugin-prettier": "^5.2.3",
"lerna": "^8.1.9",
"prettier": "^3.5.1",
"ts-node": "^10.9.2",
"typescript": "^5.7.3"
}
}
94 changes: 94 additions & 0 deletions packages/registrar_client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Registrar Client

This package provides a client for interacting with the TFGrid v4 Node Registrar.

## Prerequisites

- node 20.10.0 or higher

## Installation

1. **Clone the Repository**

```bash
git clone https://github.com/threefoldtech/tfgridv4-sdk-ts
cd tfgridv4-sdk-ts
```

2. **Navigate to the Package Directory**

```bash
cd packages/registrar_client
```

3. **Install Dependencies**

```bash
yarn install
```

4. **Build the Package**

```bash
yarn build
```

## Getting Started

To initialize the Registrar Client, you need to provide the base url of registrar and Base64-encoded, 64-byte raw Ed25519 private key (nacl format).

To generate a 64-byte ed25519 private key, you can use tweetnacl library to generate key:

```typescript
import nacl from "tweetnacl";
import base64 from "base64-js";

const keyPair = nacl.sign.keyPair();
const privateKey = base64.fromByteArray(keyPair.secretKey);

console.log("Your 64-byte ed25519 private key:", privateKey);
```

Here is an example:

```typescript
const client = new RegistrarClient({ baseURl: "https://registrar.dev4.grid.tf/v1", privateKey: your_private_key });
```

## Usage

Here is an example of how to use the Registrar Client:

```typescript
const client = new RegistrarClient({ baseUrl: URl, privateKey: your_private_key });

// Example: Create an account
const accountRequest: CreateAccountRequest = {
// your create account request data
};

const account = await client.account.createAccount(accountRequest);

// Example: Get account details
const twinID = account.twin_id;
const accountDetails = await client.account.getAccountByTwinId(twinID);

// Example: Update account information
const updateAccountRequest: UpdateAccountRequest = {
// your update account request data
};

const updatedAccount = await client.account.updateAccount(twinID, updateAccountRequest);
```

more example script can be found [here](./scripts/)

## Tests

- Before running the tests set your base url in `tests/config.json`

then run with:

```bash
yarn test
```
11 changes: 11 additions & 0 deletions packages/registrar_client/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

// jest.config.js
module.exports = {
preset: "ts-jest/presets/default-esm",
transform: {
"^.+\\.(ts|tsx)$": ["ts-jest", { useESM: true, tsconfig: "tsconfig-node.json" }],
},
extensionsToTreatAsEsm: [".ts", ".tsx"],
testEnvironment: "node",
setupFiles: ["dotenv/config"],
};
64 changes: 64 additions & 0 deletions packages/registrar_client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"name": "@threefold/registrar_client",
"version": "0.1.0",
"description": "Registrar client for the ThreeFold Grid V4",
"keywords": [],
"author": "Salma Elsoly <salmaelsoly@gmail.com>",
"license": "ISC",
"private": false,
"publishConfig": {
"access": "public"
},
"main": "./dist/node/src/index.js",
"module": "./dist/es/src/index.js",
"exports": {
".": {
"import": "./dist/es/src/index.js",
"require": "./dist/node/src/index.js"
}
},
"types": "./dist/es/src/index.d.ts",
"engines": {
"node": ">=20.10.0"
},
"directories": {
"lib": "src",
"test": "tests"
},
"files": [
"/dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/threefoldtech/tfgridv4-sdk-ts.git"
},
"scripts": {
"test": "jest",
"format": "prettier --write .",
"lint": "eslint src/**/*.ts",
"lint:fix": "eslint src/**/*.ts --fix",
"node-build": "tsc --build tsconfig-node.json",
"es-build": "tsc --build tsconfig-es.json",
"build": "npm run node-build && npm run es-build"
},
"bugs": {
"url": "https://github.com/threefoldtech/tfgridv4-sdk-ts/issues"
},
"homepage": "https://github.com/threefoldtech/tfgridv4-sdk-ts#readme",
"devDependencies": {
"@types/jest": "^29.5.14",
"@types/node": "^22.13.4",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.7.3"
},
"dependencies": {
"@stellar/stellar-base": "^12.1.1",
"@types/jest": "^29.5.14",
"dotenv": "^16.4.7",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"tweetnacl": "^1.0.3"
}
}
27 changes: 27 additions & 0 deletions packages/registrar_client/scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Registrar Client Scripts

This directory contains scripts for the Registrar Client package.

## Prerequisites

- Node.js 20 or higher

## Installation

To install the necessary dependencies, run:

```bash
yarn install
```

## Getting Started

- Set your base url and private key in `scripts/config.json`

## Usage

To run any of the scripts, use the following command format:

```bash
yarn ts-node <script_name>.ts
```
1 change: 1 addition & 0 deletions packages/registrar_client/scripts/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"baseUrl": "http://localhost:8080/v1", "privateKey": "", "stellarAddress": ""}
17 changes: 17 additions & 0 deletions packages/registrar_client/scripts/create_account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { log } from "console";
import { RegistrarClient } from "../src/";
import config from "./config.json";

async function createAccount(client: RegistrarClient) {
const account = await client.accounts.createAccount({});
log("================= Creating Account =================");
log(account);
log("================= Creating Account =================");
}

async function main() {
const client = new RegistrarClient({ baseURL: config.baseUrl, privateKey: config.privateKey });
await createAccount(client);
}

main();
36 changes: 36 additions & 0 deletions packages/registrar_client/scripts/create_farm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { log } from "console";
import { Account, RegistrarClient } from "../src/";
import config from "./config.json";

async function createAccount(client: RegistrarClient): Promise<Account> {
const account = await client.accounts.createAccount({});
log("================= Creating Account =================");
log(account);
log("================= Creating Account =================");
return account!;
}

async function createFarm(client: RegistrarClient, twinID: number, stellarAddress: string): Promise<number> {
const res = await client.farms.createFarm(`test${Date.now()}`, false, twinID, stellarAddress);
log("================= Creating Farm =================");
log(res);
log("================= Creating Farm =================");
return res!.farm_id;
}

async function getFarm(client: RegistrarClient, farmID: number) {
const farm = await client.farms.getFarm(farmID);
log("================= Getting Farm =================");
log(farm);
log("================= Getting Farm =================");
}

async function main() {
const client = new RegistrarClient({ baseURL: config.baseUrl, privateKey: config.privateKey });
const account = await createAccount(client);
const twinID = account.twin_id;
const farmID = await createFarm(client, twinID, config.stellarAddress);
await getFarm(client, farmID);
}

main();
Loading