@rialo/ts-cdk 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/README.md +350 -0
- package/dist/index.d.mts +1774 -0
- package/dist/index.d.ts +1774 -0
- package/dist/index.js +7358 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +7308 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +72 -0
package/README.md
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
# Rialo TypeScript CDK
|
|
2
|
+
|
|
3
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
4
|
+
|
|
5
|
+
A TypeScript library for interacting with the Rialo blockchain. Provides cryptographic primitives, transaction building, and RPC client for blockchain communication.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @rialo/ts-cdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
### Create a keypair and sign a message
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { Keypair } from "@rialo/ts-cdk";
|
|
19
|
+
|
|
20
|
+
const keypair = Keypair.generate();
|
|
21
|
+
console.log("Address:", keypair.publicKey.toString());
|
|
22
|
+
|
|
23
|
+
const message = new TextEncoder().encode("Hello Rialo");
|
|
24
|
+
const signature = keypair.sign(message);
|
|
25
|
+
const isValid = keypair.verify(message, signature);
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Connect to the blockchain
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { createRialoClient, URL_DEVNET } from "@rialo/ts-cdk";
|
|
32
|
+
|
|
33
|
+
const client = createRialoClient(URL_DEVNET);
|
|
34
|
+
|
|
35
|
+
// Query blockchain
|
|
36
|
+
const balance = await client.getBalance(keypair.publicKey);
|
|
37
|
+
const height = await client.getBlockHeight();
|
|
38
|
+
const blockhash = await client.getLatestBlockhash();
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Build and send a transaction
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { TransactionBuilder, transferInstruction } from "@rialo/ts-cdk";
|
|
45
|
+
|
|
46
|
+
// Get blockhash for replay protection
|
|
47
|
+
const blockhash = await client.getLatestBlockhash();
|
|
48
|
+
|
|
49
|
+
// transaction creation time
|
|
50
|
+
const txCreationTime = BigInt(Date.now());
|
|
51
|
+
|
|
52
|
+
// Create transfer instruction
|
|
53
|
+
const transfer = transferInstruction(
|
|
54
|
+
keypair.publicKey,
|
|
55
|
+
recipientAddress,
|
|
56
|
+
BigInt(1_000_000) // amount in kelvins
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
// Build and sign transaction
|
|
60
|
+
const tx = TransactionBuilder.create()
|
|
61
|
+
.setPayer(keypair.publicKey)
|
|
62
|
+
.setRecentBlockhash(blockhash)
|
|
63
|
+
.setTxCreationTime(txCreationTime)
|
|
64
|
+
.addInstruction(transfer)
|
|
65
|
+
.build();
|
|
66
|
+
|
|
67
|
+
const signedTx = tx.sign(keypair);
|
|
68
|
+
|
|
69
|
+
// Send to network
|
|
70
|
+
const signature = await client.sendTransaction(signedTx.serialize());
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Generate and use mnemonics
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
import { Mnemonic } from "@rialo/ts-cdk";
|
|
77
|
+
|
|
78
|
+
// Generate 12-word mnemonic
|
|
79
|
+
const mnemonic = Mnemonic.generate();
|
|
80
|
+
|
|
81
|
+
// Derive keypair (default path: m/44'/756'/0'/0')
|
|
82
|
+
const keypair = await mnemonic.toKeypair();
|
|
83
|
+
|
|
84
|
+
// Use custom derivation path
|
|
85
|
+
const keypair2 = await mnemonic.toKeypair("m/44'/756'/0'/1'");
|
|
86
|
+
|
|
87
|
+
// Restore from existing mnemonic
|
|
88
|
+
const restored = Mnemonic.fromPhrase("your twelve word mnemonic phrase here...");
|
|
89
|
+
const restoredKeypair = await restored.toKeypair();
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Core Modules
|
|
93
|
+
|
|
94
|
+
### Crypto
|
|
95
|
+
|
|
96
|
+
Ed25519 cryptographic primitives for key management and signing.
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
import { Keypair, PublicKey, Signature, Blockhash } from "@rialo/ts-cdk";
|
|
100
|
+
|
|
101
|
+
// Generate random keypair
|
|
102
|
+
const keypair = Keypair.generate();
|
|
103
|
+
|
|
104
|
+
// From existing secret key
|
|
105
|
+
const keypair = Keypair.fromSecretKey(secretBytes);
|
|
106
|
+
|
|
107
|
+
// Public key operations
|
|
108
|
+
const pubkey = PublicKey.fromString("base58string");
|
|
109
|
+
const address = pubkey.toString();
|
|
110
|
+
const bytes = pubkey.toBytes();
|
|
111
|
+
|
|
112
|
+
// Signature operations
|
|
113
|
+
const sig = Signature.fromBytes(signatureBytes);
|
|
114
|
+
|
|
115
|
+
// Blockhash for transactions
|
|
116
|
+
const blockhash = Blockhash.fromString("base58blockhash");
|
|
117
|
+
|
|
118
|
+
// txCreationTime for transactions
|
|
119
|
+
const txCreationTime = BigInt(Date.now());
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Transactions
|
|
123
|
+
|
|
124
|
+
Build and sign transactions with instructions.
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import {
|
|
128
|
+
TransactionBuilder,
|
|
129
|
+
transferInstruction,
|
|
130
|
+
type Instruction,
|
|
131
|
+
type AccountMeta
|
|
132
|
+
} from "@rialo/ts-cdk";
|
|
133
|
+
|
|
134
|
+
// Simple transfer
|
|
135
|
+
const transfer = transferInstruction(from, to, amount);
|
|
136
|
+
|
|
137
|
+
// Custom instruction
|
|
138
|
+
const instruction: Instruction = {
|
|
139
|
+
programId: PublicKey.fromString("YourProgramId"),
|
|
140
|
+
accounts: [
|
|
141
|
+
{ pubkey: account1, isSigner: true, isWritable: true },
|
|
142
|
+
{ pubkey: account2, isSigner: false, isWritable: true },
|
|
143
|
+
],
|
|
144
|
+
data: instructionData, // Uint8Array
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
// Build transaction
|
|
148
|
+
const tx = TransactionBuilder.create()
|
|
149
|
+
.setPayer(payerPublicKey)
|
|
150
|
+
.setRecentBlockhash(blockhash)
|
|
151
|
+
.setTxCreationTime(txCreationTime)
|
|
152
|
+
.addInstruction(instruction)
|
|
153
|
+
.build();
|
|
154
|
+
|
|
155
|
+
// Single signer
|
|
156
|
+
const signed = tx.sign(keypair);
|
|
157
|
+
|
|
158
|
+
// Multi-sig
|
|
159
|
+
const partial = tx.partialSign(signer1);
|
|
160
|
+
const complete = partial.partialSign(signer2);
|
|
161
|
+
|
|
162
|
+
// Serialize for network
|
|
163
|
+
const bytes = signed.serialize();
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### RPC Client
|
|
167
|
+
|
|
168
|
+
Communicate with Rialo blockchain nodes.
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
import { createRialoClient, URL_DEVNET, URL_MAINNET } from "@rialo/ts-cdk";
|
|
172
|
+
|
|
173
|
+
const client = createRialoClient(URL_DEVNET, {
|
|
174
|
+
timeout: 30000,
|
|
175
|
+
maxRetries: 3,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
// Query methods
|
|
179
|
+
const balance = await client.getBalance(publicKey);
|
|
180
|
+
const accountInfo = await client.getAccountInfo(publicKey);
|
|
181
|
+
const blockHeight = await client.getBlockHeight();
|
|
182
|
+
const blockhash = await client.getLatestBlockhash();
|
|
183
|
+
const txInfo = await client.getTransaction(signature);
|
|
184
|
+
|
|
185
|
+
// Send transactions
|
|
186
|
+
const signature = await client.sendTransaction(serializedTx);
|
|
187
|
+
|
|
188
|
+
// Devnet only - request airdrop
|
|
189
|
+
await client.requestAirdrop(publicKey, BigInt(1_000_000_000));
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Signers
|
|
193
|
+
|
|
194
|
+
Interface for different signing strategies.
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
import { KeypairSigner, type Signer } from "@rialo/ts-cdk";
|
|
198
|
+
|
|
199
|
+
// Keypair signer for local signing
|
|
200
|
+
const signer = new KeypairSigner(keypair);
|
|
201
|
+
|
|
202
|
+
// Use signer interface
|
|
203
|
+
const signature = await signer.sign(message);
|
|
204
|
+
const pubkey = signer.publicKey();
|
|
205
|
+
|
|
206
|
+
// Implement custom signer for hardware wallets, etc.
|
|
207
|
+
class CustomSigner implements Signer {
|
|
208
|
+
async sign(message: Uint8Array): Promise<Signature> {
|
|
209
|
+
// your signing logic
|
|
210
|
+
}
|
|
211
|
+
publicKey(): PublicKey {
|
|
212
|
+
// return public key
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## Advanced Features
|
|
218
|
+
|
|
219
|
+
### Hierarchical Deterministic Wallets
|
|
220
|
+
|
|
221
|
+
The SDK uses SLIP-0010 for Ed25519 key derivation with BIP44 paths.
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
// Coin type 756 (R=7, L=5, O=6 on phone keypad)
|
|
225
|
+
const defaultPath = "m/44'/756'/0'/0'";
|
|
226
|
+
|
|
227
|
+
// Derive multiple accounts
|
|
228
|
+
const account0 = await mnemonic.toKeypair(0);
|
|
229
|
+
const account1 = await mnemonic.toKeypair(1);
|
|
230
|
+
const account2 = await mnemonic.toKeypair(2);
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### Transaction Serialization
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
// Serialize signed transaction
|
|
237
|
+
const bytes = signedTx.serialize();
|
|
238
|
+
|
|
239
|
+
// Deserialize
|
|
240
|
+
import { Transaction } from "@rialo/ts-cdk";
|
|
241
|
+
const tx = Transaction.deserialize(bytes);
|
|
242
|
+
|
|
243
|
+
// Check signing status
|
|
244
|
+
const isSigned = tx.isSigned();
|
|
245
|
+
const sigCount = tx.getRequiredSignatureCount();
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### Error Handling
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
import { RialoError, RialoErrorType } from "@rialo/ts-cdk";
|
|
252
|
+
|
|
253
|
+
try {
|
|
254
|
+
await client.sendTransaction(tx);
|
|
255
|
+
} catch (error) {
|
|
256
|
+
if (error instanceof RialoError) {
|
|
257
|
+
switch (error.type) {
|
|
258
|
+
case RialoErrorType.NETWORK:
|
|
259
|
+
// Retry logic
|
|
260
|
+
break;
|
|
261
|
+
case RialoErrorType.INVALID_INPUT:
|
|
262
|
+
// Handle invalid input
|
|
263
|
+
break;
|
|
264
|
+
case RialoErrorType.RPC:
|
|
265
|
+
// RPC specific error
|
|
266
|
+
console.log(error.details); // RPC error details
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
### Keypair Security
|
|
274
|
+
|
|
275
|
+
```typescript
|
|
276
|
+
// Generate keypair
|
|
277
|
+
const keypair = Keypair.generate();
|
|
278
|
+
|
|
279
|
+
// Use for signing
|
|
280
|
+
const signature = keypair.sign(message);
|
|
281
|
+
|
|
282
|
+
// Securely dispose when done
|
|
283
|
+
keypair.dispose(); // Zeros out private key in memory
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
## Network URLs
|
|
287
|
+
|
|
288
|
+
```typescript
|
|
289
|
+
import {
|
|
290
|
+
URL_MAINNET,
|
|
291
|
+
URL_TESTNET,
|
|
292
|
+
URL_DEVNET,
|
|
293
|
+
URL_LOCALNET
|
|
294
|
+
} from "@rialo/ts-cdk";
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
## Constants
|
|
298
|
+
|
|
299
|
+
```typescript
|
|
300
|
+
import {
|
|
301
|
+
KELVIN_PER_RLO, // 1_000_000_000
|
|
302
|
+
SYSTEM_PROGRAM_ID, // "11111111111111111111111111111111"
|
|
303
|
+
PUBLIC_KEY_LENGTH, // 32
|
|
304
|
+
SECRET_KEY_LENGTH, // 32
|
|
305
|
+
SIGNATURE_LENGTH, // 64
|
|
306
|
+
BLOCKHASH_LENGTH, // 32
|
|
307
|
+
} from "@rialo/ts-cdk";
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
## Examples
|
|
311
|
+
|
|
312
|
+
The `examples/` directory contains complete working examples:
|
|
313
|
+
|
|
314
|
+
- `01-basic-operations.ts` - Keypairs, signatures, and basic types
|
|
315
|
+
- `02-wallet-management.ts` - HD wallet patterns (reference implementation)
|
|
316
|
+
- `03-transaction-operations.ts` - Transaction building and signing
|
|
317
|
+
- `05-alice-bob-transaction.ts` - Complete transfer workflow with RPC
|
|
318
|
+
|
|
319
|
+
Run examples:
|
|
320
|
+
```bash
|
|
321
|
+
pnpm tsx examples/01-basic-operations.ts
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
## Development
|
|
325
|
+
|
|
326
|
+
```bash
|
|
327
|
+
# Install dependencies
|
|
328
|
+
pnpm install
|
|
329
|
+
|
|
330
|
+
# Build
|
|
331
|
+
pnpm build
|
|
332
|
+
|
|
333
|
+
# Test
|
|
334
|
+
pnpm test
|
|
335
|
+
|
|
336
|
+
# Lint
|
|
337
|
+
pnpm lint
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
## Security
|
|
341
|
+
|
|
342
|
+
- Private keys stored as `Uint8Array` in memory
|
|
343
|
+
- Uses `@noble/curves` and `@noble/hashes` for cryptography
|
|
344
|
+
- Call `keypair.dispose()` to zero out private keys
|
|
345
|
+
- Never commit private keys or mnemonics
|
|
346
|
+
- Use environment variables for sensitive data
|
|
347
|
+
|
|
348
|
+
## License
|
|
349
|
+
|
|
350
|
+
Apache License 2.0
|