@avaprotocol/sdk-js 0.6.6 → 0.6.7

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/jest.config.cjs DELETED
@@ -1,21 +0,0 @@
1
- require('dotenv').config();
2
-
3
- module.exports = {
4
- preset: 'ts-jest/presets/default-esm',
5
- testEnvironment: 'node',
6
- moduleFileExtensions: ['ts', 'js'],
7
- testMatch: ['**/__tests__/**/*.test.ts'],
8
- transform: {
9
- '^.+\\.(ts|tsx|js|jsx)$': ['ts-jest', {
10
- useESM: true,
11
- }],
12
- },
13
- extensionsToTreatAsEsm: ['.ts'],
14
- moduleNameMapper: {
15
- '^(\\.{1,2}/.*)\\.js$': '$1',
16
- },
17
- setupFiles: ['dotenv/config'],
18
- transformIgnorePatterns: [
19
- 'node_modules/(?!(avs_pb.js|avs_grpc_pb.js)/)',
20
- ],
21
- };
@@ -1,27 +0,0 @@
1
- import { ethers } from "ethers";
2
- import { getKeyRequestMessage } from "../dist/index.js";
3
-
4
- const mnemonic = process.env.TEST_MNEMONIC;
5
- const expiredAtEpoch = Math.floor(Date.now() / 1000) + 24 * 60 * 60;
6
-
7
- if (!mnemonic) {
8
- throw new Error("Mnemonic is required; Please set the TEST_MNEMONIC environment variable.");
9
- }
10
-
11
- const wallet = ethers.Wallet.fromPhrase(mnemonic);
12
-
13
- console.log("Address:", wallet.address, "Expired at epoch:", expiredAtEpoch);
14
- console.log("Private key:", wallet.privateKey);
15
-
16
- // Generate the key request message
17
- const message = getKeyRequestMessage(wallet.address, expiredAtEpoch);
18
-
19
- async function main() {
20
- const signature = await wallet.signMessage(message);
21
- console.log("Message:", message);
22
- console.log("Signature:", signature);
23
- }
24
-
25
- main()
26
- .catch(console.error)
27
- .finally(() => process.exit());
@@ -1,172 +0,0 @@
1
- import { describe, beforeAll, test, expect } from "@jest/globals";
2
- import Client from "../index";
3
- import dotenv from "dotenv";
4
- import path from "path";
5
- import { ethers } from "ethers";
6
- import { getKeyRequestMessage } from "../auth";
7
-
8
- // Load environment variables from .env.test file
9
- dotenv.config({ path: path.resolve(__dirname, "../../.env.test") });
10
-
11
- const { TEST_JWT_TOKEN, TEST_PRIVATE_KEY } = process.env;
12
- const EXPIRED_AT = Math.floor(Date.now() / 1000) + 24 * 60 * 60;
13
-
14
- // Add signature generation logic
15
- async function getAddress(privateKey: string): Promise<string> {
16
- const wallet = new ethers.Wallet(privateKey);
17
-
18
- console.log("Wallet address:", wallet.address);
19
- return wallet.address;
20
- }
21
-
22
- // Add signature generation logic
23
- async function generateSignature(
24
- privateKey: string,
25
- expiredAt: number
26
- ): Promise<string> {
27
- const wallet = new ethers.Wallet(privateKey);
28
- const message = getKeyRequestMessage(wallet.address, expiredAt);
29
-
30
- console.log("Signing message:", message, "Expired at:", expiredAt);
31
-
32
- const signature = await wallet.signMessage(message);
33
-
34
- return signature;
35
- }
36
-
37
- describe("Client E2E Tests", () => {
38
- let client: Client;
39
- let walletAddress: string; // Add this line to declare the variable
40
-
41
- beforeAll(async () => {
42
- // Initialize the client with test credentials
43
- client = new Client({
44
- env: "staging",
45
- });
46
-
47
- if (!TEST_PRIVATE_KEY) {
48
- throw new Error("TEST_PRIVATE_KEY is not set in the .env.test file");
49
- }
50
-
51
- // Generate the address here
52
- const address = await getAddress(TEST_PRIVATE_KEY);
53
- walletAddress = address;
54
- });
55
-
56
- // test("authWithJwtToken", async () => {
57
- // if (!TEST_JWT_TOKEN) {
58
- // throw new Error("TEST_JWT_TOKEN is not set in the .env.test file");
59
- // }
60
-
61
- // const response = await client.authWithJwtToken(
62
- // walletAddress,
63
- // TEST_JWT_TOKEN,
64
- // EXPIRED_AT
65
- // );
66
-
67
- // console.log("authWithJwtToken response:", response);
68
- // expect(response).toBeDefined();
69
- // });
70
-
71
- test("authWithSignature", async () => {
72
- if (!TEST_PRIVATE_KEY) {
73
- throw new Error("TEST_PRIVATE_KEY is not set in the .env.test file");
74
- }
75
-
76
- if (!EXPIRED_AT) {
77
- throw new Error("EXPIRED_AT is not set.");
78
- }
79
-
80
- const signature = await generateSignature(TEST_PRIVATE_KEY, EXPIRED_AT);
81
-
82
- if (!signature) {
83
- throw new Error(
84
- "Signature could not be generated. Make sure TEST_PRIVATE_KEY is set in the .env.test file"
85
- );
86
- }
87
-
88
- const response = await client.authWithSignature(
89
- walletAddress,
90
- signature,
91
- EXPIRED_AT
92
- );
93
-
94
- console.log("authWithSignature response:", response);
95
-
96
- expect(response).toBeDefined();
97
- expect(response).toHaveProperty("key");
98
- expect(typeof response.key).toBe("string");
99
-
100
- // Check if the key is a valid JWT token
101
- const jwtParts = response.key.split(".");
102
- expect(jwtParts).toHaveLength(3);
103
-
104
- // Decode the base64 token and check the payload
105
- const payload = JSON.parse(Buffer.from(jwtParts[1], "base64").toString());
106
- expect(payload).toHaveProperty("iss", "AvaProtocol");
107
- expect(payload).toHaveProperty("sub");
108
- expect(payload.sub).toMatch(/^0x[a-fA-F0-9]{40}$/); // Ethereum address format
109
- expect(payload).toHaveProperty("exp", EXPIRED_AT);
110
- });
111
-
112
- // describe("Authenticated Tests", () => {
113
- // let walletAddress: string;
114
- // let client: Client;
115
-
116
- // beforeAll(async () => {
117
- // // Initialize the client with test credentials
118
- // client = new Client({
119
- // env: "staging",
120
- // });
121
-
122
- // if (!TEST_PRIVATE_KEY) {
123
- // throw new Error("TEST_PRIVATE_KEY is not set in the .env.test file");
124
- // }
125
-
126
- // walletAddress = await getAddress(TEST_PRIVATE_KEY);
127
-
128
- // if (!EXPIRED_AT) {
129
- // throw new Error("EXPIRED_AT is not set");
130
- // }
131
-
132
- // const signature = await generateSignature(TEST_PRIVATE_KEY, EXPIRED_AT);
133
-
134
- // if (!signature) {
135
- // throw new Error(
136
- // "Signature could not be generated. Make sure TEST_PRIVATE_KEY is set in the .env.test file"
137
- // );
138
- // }
139
-
140
- // await client.authWithSignature(walletAddress, signature, EXPIRED_AT);
141
- // });
142
-
143
- // test("listTask", async () => {
144
- // const result = await client.listTask();
145
- // console.log("List task result:", result);
146
- // expect(result).toBeDefined();
147
- // expect(Array.isArray(result.tasks)).toBe(true);
148
- // });
149
-
150
- // test("getSmartWalletAddress", async () => {
151
- // const result = await client.getSmartWalletAddress(walletAddress);
152
- // console.log("Get smart wallet address result:", result);
153
- // expect(result).toBeDefined();
154
- // expect(result.smart_account_address).toMatch(/^0x[a-fA-F0-9]{40}$/); // Ethereum address format
155
- // expect(result).toHaveProperty("nonce");
156
- // });
157
-
158
- // test("getTask", async () => {
159
- // // First, list tasks to get a valid task ID
160
- // const listResult = await client.listTask();
161
- // if (listResult.tasks.length > 0) {
162
- // const taskId = listResult.tasks[0].id;
163
- // const result = await client.getTask(taskId);
164
- // console.log("Get task result:", result);
165
- // expect(result).toBeDefined();
166
- // expect(result.id).toBe(taskId);
167
- // } else {
168
- // console.warn("No tasks available to test getTask");
169
- // }
170
- // });
171
- // });
172
- });
package/src/auth.js DELETED
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getKeyRequestMessage = void 0;
4
- var getKeyRequestMessage = function (address, expiredAt) {
5
- return "key request for ".concat(address, " expired at ").concat(expiredAt);
6
- };
7
- exports.getKeyRequestMessage = getKeyRequestMessage;
package/src/auth.ts DELETED
@@ -1,3 +0,0 @@
1
- export const getKeyRequestMessage = (address: string, expiredAt: number): string => {
2
- return `key request for ${address} expired at ${expiredAt}`;
3
- }
package/src/config.js DELETED
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.configs = exports.DEFAULT_JWT_EXPIRATION = void 0;
4
- exports.getRpcEndpoint = getRpcEndpoint;
5
- exports.DEFAULT_JWT_EXPIRATION = 24 * 60 * 60; // 24 hours
6
- // Define the configs object with typed keys
7
- var configs = {
8
- development: {
9
- endpoint: process.env.AVS_RPC_URL || "localhost:2206",
10
- },
11
- staging: {
12
- endpoint: "aggregator-holesky.avaprotocol.org:2206",
13
- },
14
- production: {
15
- endpoint: "aggregator.avaprotocol.org:2206",
16
- },
17
- };
18
- exports.configs = configs;
19
- // Function to get RPC endpoint with improved type safety
20
- function getRpcEndpoint(env) {
21
- return configs[env].endpoint;
22
- }
package/src/config.ts DELETED
@@ -1,27 +0,0 @@
1
- import { Environment } from "./types";
2
-
3
- export const DEFAULT_JWT_EXPIRATION = 24 * 60 * 60; // 24 hours
4
- interface Config {
5
- AVS_RPC_URL: string;
6
- }
7
-
8
- // Define the configs object with typed keys
9
- const configs: Record<Environment, Config> = {
10
- development: {
11
- AVS_RPC_URL: process.env.AVS_RPC_URL || "localhost:2206",
12
- },
13
- staging: {
14
- AVS_RPC_URL: "aggregator-holesky.avaprotocol.org:2206",
15
- },
16
- production: {
17
- AVS_RPC_URL: "aggregator.avaprotocol.org:2206",
18
- },
19
- };
20
-
21
- // Function to get RPC endpoint with improved type safety
22
- export function getRpcEndpoint(env: Environment): string {
23
- return configs[env].AVS_RPC_URL;
24
- }
25
-
26
- // Export the configs only
27
- export { configs };
package/src/index.ts DELETED
@@ -1,131 +0,0 @@
1
- import * as grpc from "@grpc/grpc-js";
2
- import { Metadata } from "@grpc/grpc-js";
3
- import { DEFAULT_JWT_EXPIRATION, getRpcEndpoint } from "./config";
4
- import { Environment } from "./types";
5
- import { getKeyRequestMessage } from "./auth";
6
- import { AggregatorClient } from "../grpc_codegen/avs_grpc_pb";
7
- import * as avs_pb from "../grpc_codegen/avs_pb";
8
-
9
- const metadata = new grpc.Metadata();
10
-
11
- // Move interfaces to a separate file, e.g., types.ts
12
- import {
13
- KeyExchangeResp,
14
- ClientOption,
15
- TaskResp,
16
- TaskListResp,
17
- SmartWalletResp,
18
- } from "./types";
19
-
20
- class BaseClient {
21
- readonly env: Environment;
22
- readonly rpcClient;
23
- // protected owner?: string;
24
- // readonly opts: ClientOption;
25
- protected authkey?: string;
26
- protected wallet?: any;
27
-
28
- constructor(opts: ClientOption) {
29
- // if (!opts.jwtApiKey && !opts.owner) {
30
- // throw new Error("missing jwtApiKey or owner");
31
- // }
32
-
33
- this.env = opts.env || ("production" as Environment);
34
- this.rpcClient = new AggregatorClient(
35
- getRpcEndpoint(this.env),
36
- // TODO: switch to the TLS after we're able to update all the operator
37
- grpc.credentials.createInsecure()
38
- );
39
-
40
- console.log("this.rpcClient:", this.rpcClient);
41
-
42
- // this.opts = opts;
43
- // this.owner = opts.owner ?? undefined;
44
- }
45
-
46
- // async authWithJwtToken(
47
- // address: string,
48
- // jwtToken: string,
49
- // expiredAt?: number
50
- // ): Promise<KeyExchangeResp> {
51
- // console.log("Authenticating with JWT token: ", jwtToken);
52
-
53
- // // Use the provided expiredAt or set it to 24 hours from now if not provided
54
- // const expirationTime =
55
- // expiredAt || Math.floor(Date.now() / 1000) + DEFAULT_JWT_EXPIRATION;
56
-
57
- // const result: avsPb.KeyResp = await this._callRPC<
58
- // avsPb.KeyResp,
59
- // avsPb.GetKeyReq
60
- // >("getKey", {
61
- // owner: address,
62
- // expired_at: expirationTime,
63
- // signature: jwtToken,
64
- // });
65
-
66
- // this.authkey = result.getKey();
67
- // return { key: result.getKey() };
68
- // }
69
-
70
- // This flow can be used where the signature is generate from outside, such as in front-end and pass in
71
- async authWithSignature(
72
- address: string,
73
- signature: string,
74
- expiredAtEpoch: number
75
- ): Promise<KeyExchangeResp> {
76
- console.log(
77
- "Authenticating with signature:",
78
- signature,
79
- "Expired at epoch:",
80
- expiredAtEpoch
81
- );
82
-
83
- // Create a new GetKeyReq message
84
- const request = new avs_pb.GetKeyReq();
85
- request.setOwner(address);
86
- request.setExpiredAt(expiredAtEpoch);
87
- request.setSignature(signature);
88
-
89
- let result = await this._callRPC<avs_pb.KeyResp, avs_pb.GetKeyReq>(
90
- "getKey",
91
- request
92
- );
93
-
94
- console.log("result:", result);
95
- this.authkey = result.getKey();
96
-
97
- return { key: result.getKey() };
98
- }
99
-
100
- public isAuthenticated(): boolean {
101
- return !!this.authkey;
102
- }
103
-
104
- protected _callRPC<TResponse, TRequest>(
105
- method: string,
106
- request: TRequest | any
107
- ): Promise<TResponse> {
108
- return new Promise((resolve, reject) => {
109
- (this.rpcClient as any)[method].bind(this.rpcClient)(
110
- request,
111
- metadata,
112
- (error: any, response: TResponse) => {
113
- if (error) reject(error);
114
- else resolve(response);
115
- }
116
- );
117
- });
118
- }
119
- }
120
-
121
- export default class Client extends BaseClient {
122
- constructor(config: ClientOption) {
123
- super(config);
124
- }
125
- }
126
-
127
- // Export types for easier use
128
- export * from "./types";
129
-
130
- // Add this line at the end of the file
131
- export { getKeyRequestMessage };
package/src/rpc-client.ts DELETED
@@ -1,7 +0,0 @@
1
- export interface Rpc {
2
- // Add necessary methods based on your gRPC implementation
3
- // For example:
4
- request: (service: string, method: string, data: any) => Promise<any>;
5
- }
6
-
7
- // You can add a concrete implementation here if needed
package/src/types.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
package/src/types.ts DELETED
@@ -1,47 +0,0 @@
1
- // Define the environment type
2
- export type Environment = "production" | "development" | "staging";
3
-
4
- export interface KeyExchangeResp {
5
- key: string;
6
- }
7
-
8
- export interface ClientOption {
9
- // used primary in backend where owner own the key
10
- privateKey?: string;
11
-
12
- // used in automated manner where this key can schedule job on behalf of other user. this jwtApiKey is generated from aggregator CLI. It's a long live key.
13
- // This currently used in our telegram bot, because the bot can schedule and manage tasks for any user
14
- // It is just require the user to prove they own the wallet in initial onboarding.
15
- jwtApiKey?: string;
16
-
17
- // used in front-end app where front-end generate the signature
18
- presignSignature?: string;
19
- signatureExpiredAt?: number;
20
-
21
- env?: Environment;
22
- owner?: string;
23
- }
24
-
25
- export interface TaskResp {
26
- id: string;
27
- status: string;
28
- result?: any;
29
- error?: string;
30
- }
31
-
32
- export interface TaskListResp {
33
- tasks: TaskResp[];
34
- }
35
-
36
- export interface SmartWalletResp {
37
- address: string;
38
- smart_account_address: string;
39
- }
40
-
41
- export interface TransactionResp {
42
- hash: string;
43
- }
44
-
45
- export interface BalanceResp {
46
- balance: string;
47
- }
package/tsconfig.json DELETED
@@ -1,20 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2020",
4
- "module": "ESNext",
5
- "moduleResolution": "node",
6
- "esModuleInterop": true,
7
- "allowSyntheticDefaultImports": true,
8
- "strict": true,
9
- "outDir": "./dist",
10
- "declaration": true,
11
- "rootDir": "src"
12
- },
13
- "include": [
14
- "src/**/*",
15
- "grpc_codegen/**/*"
16
- ],
17
- "ts-node": {
18
- "esm": true
19
- }
20
- }