or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

account-management.mdcore-web3.mdens.mdethereum-interaction.mdindex.mdproviders.mdsmart-contracts.mdutilities.md

account-management.mddocs/

0

# Account Management

1

2

The accounts module provides comprehensive functionality for creating, managing, and securing Ethereum accounts. It includes account generation, private key management, transaction signing, message signing, encryption/decryption of keystores, and wallet management with full TypeScript support.

3

4

## Capabilities

5

6

### Account Creation

7

8

Generate new Ethereum accounts with cryptographically secure random private keys.

9

10

```typescript { .api }

11

/**

12

* Create a new account with random private key

13

* @returns Web3Account with address, private key, and signing methods

14

*/

15

create(): Web3Account;

16

17

/**

18

* Create account from existing private key

19

* @param privateKey - Private key as hex string or Uint8Array

20

* @returns Web3Account instance

21

*/

22

privateKeyToAccount(privateKey: Uint8Array | string): Web3Account;

23

24

/**

25

* Extract address from private key

26

* @param privateKey - Private key as hex string or Uint8Array

27

* @returns Ethereum address

28

*/

29

privateKeyToAddress(privateKey: Bytes): string;

30

31

/**

32

* Extract public key from private key

33

* @param privateKey - Private key as hex string or Uint8Array

34

* @param isCompressed - Whether to return compressed public key

35

* @returns Public key as hex string

36

*/

37

privateKeyToPublicKey(privateKey: Bytes, isCompressed: boolean): string;

38

```

39

40

**Usage Examples:**

41

42

```typescript

43

// Create new random account

44

const account = web3.eth.accounts.create();

45

console.log('Address:', account.address);

46

console.log('Private Key:', account.privateKey);

47

48

// Create account from existing private key

49

const privateKey = '0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d';

50

const account = web3.eth.accounts.privateKeyToAccount(privateKey);

51

52

// Get address from private key

53

const address = web3.eth.accounts.privateKeyToAddress(privateKey);

54

55

// Get public key from private key

56

const publicKey = web3.eth.accounts.privateKeyToPublicKey(privateKey, false);

57

```

58

59

### Transaction Signing

60

61

Sign transactions with private keys for secure blockchain submission.

62

63

```typescript { .api }

64

/**

65

* Sign a transaction with private key

66

* @param transaction - Transaction object to sign

67

* @param privateKey - Private key for signing

68

* @returns Signed transaction object

69

*/

70

signTransaction(

71

transaction: Transaction,

72

privateKey: Bytes

73

): Promise<SignedTransactionInfoAPI>;

74

75

/**

76

* Recover transaction sender from signed transaction

77

* @param rawTransaction - Raw signed transaction data

78

* @returns Address of transaction sender

79

*/

80

recoverTransaction(rawTransaction: Bytes): string;

81

```

82

83

**Usage Examples:**

84

85

```typescript

86

// Sign a transaction

87

const transaction = {

88

to: '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984',

89

value: web3.utils.toWei('0.1', 'ether'),

90

gas: 21000,

91

gasPrice: web3.utils.toWei('20', 'gwei'),

92

nonce: 0

93

};

94

95

const signedTx = await web3.eth.accounts.signTransaction(transaction, privateKey);

96

console.log('Signed transaction:', signedTx.rawTransaction);

97

98

// Send signed transaction

99

const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);

100

101

// Recover sender from signed transaction

102

const senderAddress = web3.eth.accounts.recoverTransaction(signedTx.rawTransaction);

103

```

104

105

### Message Signing and Recovery

106

107

Sign arbitrary messages and recover signers for authentication and verification.

108

109

```typescript { .api }

110

/**

111

* Sign a message with private key

112

* @param message - Message to sign

113

* @param privateKey - Private key for signing

114

* @returns Signature object with message hash and signature

115

*/

116

sign(message: string, privateKey: Bytes): SignResult;

117

118

/**

119

* Recover signer address from message and signature

120

* @param signatureObject - Signature object or signature string

121

* @param message - Original message (if signature is string)

122

* @returns Address of message signer

123

*/

124

recover(signatureObject: SignatureObject): string;

125

recover(signature: string, message: string): string;

126

127

/**

128

* Hash a message using Ethereum signed message format

129

* @param message - Message to hash

130

* @returns Message hash

131

*/

132

hashMessage(message: string): string;

133

```

134

135

**Usage Examples:**

136

137

```typescript

138

// Sign a message

139

const message = 'Hello, Web3!';

140

const signature = web3.eth.accounts.sign(message, privateKey);

141

console.log('Signature:', signature.signature);

142

console.log('Message hash:', signature.messageHash);

143

144

// Recover signer address

145

const signerAddress = web3.eth.accounts.recover(signature);

146

console.log('Signer:', signerAddress);

147

148

// Alternative recovery with signature string

149

const recoveredAddress = web3.eth.accounts.recover(signature.signature, message);

150

151

// Hash message manually

152

const messageHash = web3.eth.accounts.hashMessage(message);

153

```

154

155

### Keystore Encryption and Decryption

156

157

Encrypt and decrypt private keys using password-based encryption for secure storage.

158

159

```typescript { .api }

160

/**

161

* Encrypt an account's private key to create keystore

162

* @param privateKey - Private key to encrypt

163

* @param password - Password for encryption

164

* @param options - Encryption options

165

* @returns Encrypted keystore object

166

*/

167

encrypt(privateKey: Bytes, password: string, options?: EncryptOptions): Promise<KeyStore>;

168

169

/**

170

* Decrypt keystore to recover account

171

* @param keystore - Keystore object or JSON string

172

* @param password - Password for decryption

173

* @param options - Decryption options

174

* @returns Decrypted Web3Account

175

*/

176

decrypt(

177

keystore: KeyStore | string,

178

password: string,

179

options?: DecryptOptions

180

): Promise<Web3Account>;

181

```

182

183

**Usage Examples:**

184

185

```typescript

186

// Encrypt account

187

const keystore = await web3.eth.accounts.encrypt(privateKey, 'mySecretPassword');

188

console.log('Keystore:', JSON.stringify(keystore, null, 2));

189

190

// Decrypt keystore

191

const decryptedAccount = await web3.eth.accounts.decrypt(keystore, 'mySecretPassword');

192

console.log('Decrypted address:', decryptedAccount.address);

193

194

// Decrypt from JSON string

195

const keystoreJson = JSON.stringify(keystore);

196

const account = await web3.eth.accounts.decrypt(keystoreJson, 'mySecretPassword');

197

```

198

199

### Wallet Management

200

201

Manage multiple accounts in a convenient wallet interface with bulk operations.

202

203

```typescript { .api }

204

interface Wallet extends Array<Web3Account> {

205

/**

206

* Create and add new account to wallet

207

* @param entropy - Optional entropy for key generation

208

* @returns Created Web3Account

209

*/

210

create(entropy?: string): Web3Account;

211

212

/**

213

* Add account to wallet from private key

214

* @param privateKey - Private key or Web3Account

215

* @returns Added Web3Account

216

*/

217

add(privateKey: string | Web3Account): Web3Account;

218

219

/**

220

* Remove account from wallet

221

* @param addressOrIndex - Account address or index

222

* @returns Boolean indicating success

223

*/

224

remove(addressOrIndex: string | number): boolean;

225

226

/**

227

* Clear all accounts from wallet

228

* @returns Wallet instance

229

*/

230

clear(): Wallet;

231

232

/**

233

* Encrypt all accounts in wallet

234

* @param password - Password for encryption

235

* @returns Array of encrypted keystores

236

*/

237

encrypt(password: string): Promise<KeyStore[]>;

238

239

/**

240

* Decrypt and load accounts from keystores

241

* @param keystores - Array of keystores

242

* @param password - Password for decryption

243

* @returns Wallet instance

244

*/

245

decrypt(keystores: KeyStore[], password: string): Promise<Wallet>;

246

247

/**

248

* Save wallet to local storage (browser only)

249

* @param password - Password for encryption

250

* @param keyName - Local storage key name

251

* @returns Boolean indicating success

252

*/

253

save(password: string, keyName?: string): boolean;

254

255

/**

256

* Load wallet from local storage (browser only)

257

* @param password - Password for decryption

258

* @param keyName - Local storage key name

259

* @returns Wallet instance

260

*/

261

load(password: string, keyName?: string): Wallet;

262

}

263

264

/**

265

* Wallet instance for managing multiple accounts

266

*/

267

wallet: Wallet;

268

```

269

270

**Usage Examples:**

271

272

```typescript

273

// Create accounts in wallet

274

web3.eth.accounts.wallet.create(2); // Create 2 accounts

275

console.log('Wallet size:', web3.eth.accounts.wallet.length);

276

277

// Add existing account

278

web3.eth.accounts.wallet.add(privateKey);

279

280

// Access accounts

281

const firstAccount = web3.eth.accounts.wallet[0];

282

const accountByAddress = web3.eth.accounts.wallet['0x742C1382...'];

283

284

// Remove account

285

web3.eth.accounts.wallet.remove(0); // Remove by index

286

web3.eth.accounts.wallet.remove('0x742C1382...'); // Remove by address

287

288

// Encrypt entire wallet

289

const keystores = await web3.eth.accounts.wallet.encrypt('myPassword');

290

291

// Clear and decrypt wallet

292

web3.eth.accounts.wallet.clear();

293

await web3.eth.accounts.wallet.decrypt(keystores, 'myPassword');

294

295

// Browser storage (if available)

296

web3.eth.accounts.wallet.save('myPassword', 'myWallet');

297

web3.eth.accounts.wallet.load('myPassword', 'myWallet');

298

```

299

300

### Private Key Utilities

301

302

Utility functions for private key validation and manipulation.

303

304

```typescript { .api }

305

/**

306

* Parse and validate private key format

307

* @param data - Private key data

308

* @param ignoreLength - Whether to ignore key length validation

309

* @returns Validated private key as Uint8Array

310

*/

311

parseAndValidatePrivateKey(data: Bytes, ignoreLength?: boolean): Uint8Array;

312

```

313

314

## Types

315

316

```typescript { .api }

317

interface Web3Account {

318

address: string;

319

privateKey: string;

320

321

/**

322

* Sign transaction with this account

323

* @param transaction - Transaction to sign

324

* @returns Signed transaction info

325

*/

326

signTransaction(transaction: Transaction): Promise<SignedTransactionInfoAPI>;

327

328

/**

329

* Sign message with this account

330

* @param data - Message to sign

331

* @returns Signature result

332

*/

333

sign(data: string): SignResult;

334

335

/**

336

* Encrypt this account

337

* @param password - Password for encryption

338

* @returns Encrypted keystore

339

*/

340

encrypt(password: string): Promise<KeyStore>;

341

}

342

343

interface SignResult {

344

message: string;

345

messageHash: string;

346

v: string;

347

r: string;

348

s: string;

349

signature: string;

350

}

351

352

interface SignatureObject {

353

messageHash: string;

354

v: string;

355

r: string;

356

s: string;

357

signature: string;

358

}

359

360

interface SignedTransactionInfoAPI {

361

messageHash: string;

362

v: string;

363

r: string;

364

s: string;

365

rawTransaction: string;

366

transactionHash: string;

367

}

368

369

interface KeyStore {

370

version: number;

371

id: string;

372

address: string;

373

crypto: {

374

ciphertext: string;

375

cipherparams: { iv: string };

376

cipher: string;

377

kdf: string;

378

kdfparams: {

379

dklen: number;

380

salt: string;

381

n: number;

382

r: number;

383

p: number;

384

};

385

mac: string;

386

};

387

}

388

389

interface EncryptOptions {

390

salt?: Uint8Array;

391

iv?: Uint8Array;

392

kdfparams?: {

393

dklen?: number;

394

salt?: string;

395

n?: number;

396

r?: number;

397

p?: number;

398

};

399

}

400

401

interface DecryptOptions {

402

nonStrict?: boolean;

403

}

404

405

type Bytes = string | Uint8Array;

406

```