ElderJS
ElderJS is a JavaScript library designed to enable front-end integration for Rollapps connected to the Elder Ecosystem. It provides tools to interact with Ethereum and Cosmos-based wallets, facilitating transaction creation, signing, and broadcasting within the Elder framework.
Features
Ethereum Wallet Support: Create, sign, and broadcast transactions using Ethereum-based keys.
Cosmos Wallet Support: Integrate with Keplr wallet for Cosmos-based transaction signing and broadcasting.
Elder Ecosystem Integration: Seamlessly connect Rollapps to the Elder ecosystem with custom transaction handling.
TypeScript Support: Fully typed codebase for better developer experience.
Installation
Prerequisites
Node.js (v16 or higher recommended)
npm or yarn
A modern browser with Web3 support (for Ethereum) or Keplr wallet (for Cosmos)
Install via npm
npm install elderjs
Install from GitHub
Clone the repository and install dependencies:
git clone https://github.com/0xElder/elderjs.git
cd elderjs
npm install
Usage
Ethereum Wallet Example
import { eth_getElderMsgAndFeeTxRaw, eth_broadcastTx, eth_getElderAccountInfoFromSignature } from 'elderjs';
import { ethers } from "ethers";
function getWeb3Provider() {
if (window.ethereum) {
return new ethers.BrowserProvider(window.ethereum);
}
return null;
}
const provider = getWeb3Provider();
let message = "Sign to Login";
const signer = await provider.getSigner();
const signature = await signer.signMessage(message);
var { recoveredPublicKey, elderAddr } = await eth_getElderAccountInfoFromSignature(message, signature)
// Example transaction data
const tx = await DUMMY_CONTRACT.METHOD.populateTransaction();
const elderAddress = elderAddr;
const elderPublicKey = recoveredPublicKey;
const gasLimit = 21000;
const elderChainConfig = {
chainName: "elder-testnet",
rpc: "https://rpc.elder.example.com",
rest: "https://rest.elder.example.com",
rollChainID: 1234,
// Elder Roll ID
rollID: 2,
};
// Create and sign transaction
const { tx_hash, rawTx } = await eth_getElderMsgAndFeeTxRaw(
tx,
elderAddress,
elderPublicKey,
gasLimit,
ethers.parseEther("1.0"),
elderChainConfig
);
// Broadcast transaction
// broadcastResult.code == 0 ( for tx success )
const broadcastResult = await eth_broadcastTx(rawTx, elderChainConfig.rpc);
console.log("Transaction Hash:", tx_hash);
console.log("Broadcast Result:", broadcastResult);
Cosmos Wallet Example (with Keplr)
import { cosmos_getElderClient, cosmos_getElderMsgAndFeeTxRaw, getEthereumAddressFromCosmosCompressedPubKey } from 'elderjs';
// Elder chain configuration
const elderChainConfig = {
chainName: "elder-testnet",
rpc: "https://rpc.elder.example.com",
rest: "https://rest.elder.example.com",
rollChainID: 1234,
// Elder Roll ID
rollID: 2,
};
// Connect to Keplr and get client
const { elderAddress, elderClient, elderPublicKey } = await cosmos_getElderClient(elderChainConfig);
const ethAddress = getEthereumAddressFromCosmosCompressedPubKey(elderPublicKey);
// Example transaction data
const tx = await DUMMY_CONTRACT.METHOD.populateTransaction();
// Create and sign transaction
const { tx_hash, rawTx } = await cosmos_getElderMsgAndFeeTxRaw(
tx,
elderAddress,
elderPublicKey,
21000, // gasLimit
ethers.parseEther("1.0"),
1234, // rollChainId
2, // ElderRollID
"elder-testnet" // chainName
);
// Broadcast transaction using the client
// broadcastResult.code == 0 ( for tx success )
const broadcastResult = await elderClient.broadcastTx(rawTx);
console.log("Transaction Hash:", tx_hash);
console.log("Broadcast Result:", broadcastResult);
Project Structure
elderjs/
├── eth_wallet/
│ └── index.js # Ethereum wallet utilities
├── cosmos_wallet/
│ └── index.js # Cosmos wallet utilities (Keplr integration)
├── common/ # Shared utilities and helpers
├── package.json # Project metadata and dependencies
└── README.md # This file
Dependencies
ethers
: Ethereum JavaScript library for transaction handling.@cosmjs/stargate
: Cosmos SDK client for transaction signing and broadcasting.@cosmjs/proto-signing
: Protobuf-based signing utilities for Cosmos.bech32
: Bech32 address encoding/decoding.@noble/hashes
: Cryptographic hash functions.
See package.json
for the full list.
Development
Transpile TypeScript
To transpile TypeScript files (if applicable):
npm run transpile-ts
Contributing
Contributions are welcome! Please follow these steps:
Fork the repository.
Create a feature branch (
git checkout -b feature/your-feature
).Commit your changes (
git commit -m "Add your feature"
).Push to the branch (
git push origin feature/your-feature
).Open a pull request.
License
This project is licensed under the ISC License. See the LICENSE file for details.
Issues
Found a bug? Have a suggestion? Open an issue on the GitHub Issues page.
Contact
For questions or support, reach out via the GitHub repository.
Last updated