kuantum-qrng-sdk 1.0.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/dist/QuantumOracleSDK.d.ts +25 -0
- package/dist/QuantumOracleSDK.js +84 -0
- package/package.json +17 -0
- package/src/QuantumOracleSDK.ts +94 -0
- package/tsconfig.json +14 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export declare class QuantumSDK {
|
|
2
|
+
private provider;
|
|
3
|
+
private wallet;
|
|
4
|
+
private contractAddress;
|
|
5
|
+
private contract;
|
|
6
|
+
private relayerUrl;
|
|
7
|
+
/**
|
|
8
|
+
* @param rpcUrl Monad network RPC URL
|
|
9
|
+
* @param contractAddress Deployed QuantumOracle contract address
|
|
10
|
+
* @param privateKey Private key of the caller/dApp wallet that pays for transaction gas
|
|
11
|
+
* @param relayerUrl Optional custom relayer URL, defaults to http://localhost:3000
|
|
12
|
+
*/
|
|
13
|
+
constructor(rpcUrl: string, contractAddress: string, privateKey: string, relayerUrl?: string);
|
|
14
|
+
/**
|
|
15
|
+
* Fetches quantum random number & signature from the off-chain relayer,
|
|
16
|
+
* then submits it directly to the Monad smart contract.
|
|
17
|
+
*
|
|
18
|
+
* @returns The transaction hash of the successful transaction.
|
|
19
|
+
*/
|
|
20
|
+
fetchAndSubmitRandom(): Promise<string>;
|
|
21
|
+
/**
|
|
22
|
+
* Helper to query the last verified random number on-chain.
|
|
23
|
+
*/
|
|
24
|
+
getLastRandomNumber(): Promise<number>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.QuantumSDK = void 0;
|
|
7
|
+
const ethers_1 = require("ethers");
|
|
8
|
+
const axios_1 = __importDefault(require("axios"));
|
|
9
|
+
// Minimal ABI for interacting with the consumeRandom function of the QuantumOracle contract
|
|
10
|
+
const ORACLE_ABI = [
|
|
11
|
+
'function consumeRandom(uint8 _randomNumber, bytes calldata _signature) external',
|
|
12
|
+
'function lastRandomNumber() external view address',
|
|
13
|
+
'function oracleNode() external view address'
|
|
14
|
+
];
|
|
15
|
+
class QuantumSDK {
|
|
16
|
+
provider;
|
|
17
|
+
wallet;
|
|
18
|
+
contractAddress;
|
|
19
|
+
contract;
|
|
20
|
+
relayerUrl;
|
|
21
|
+
/**
|
|
22
|
+
* @param rpcUrl Monad network RPC URL
|
|
23
|
+
* @param contractAddress Deployed QuantumOracle contract address
|
|
24
|
+
* @param privateKey Private key of the caller/dApp wallet that pays for transaction gas
|
|
25
|
+
* @param relayerUrl Optional custom relayer URL, defaults to http://localhost:3000
|
|
26
|
+
*/
|
|
27
|
+
constructor(rpcUrl, contractAddress, privateKey, relayerUrl = 'http://localhost:3000') {
|
|
28
|
+
this.provider = new ethers_1.JsonRpcProvider(rpcUrl);
|
|
29
|
+
this.wallet = new ethers_1.Wallet(privateKey, this.provider);
|
|
30
|
+
this.contractAddress = contractAddress;
|
|
31
|
+
this.contract = new ethers_1.Contract(contractAddress, ORACLE_ABI, this.wallet);
|
|
32
|
+
this.relayerUrl = relayerUrl;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Fetches quantum random number & signature from the off-chain relayer,
|
|
36
|
+
* then submits it directly to the Monad smart contract.
|
|
37
|
+
*
|
|
38
|
+
* @returns The transaction hash of the successful transaction.
|
|
39
|
+
*/
|
|
40
|
+
async fetchAndSubmitRandom() {
|
|
41
|
+
try {
|
|
42
|
+
console.log(`[SDK] Fetching quantum random number from relayer: ${this.relayerUrl}/quantum-random...`);
|
|
43
|
+
// 1. Fetch random number and signature from off-chain relayer
|
|
44
|
+
const response = await axios_1.default.get(`${this.relayerUrl}/quantum-random`);
|
|
45
|
+
const { randomNumber, signature, oracleAddress } = response.data;
|
|
46
|
+
if (randomNumber === undefined || !signature) {
|
|
47
|
+
throw new Error('Invalid response from QRNG Relayer');
|
|
48
|
+
}
|
|
49
|
+
console.log(`[SDK] Quantum Number retrieved: ${randomNumber}`);
|
|
50
|
+
console.log(`[SDK] Relayer Signature: ${signature}`);
|
|
51
|
+
console.log(`[SDK] Signed by Oracle Address: ${oracleAddress}`);
|
|
52
|
+
// 2. Submit transaction to Monad contract
|
|
53
|
+
console.log(`[SDK] Submitting transaction to QuantumOracle at ${this.contractAddress}...`);
|
|
54
|
+
const tx = await this.contract.consumeRandom(randomNumber, signature);
|
|
55
|
+
console.log(`[SDK] Transaction submitted! Hash: ${tx.hash}`);
|
|
56
|
+
// 3. Wait for confirmation
|
|
57
|
+
console.log('[SDK] Waiting for block confirmation...');
|
|
58
|
+
const receipt = await tx.wait();
|
|
59
|
+
if (!receipt || receipt.status !== 1) {
|
|
60
|
+
throw new Error('Transaction execution failed on-chain');
|
|
61
|
+
}
|
|
62
|
+
console.log(`[SDK] Transaction confirmed successfully in block ${receipt.blockNumber}!`);
|
|
63
|
+
return tx.hash;
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
console.error('[SDK] Error in fetchAndSubmitRandom:', error.message || error);
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Helper to query the last verified random number on-chain.
|
|
72
|
+
*/
|
|
73
|
+
async getLastRandomNumber() {
|
|
74
|
+
try {
|
|
75
|
+
const num = await this.contract.lastRandomNumber();
|
|
76
|
+
return Number(num);
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
console.error('[SDK] Error reading last random number:', error.message || error);
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.QuantumSDK = QuantumSDK;
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "kuantum-qrng-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "TypeScript SDK for Monad QRNG Oracle",
|
|
5
|
+
"main": "dist/QuantumOracleSDK.js",
|
|
6
|
+
"types": "dist/QuantumOracleSDK.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"axios": "^1.7.2",
|
|
12
|
+
"ethers": "^6.13.1"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"typescript": "^5.4.5"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { JsonRpcProvider, Wallet, Contract } from 'ethers';
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
|
|
4
|
+
// Minimal ABI for interacting with the consumeRandom function of the QuantumOracle contract
|
|
5
|
+
const ORACLE_ABI = [
|
|
6
|
+
'function consumeRandom(uint8 _randomNumber, bytes calldata _signature) external',
|
|
7
|
+
'function lastRandomNumber() external view address',
|
|
8
|
+
'function oracleNode() external view address'
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
export class QuantumSDK {
|
|
12
|
+
private provider: JsonRpcProvider;
|
|
13
|
+
private wallet: Wallet;
|
|
14
|
+
private contractAddress: string;
|
|
15
|
+
private contract: Contract;
|
|
16
|
+
private relayerUrl: string;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param rpcUrl Monad network RPC URL
|
|
20
|
+
* @param contractAddress Deployed QuantumOracle contract address
|
|
21
|
+
* @param privateKey Private key of the caller/dApp wallet that pays for transaction gas
|
|
22
|
+
* @param relayerUrl Optional custom relayer URL, defaults to http://localhost:3000
|
|
23
|
+
*/
|
|
24
|
+
constructor(
|
|
25
|
+
rpcUrl: string,
|
|
26
|
+
contractAddress: string,
|
|
27
|
+
privateKey: string,
|
|
28
|
+
relayerUrl: string = 'http://localhost:3000'
|
|
29
|
+
) {
|
|
30
|
+
this.provider = new JsonRpcProvider(rpcUrl);
|
|
31
|
+
this.wallet = new Wallet(privateKey, this.provider);
|
|
32
|
+
this.contractAddress = contractAddress;
|
|
33
|
+
this.contract = new Contract(contractAddress, ORACLE_ABI, this.wallet);
|
|
34
|
+
this.relayerUrl = relayerUrl;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Fetches quantum random number & signature from the off-chain relayer,
|
|
39
|
+
* then submits it directly to the Monad smart contract.
|
|
40
|
+
*
|
|
41
|
+
* @returns The transaction hash of the successful transaction.
|
|
42
|
+
*/
|
|
43
|
+
async fetchAndSubmitRandom(): Promise<string> {
|
|
44
|
+
try {
|
|
45
|
+
console.log(`[SDK] Fetching quantum random number from relayer: ${this.relayerUrl}/quantum-random...`);
|
|
46
|
+
|
|
47
|
+
// 1. Fetch random number and signature from off-chain relayer
|
|
48
|
+
const response = await axios.get(`${this.relayerUrl}/quantum-random`);
|
|
49
|
+
const { randomNumber, signature, oracleAddress } = response.data;
|
|
50
|
+
|
|
51
|
+
if (randomNumber === undefined || !signature) {
|
|
52
|
+
throw new Error('Invalid response from QRNG Relayer');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
console.log(`[SDK] Quantum Number retrieved: ${randomNumber}`);
|
|
56
|
+
console.log(`[SDK] Relayer Signature: ${signature}`);
|
|
57
|
+
console.log(`[SDK] Signed by Oracle Address: ${oracleAddress}`);
|
|
58
|
+
|
|
59
|
+
// 2. Submit transaction to Monad contract
|
|
60
|
+
console.log(`[SDK] Submitting transaction to QuantumOracle at ${this.contractAddress}...`);
|
|
61
|
+
|
|
62
|
+
const tx = await this.contract.consumeRandom(randomNumber, signature);
|
|
63
|
+
console.log(`[SDK] Transaction submitted! Hash: ${tx.hash}`);
|
|
64
|
+
|
|
65
|
+
// 3. Wait for confirmation
|
|
66
|
+
console.log('[SDK] Waiting for block confirmation...');
|
|
67
|
+
const receipt = await tx.wait();
|
|
68
|
+
|
|
69
|
+
if (!receipt || receipt.status !== 1) {
|
|
70
|
+
throw new Error('Transaction execution failed on-chain');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
console.log(`[SDK] Transaction confirmed successfully in block ${receipt.blockNumber}!`);
|
|
74
|
+
return tx.hash;
|
|
75
|
+
|
|
76
|
+
} catch (error: any) {
|
|
77
|
+
console.error('[SDK] Error in fetchAndSubmitRandom:', error.message || error);
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Helper to query the last verified random number on-chain.
|
|
84
|
+
*/
|
|
85
|
+
async getLastRandomNumber(): Promise<number> {
|
|
86
|
+
try {
|
|
87
|
+
const num = await this.contract.lastRandomNumber();
|
|
88
|
+
return Number(num);
|
|
89
|
+
} catch (error: any) {
|
|
90
|
+
console.error('[SDK] Error reading last random number:', error.message || error);
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
"rootDir": "./src",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true
|
|
12
|
+
},
|
|
13
|
+
"include": ["src/**/*"]
|
|
14
|
+
}
|