### Ubichain Project Setup (Bash) Source: https://github.com/oritwoen/ubichain/blob/main/README.md Provides the necessary bash commands to clone the Ubichain repository, install dependencies using pnpm, and set up the development environment. ```bash git clone https://github.com/oritwoen/ubichain.git cd ubichain pnpm install ``` -------------------------------- ### Install Ubichain Source: https://github.com/oritwoen/ubichain/blob/main/README.md Instructions for installing the Ubichain library using npm, yarn, or pnpm. ```bash npm install ubichain # or yarn add ubichain # or pnpm add ubichain ``` -------------------------------- ### Run BIP32 Demo with tsx Source: https://github.com/oritwoen/ubichain/blob/main/playground/README.md Demonstrates how to execute the BIP32 demo script using the tsx command-line tool. This is useful for running TypeScript examples directly. ```bash npx tsx playground/bip32-demo.ts ``` -------------------------------- ### BIP32 Hierarchical Deterministic Wallet Demo Source: https://github.com/oritwoen/ubichain/blob/main/playground/README.md This TypeScript example demonstrates the BIP32 hierarchical deterministic wallet functionality provided by the ubichain library. It showcases how to generate and manage hierarchical wallets. ```typescript // Placeholder for bip32-demo.ts content // This file demonstrates BIP32 hierarchical deterministic wallet functionality. ``` -------------------------------- ### Run Playground Examples (Bash) Source: https://github.com/oritwoen/ubichain/blob/main/README.md Executes various demonstration scripts for cryptographic functionalities like BIP32, SLIP-0010, BIP39, and BIP44. Also allows running custom TypeScript files from the playground directory. ```bash pnpm playground:bip32 # Run BIP32 demo pnpm playground:slip10 # Run SLIP-0010 demo pnpm playground:bip39 # Run BIP39 demo pnpm playground:bip44 # Run BIP44 demo pnpm playground # Run any TypeScript file in playground folder ``` -------------------------------- ### Generate Solana Wallet Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/4.wallets.md Example of generating a Solana wallet using Ubichain, logging the private key, public key, and the Solana address. ```javascript const solanaChain = useBlockchain(solana()); const solWallet = solanaChain.generateWallet(); console.log('Solana Private Key:', solWallet.keys.private); console.log('Solana Public Key:', solWallet.keys.public); console.log('Solana Address:', solWallet.address); ``` -------------------------------- ### Ubichain Lazy Loading Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/1.index.md Illustrates the lazy loading mechanism in ubichain, where blockchain implementations are loaded on-demand. This example shows how to load the Bitcoin implementation and optionally configure it for a testnet network. ```js // The blockchain implementation is only loaded when you call this function const bitcoinImpl = await blockchains.bitcoin()(); // You can pass options to configure the blockchain const testnetImpl = await blockchains.bitcoin({ network: 'testnet' })(); ``` -------------------------------- ### Generate Bitcoin Wallet Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/4.wallets.md Example of generating a Bitcoin-specific wallet using Ubichain, logging the private key, public key, and the Bitcoin address. ```javascript const bitcoinChain = useBlockchain(bitcoin()); const btcWallet = bitcoinChain.generateWallet(); console.log('Bitcoin Private Key:', btcWallet.keys.private); console.log('Bitcoin Public Key:', btcWallet.keys.public); console.log('Bitcoin Address:', btcWallet.address); // Starts with '1' ``` -------------------------------- ### Test Custom Blockchain Implementation Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/6.custom.md Provides an example of how to test a custom blockchain implementation (MyChain) using Vitest, verifying basic properties like name and curve. ```js // test/blockchains/mychain.test.ts import { describe, expect, it } from "vitest"; import { useBlockchain } from "../../src"; import mychain from "../../src/blockchains/mychain"; describe("MyChain blockchain", () => { const blockchain = useBlockchain(mychain()); it("should have a name", () => { expect(blockchain.name).toBe("mychain"); }); it("should use secp256k1 curve", () => { expect(blockchain.curve).toBe("secp256k1"); }); // Test key and address generation // ... }); ``` -------------------------------- ### Basic Ubichain Usage Source: https://github.com/oritwoen/ubichain/blob/main/README.md Demonstrates how to import and use blockchain implementations from the Ubichain library. It shows how to lazily load blockchain interfaces for Bitcoin, Ethereum, and Solana, and then use the `useBlockchain` hook to get blockchain-specific functionalities like the cryptographic curve. ```typescript import { useBlockchain, blockchains } from 'ubichain'; // Using lazy factories for better performance and tree-shaking // Create blockchain interfaces with lazy loading const bitcoinImpl = await blockchains.bitcoin()(); const ethereumImpl = await blockchains.ethereum()(); const solanaImpl = await blockchains.solana()(); const bitcoinChain = useBlockchain(bitcoinImpl); const ethereumChain = useBlockchain(ethereumImpl); const solanaChain = useBlockchain(solanaImpl); // Get cryptographic curve used by the blockchain console.log('Bitcoin uses curve:', bitcoinChain.curve); // secp256k1 console.log('Ethereum uses curve:', ethereumChain.curve); // secp256k1 console.log('Solana uses curve:', solanaChain.curve); // ed25519 ``` -------------------------------- ### Generate Bitcoin Wallet and Addresses Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/bitcoin.md Provides examples for generating a new Bitcoin wallet, including private key, public key, and different address types (Legacy, P2SH, SegWit v0, Taproot). ```js const wallet = bitcoinChain.generateWallet(); console.log('Private Key:', wallet.keys.private); console.log('Public Key:', wallet.keys.public); console.log('Address:', wallet.address); // Legacy address starting with '1' ``` ```js // Generate a P2SH wallet const p2shWallet = bitcoinChain.generateWallet({}, 'p2sh'); console.log('P2SH Address:', p2shWallet.address); // Starts with '3' // Or get a P2SH address from a public key const publicKey = bitcoinChain.getKeyPublic(privateKey); const p2shAddress = bitcoinChain.getAddress(publicKey, 'p2sh'); ``` ```js // Generate a SegWit v0 wallet const segwitWallet = bitcoinChain.generateWallet({}, 'segwit'); console.log('SegWit v0 Address:', segwitWallet.address); // Starts with 'bc1q' // Or get a SegWit v0 address from a public key const publicKey = bitcoinChain.getKeyPublic(privateKey); const segwitAddress = bitcoinChain.getAddress(publicKey, 'segwit'); ``` ```js // Generate a Taproot wallet const taprootWallet = bitcoinChain.generateWallet({}, 'taproot'); console.log('Taproot Address:', taprootWallet.address); // Starts with 'bc1p' // Or get a Taproot address from a public key const publicKey = bitcoinChain.getKeyPublic(privateKey); const taprootAddress = bitcoinChain.getAddress(publicKey, 'taproot'); ``` -------------------------------- ### Create EVM Chain File Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/5.evm.md Example of creating a new file for an EVM chain (e.g., Polygon) in the 'blockchains' directory. This function utilizes a utility to create an EVM blockchain instance. ```typescript import { createEVMBlockchain } from '../utils/evm'; export default function polygon() { return createEVMBlockchain("polygon"); } ``` -------------------------------- ### MyChain Blockchain Implementation Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/6.custom.md A concrete example of a custom blockchain implementation for 'MyChain'. It uses secp256k1 curve and implements address generation using SHA-256, RIPEMD-160 hashing, and Base58Check encoding. ```javascript // blockchains/mychain.ts import { generateKeyPublic as getSecp256k1KeyPublic } from '../utils/secp256k1'; import { hexToBytes, bytesToHex } from '@noble/hashes/utils'; import { sha256 } from '@noble/hashes/sha256'; import { ripemd160 } from '@noble/hashes/ripemd160'; import { encodeBase58Check } from '../utils/encoding'; import type { Curve } from '../types'; export default function mychain() { const name = "mychain"; const curve: Curve = "secp256k1"; function getKeyPublic(keyPrivate: string, options?: Record): string { return getSecp256k1KeyPublic(keyPrivate, options); } function getAddress(keyPublic: string): string { // Convert public key to bytes const keyPublicBytes = hexToBytes(keyPublic); // Hash the public key (SHA-256 followed by RIPEMD-160) const hash = ripemd160(sha256(keyPublicBytes)); // Add version byte (0x4D for "M" prefix) const hashVersioned = new Uint8Array(hash.length + 1); hashVersioned[0] = 0x4D; hashVersioned.set(hash, 1); // Encode with Base58Check return encodeBase58Check(hashVersioned); } function validateAddress(address: string): boolean { // Basic validation: check it starts with "M" and has correct length if (!address.startsWith('M')) { return false; } try { // Try to decode the address (will throw if invalid) // Full implementation would do more thorough validation return address.length >= 26 && address.length <= 35; } catch (error) { return false; } } return { name, curve, getKeyPublic, getAddress, validateAddress, }; } ``` -------------------------------- ### EVM Address Format and EIP-55 Checksum Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/5.evm.md Illustrates the standard format for EVM addresses, which are hex-encoded, start with '0x', are 42 characters long, and support case-sensitive EIP-55 checksums. It shows examples of both lowercase and EIP-55 checksummed addresses. ```js // Lowercase address (valid but doesn't include checksum) const lowercaseAddress = '0x7e5f4552091a69125d5dfcb7b8c2659029395bdf'; // EIP-55 checksummed address (same address with checksum) const checksumAddress = '0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf'; ``` -------------------------------- ### Ubichain Development Commands (Bash) Source: https://github.com/oritwoen/ubichain/blob/main/README.md Lists the essential bash commands for developing with the Ubichain library, including running tests, building the library, and linting the code. ```bash # Run tests pnpm run dev # Build the library pnpm run build # Lint the code pnpm run lint ``` -------------------------------- ### TRON Address Validation Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/tron.md Provides examples of validating both correct and incorrect TRON addresses. ```javascript // Validate a TRON address const isValid = tronChain.validateAddress?.('TJCnKsPa7y5okkXvQAidZBzqx3QyQ6sxMW'); console.log('Is valid:', isValid); // true // Invalid address (wrong prefix) const isInvalid = tronChain.validateAddress?.('1JCnKsPa7y5okkXvQAidZBzqx3QyQ6sxMW'); console.log('Is valid:', isInvalid); // false (doesn't start with T) ``` -------------------------------- ### Aptos Blockchain Usage Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/aptos.md Demonstrates how to import and initialize the Aptos blockchain driver from the ubichain library. ```javascript import { useBlockchain } from 'ubichain'; import aptos from 'ubichain/blockchains/aptos'; const aptosChain = useBlockchain(aptos()); ``` -------------------------------- ### Validate Ethereum Address Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/ethereum.md Provides examples of validating Ethereum addresses, including checksummed and lowercase formats. ```js // Validate a checksummed address const isValidChecksum = ethereumChain.validateAddress?.('0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf'); // Validate a lowercase address (also valid) const isValidLowercase = ethereumChain.validateAddress?.('0x7e5f4552091a69125d5dfcb7b8c2659029395bdf'); // Invalid checksum (wrong case) const isInvalidChecksum = ethereumChain.validateAddress?.('0x7e5F4552091A69125d5DfCb7b8C2659029395Bdf'); ``` -------------------------------- ### Initialize Ethereum Blockchain Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/ethereum.md Demonstrates how to import and initialize the Ethereum blockchain driver from the Ubichain library. ```js import { useBlockchain } from 'ubichain'; import ethereum from 'ubichain/blockchains/ethereum'; const ethereumChain = useBlockchain(ethereum()); ``` -------------------------------- ### Validate Cardano Address Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/cardano.md Provides an example of how to validate a given Cardano address string to check its format and correctness. ```javascript // Validate an address const isValid = cardanoBlockchain.validateAddress('addr1q9kytfmxk3vdze7s5prpnrjl6j3qldqssvn7mkcpnpvd2p0ltsyswunewxmf58504d9tkqelz2vf02w0msgtvcuzdmsdhq0z4'); // true or false ``` -------------------------------- ### Initialize Base Blockchain Driver Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/base.md Demonstrates how to import and initialize the Base blockchain driver using the useBlockchain hook from the Ubichain library. ```javascript import { useBlockchain } from 'ubichain'; import base from 'ubichain/blockchains/base'; const baseChain = useBlockchain(base()); ``` -------------------------------- ### Validate Base Address Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/base.md Provides examples of validating Base addresses, including correctly checksummed, lowercase, and incorrectly checksummed addresses. ```javascript // Validate a checksummed address const isValidChecksum = baseChain.validateAddress?.('0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf'); console.log('Is valid:', isValidChecksum); // true // Validate a lowercase address (also valid) const isValidLowercase = baseChain.validateAddress?.('0x7e5f4552091a69125d5dfcb7b8c2659029395bdf'); console.log('Is valid:', isValidLowercase); // true // Invalid checksum (wrong case) const isInvalidChecksum = baseChain.validateAddress?.('0x7e5F4552091A69125d5DfCb7b8C2659029395Bdf'); console.log('Is valid:', isInvalidChecksum); // false ``` -------------------------------- ### Run Tests Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/6.custom.md Command to execute the tests for the blockchain implementation using pnpm. ```bash pnpm dev ``` -------------------------------- ### Working with Bitcoin Keys and Addresses Source: https://github.com/oritwoen/ubichain/blob/main/README.md Demonstrates generating private keys, public keys, and various Bitcoin addresses (legacy, P2SH, P2WSH, Taproot) for both mainnet and testnet using Ubichain. ```typescript import { useBlockchain, blockchains } from 'ubichain'; // Mainnet blockchain instance (default) const bitcoinImpl = await blockchains.bitcoin()(); const chain = useBlockchain(bitcoinImpl); // Testnet blockchain instance const testnetImpl = await blockchains.bitcoin({ network: 'testnet' })(); const testnet = useBlockchain(testnetImpl); // Generate a single private key const privateKey = chain.generateKeyPrivate(); console.log('Private Key:', privateKey); // Get a public key from a private key const publicKey = chain.getKeyPublic(privateKey); console.log('Public Key:', publicKey); // Generate an uncompressed public key const uncompressedPublicKey = chain.getKeyPublic(privateKey, { compressed: false }); console.log('Uncompressed Public Key:', uncompressedPublicKey); // Generate mainnet addresses const address = chain.getAddress(publicKey); console.log('Mainnet Legacy Address:', address); // Generate different types of Bitcoin mainnet addresses const p2shAddress = chain.getAddress(publicKey, 'p2sh'); const p2wshAddress = chain.getAddress(publicKey, 'p2wsh'); const taprootAddress = chain.getAddress(publicKey, 'taproot'); console.log('Mainnet P2SH Address:', p2shAddress); console.log('Mainnet P2WSH Address:', p2wshAddress); console.log('Mainnet Taproot Address:', taprootAddress); // Generate testnet addresses const testnetAddress = testnet.getAddress(publicKey); console.log('Testnet Legacy Address:', testnetAddress); const testnetSegwit = testnet.getAddress(publicKey, 'segwit'); console.log('Testnet SegWit Address:', testnetSegwit); // Validate an address const isValid = chain.validateAddress?.(address); console.log('Is Valid Mainnet Address:', isValid); // Validate testnet address const isValidTestnet = testnet.validateAddress?.(testnetAddress); console.log('Is Valid Testnet Address:', isValidTestnet); ``` -------------------------------- ### Solana Blockchain Usage Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/solana.md Demonstrates how to import and initialize the Solana blockchain driver within the Ubichain framework. ```javascript import { useBlockchain } from 'ubichain'; import solana from 'ubichain/blockchains/solana'; const solanaChain = useBlockchain(solana()); ``` -------------------------------- ### Ubichain Address Validation Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/1.index.md Demonstrates how to validate blockchain addresses using ubichain's `validateAddress` function. This example checks the validity of a standard Bitcoin address. ```js // Validate an address if (bitcoinChain.validateAddress?.('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa')) { console.log('Address is valid'); } ``` -------------------------------- ### Initialize Bitcoin Blockchain Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/bitcoin.md Demonstrates how to import and initialize the Bitcoin blockchain driver within the Ubichain framework. Requires the 'ubichain' package. ```js import { useBlockchain } from 'ubichain'; import bitcoin from 'ubichain/blockchains/bitcoin'; const bitcoinChain = useBlockchain(bitcoin()); ``` -------------------------------- ### Generate a Complete Wallet Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/4.wallets.md Demonstrates how to generate a new cryptocurrency wallet, including private key, public key, and address, using the ubichain library. ```javascript import { useBlockchain } from 'ubichain'; import bitcoin from 'ubichain/blockchains/bitcoin'; const chain = useBlockchain(bitcoin()); const wallet = chain.generateWallet(); console.log('Private Key:', wallet.keys.private); console.log('Public Key:', wallet.keys.public); console.log('Address:', wallet.address); ``` -------------------------------- ### Generate TRON Address Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/3.addresses.md Generates a TRON address from a public key. TRON addresses are Base58Check encoded, starting with 'T', and are derived using Keccak-256 hashing. Requires the TRON blockchain module. ```js const tronChain = useBlockchain(tron()); const publicKey = tronChain.getKeyPublic(privateKey); const address = tronChain.getAddress(publicKey); // 'T...' ``` -------------------------------- ### TRON Blockchain Initialization Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/tron.md Demonstrates how to import and initialize the TRON blockchain driver using the useBlockchain hook from ubichain. ```javascript import { useBlockchain } from 'ubichain'; import tron from 'ubichain/blockchains/tron'; const tronChain = useBlockchain(tron()); ``` -------------------------------- ### Import Existing Private Key Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/4.wallets.md Illustrates how to create a wallet object manually by importing an existing private key and deriving the public key and address using Ubichain functions. ```javascript const privateKey = '7f9e5b9e3bbed34a4c28c8c1665525fc2cd7afb4fdc7edca3eb93ddf8a31ef56'; // Generate the public key const publicKey = chain.getKeyPublic(privateKey); // Generate the address const address = chain.getAddress(publicKey); // Create a wallet object manually const wallet = { keys: { private: privateKey, public: publicKey }, address }; console.log('Imported Wallet:', wallet); ``` -------------------------------- ### Initialize SUI Blockchain Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/sui.md Imports and initializes the SUI blockchain instance using the useBlockchain hook from Ubichain. ```js import { useBlockchain } from 'ubichain'; import sui from 'ubichain/blockchains/sui'; const suiChain = useBlockchain(sui()); ``` -------------------------------- ### Validate EVM Address with EIP-55 Checksum Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/5.evm.md Provides examples of validating EVM addresses using Ubichain's `validateAddress` function. It shows successful validation for correctly checksummed addresses (mixed-case) and lowercase addresses, while also illustrating how invalid checksums are rejected. ```js // Validate an address const isValid = ethereumChain.validateAddress?.('0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf'); // Different case with invalid checksum const isInvalid = ethereumChain.validateAddress?.('0x7e5F4552091A69125d5DfCb7b8C2659029395Bdf'); ``` -------------------------------- ### Generating Ethereum and Bitcoin Wallets Source: https://github.com/oritwoen/ubichain/blob/main/README.md Shows how to generate complete wallets (private key, public key, address) for Ethereum and Bitcoin, including options for compressed public keys and specific Bitcoin address types. ```typescript import { useBlockchain, blockchains } from 'ubichain'; const ethereumImpl = await blockchains.ethereum()(); const chain = useBlockchain(ethereumImpl); // Generate a key pair (private and public keys) const keys = chain.generateKeys(); console.log('Private Key:', keys.keys.private); console.log('Public Key:', keys.keys.public); // Generate a complete wallet (private key, public key, and address) const wallet = chain.generateWallet(); console.log('Private Key:', wallet.keys.private); console.log('Public Key:', wallet.keys.public); console.log('Address:', wallet.address); // Generate wallet with specific options const uncompressedWallet = chain.generateWallet({ compressed: false }); console.log('Uncompressed Public Key:', uncompressedWallet.keys.public); // Generate Bitcoin wallet with specific address type const bitcoinImpl = await blockchains.bitcoin()(); const bitcoinChain = useBlockchain(bitcoinImpl); const p2shWallet = bitcoinChain.generateWallet({}, 'p2sh'); console.log('Bitcoin P2SH Address:', p2shWallet.address); // Starts with '3' ``` -------------------------------- ### Get Public Key from Private Key (JavaScript) Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/2.keys.md Derives a public key from a given private key using the blockchain's cryptographic curve. Supports generating both compressed (default) and uncompressed public keys. Requires an initialized blockchain instance and the private key string. ```js const publicKey = chain.getKeyPublic(privateKey); console.log(publicKey); // e.g., '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc' ``` ```js const uncompressedPublicKey = chain.getKeyPublic(privateKey, { compressed: false }); console.log(uncompressedPublicKey); // e.g., '04a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc7513...' ``` -------------------------------- ### Generate Ethereum Wallet Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/4.wallets.md Demonstrates generating an Ethereum wallet with Ubichain, displaying the private key, public key, and the Ethereum address. ```javascript const ethereumChain = useBlockchain(ethereum()); const ethWallet = ethereumChain.generateWallet(); console.log('Ethereum Private Key:', ethWallet.keys.private); console.log('Ethereum Public Key:', ethWallet.keys.public); console.log('Ethereum Address:', ethWallet.address); // Starts with '0x' ``` -------------------------------- ### Ubichain Project Roadmap (TypeScript Context) Source: https://github.com/oritwoen/ubichain/blob/main/README.md Outlines the development progress and future plans for the Ubichain project, including added support for various blockchain standards, key management features, and planned enhancements. ```typescript // Completed Features: // Add support for Ethereum and EIP-55 checksums // Add support for Base (and other EVM chains) // Add key pair generation in one step // Add wallet generation in one step // Create hierarchical data model for keys and wallets // Add SegWit (bech32) address support // Add SegWit v1 (bech32m/Taproot) address support // Add P2WSH address support // Add Testnet address support // Add HD wallet support for secp256k1 chains (BIP32) // Add SLIP-0010 support for ed25519 chains (Solana, Aptos, etc.) // Add support for BIP39 mnemonic phrases // Add support for BIP44 derivation paths // Add lazy loading for blockchain implementations // Future Features: // Add transaction creation and signing // Add support for more EVM blockchains (Polygon, Arbitrum, Optimism, etc.) // Add support for additional blockchains (Polkadot, Cosmos, etc.) ``` -------------------------------- ### Ubichain Wallet Generation Convenience Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/1.index.md Provides a simplified method for generating a complete wallet, including the private key, public key, and address, in a single step using ubichain. ```js const wallet = bitcoinChain.generateWallet(); console.log('Private Key:', wallet.keys.private); console.log('Public Key:', wallet.keys.public); console.log('Address:', wallet.address); ``` -------------------------------- ### Ubichain Wallet Generation Implementation Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/4.wallets.md Provides a glimpse into the internal implementation of the `generateWallet` function in Ubichain, showing how keys are generated and an address is derived. ```javascript function generateWallet(options?, addressType?) { const keyPair = generateKeys(options); const address = blockchain.getAddress(keyPair.keys.public, addressType); return { ...keyPair, address }; } ``` -------------------------------- ### Registering a New Blockchain Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/6.custom.md Demonstrates how to register a newly created blockchain implementation by adding its path to the `blockchains` object in `_blockchains.ts`. ```javascript export const blockchains = Object.freeze({ 'bitcoin': 'ubichain/blockchains/bitcoin', 'ethereum': 'ubichain/blockchains/ethereum', // ... other blockchains 'mychain': 'ubichain/blockchains/mychain', }); ``` -------------------------------- ### Using Shared EVM Blockchain Implementation Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/6.custom.md Shows how to leverage the `createEVMBlockchain` factory function for creating implementations of EVM-compatible blockchains, such as Arbitrum. ```javascript // blockchains/arbitrum.ts import { createEVMBlockchain } from '../utils/evm'; export default function arbitrum() { return createEVMBlockchain("arbitrum"); } ``` -------------------------------- ### Import Cardano Blockchain Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/cardano.md Demonstrates how to import and initialize the Cardano blockchain functionality from the ubichain library. ```javascript import { useBlockchain } from 'ubichain'; import cardano from 'ubichain/blockchains/cardano'; const cardanoBlockchain = useBlockchain(cardano()); ``` -------------------------------- ### Basic Ubichain Wallet Generation Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/1.index.md Demonstrates how to use ubichain to generate wallets for Bitcoin and Ethereum. It utilizes lazy factories for efficient loading of blockchain implementations and shows how to derive addresses from generated wallets. ```js import { useBlockchain, blockchains } from 'ubichain'; // Using lazy factories for better performance and tree-shaking // Create blockchain interfaces with lazy loading const bitcoinImpl = await blockchains.bitcoin()(); const ethereumImpl = await blockchains.ethereum()(); const bitcoinChain = useBlockchain(bitcoinImpl); const ethereumChain = useBlockchain(ethereumImpl); // Generate a complete wallet for each blockchain const btcWallet = bitcoinChain.generateWallet(); const ethWallet = ethereumChain.generateWallet(); console.log('Bitcoin Address:', btcWallet.address); console.log('Ethereum Address:', ethWallet.address); ``` -------------------------------- ### Generate Wallet with Options Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/4.wallets.md Shows how to customize wallet generation by passing options, such as specifying an uncompressed public key or a specific address type like P2SH for Bitcoin. ```javascript // Generate a wallet with an uncompressed public key const uncompressedWallet = chain.generateWallet({ compressed: false }); // Generate a Bitcoin P2SH wallet const bitcoinChain = useBlockchain(bitcoin()); const p2shWallet = bitcoinChain.generateWallet({}, 'p2sh'); console.log('P2SH Address:', p2shWallet.address); // Starts with '3' ``` -------------------------------- ### HD Wallet Derivation (TypeScript) Source: https://github.com/oritwoen/ubichain/blob/main/README.md Demonstrates how to generate and derive private keys for Bitcoin, Ethereum, and Solana using mnemonic phrases, BIP39, BIP32, and SLIP-10 standards. It shows how to create blockchain instances and generate wallet addresses from derived keys. ```typescript import { useBlockchain, blockchains, getBIP44Path, BIP44 } from 'ubichain'; import { mnemonicToSeed } from 'ubichain/utils/bip39'; import { getMasterKeyFromSeed, deriveHDKey } from 'ubichain/utils/bip32'; import { getMasterKeyFromSeed as slip10MasterKey, deriveHDKey as slip10Derive } from 'ubichain/utils/slip10'; // Generate a mnemonic phrase (or use your existing one) // import { generateMnemonic } from 'ubichain/utils/bip39'; // const mnemonic = generateMnemonic(); const mnemonic = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'; // Convert mnemonic to seed const seed = mnemonicToSeed(mnemonic); // Get master key from seed const masterKey = getMasterKeyFromSeed(seed); // Create blockchain instances const bitcoinImpl = await blockchains.bitcoin()(); const ethereumImpl = await blockchains.ethereum()(); const solanaImpl = await blockchains.solana()(); const bitcoinChain = useBlockchain(bitcoinImpl); const ethereumChain = useBlockchain(ethereumImpl); const solanaChain = useBlockchain(solanaImpl); // Get BIP44 paths for different blockchains const bitcoinPath = getBIP44Path(BIP44.BITCOIN); // m/44'/0'/0'/0/0 const ethereumPath = getBIP44Path(BIP44.ETHEREUM); // m/44'/60'/0'/0/0 const solanaPath = getBIP44Path(BIP44.SOLANA); // m/44'/501'/0'/0/0 // Derive keys for Bitcoin const bitcoinKey = deriveHDKey(masterKey, bitcoinPath); const bitcoinPrivateKey = Buffer.from(bitcoinKey.privateKey!).toString('hex'); console.log('Bitcoin Derived Private Key:', bitcoinPrivateKey); // Derive keys for Ethereum const ethereumKey = deriveHDKey(masterKey, ethereumPath); const ethereumPrivateKey = Buffer.from(ethereumKey.privateKey!).toString('hex'); console.log('Ethereum Derived Private Key:', ethereumPrivateKey); // Generate wallets from derived private keys const bitcoinWallet = bitcoinChain.generateWallet({ privateKey: bitcoinPrivateKey }); const ethereumWallet = ethereumChain.generateWallet({ privateKey: ethereumPrivateKey }); console.log('Bitcoin Address:', bitcoinWallet.address); console.log('Ethereum Address:', ethereumWallet.address); // For ed25519 curves (like Solana), use SLIP-10 instead of BIP32 // Derive ed25519 keys using SLIP-10 const slip10Master = slip10MasterKey(seed); const solanaKey = slip10Derive(slip10Master, solanaPath, true); const solanaPrivateKey = Buffer.from(solanaKey.privateKey).toString('hex'); console.log('Solana Derived Private Key:', solanaPrivateKey); ``` -------------------------------- ### Create EVM Blockchain Implementation Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/5.evm.md Demonstrates how to create a new EVM blockchain implementation using the `createEVMBlockchain` utility. This function is central to defining new EVM-compatible chains within Ubichain, ensuring consistent behavior for key generation and address derivation. ```js import { createEVMBlockchain } from '../utils/evm'; export default function ethereum() { return createEVMBlockchain("ethereum"); } ``` -------------------------------- ### Generate Ethereum Wallet Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/ethereum.md Shows how to generate a new Ethereum wallet, including its private key, public key, and address. ```js const wallet = ethereumChain.generateWallet(); console.log('Private Key:', wallet.keys.private); console.log('Public Key:', wallet.keys.public); console.log('Address:', wallet.address); // 0x-prefixed address with EIP-55 checksum ``` -------------------------------- ### Basic Blockchain Implementation Structure Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/6.custom.md Illustrates the fundamental structure for implementing a new blockchain. It shows how to define the blockchain's name, curve, and the required key derivation and address generation functions. ```javascript import { generateKeyPublic as getSecp256k1KeyPublic } from '../utils/secp256k1'; import type { Curve } from '../types'; export default function myBlockchain() { const name = "myblockchain"; const curve: Curve = "secp256k1"; function getKeyPublic(keyPrivate: string, options?: Record): string { // Implement key derivation logic return getSecp256k1KeyPublic(keyPrivate, options); } function getAddress(keyPublic: string, type?: string): string { // Implement address derivation logic return /* derived address */; } function validateAddress(address: string): boolean { // Implement address validation logic return /* validation result */; } return { name, curve, getKeyPublic, getAddress, validateAddress, }; } ``` -------------------------------- ### Generate Bitcoin Legacy and P2SH Addresses with Key Derivation Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/3.addresses.md Shows how to derive a public key from a private key and then generate both legacy (P2PKH) and P2SH addresses for Bitcoin using Ubichain. ```js const bitcoinChain = useBlockchain(bitcoin()); const publicKey = bitcoinChain.getKeyPublic(privateKey); // Legacy address (P2PKH) const legacyAddress = bitcoinChain.getAddress(publicKey); // '1...' // P2SH address const p2shAddress = bitcoinChain.getAddress(publicKey, 'p2sh'); // '3...' ``` -------------------------------- ### Ethereum Key Generation Details Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/ethereum.md Details the format and derivation of private and public keys for Ethereum. ```APIDOC Key Generation: - Private Key: - 32 random bytes (256 bits). - Represented as a 64-character hex string. - Public Key: - Derived from the private key using the secp256k1 elliptic curve. - Compressed Format: 33 bytes (66 hex chars), starting with 02 or 03. - Uncompressed Format: 65 bytes (130 hex chars), starting with 04. ``` -------------------------------- ### Base EVM Blockchain Creation Utility Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/base.md Shows the internal utility function used to create an EVM-compatible blockchain instance, specifically for the 'base' chain. ```typescript import { createEVMBlockchain } from '../utils/evm'; export default function base() { return createEVMBlockchain("base"); } ``` -------------------------------- ### Ubichain Key Generation Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/1.index.md Demonstrates various methods for generating cryptographic keys using ubichain. It covers generating a private key, deriving a public key from a private key, and generating a complete key pair. ```js // Generate a private key const privateKey = bitcoinChain.generateKeyPrivate(); // Generate a public key from a private key const publicKey = bitcoinChain.getKeyPublic(privateKey); // Generate a key pair (private and public keys) in one step const keys = bitcoinChain.generateKeys(); ``` -------------------------------- ### EVM Blockchain Wallet Generation (TypeScript) Source: https://github.com/oritwoen/ubichain/blob/main/README.md Illustrates how to generate wallets for EVM-compatible blockchains like Ethereum and Base using the Ubichain library. It highlights that the same private key generates the same public key and address across these chains. ```typescript import { useBlockchain, blockchains } from 'ubichain'; // EVM blockchains share the same address format const ethereumImpl = await blockchains.ethereum()(); const baseImpl = await blockchains.base()(); const ethereumChain = useBlockchain(ethereumImpl); const baseChain = useBlockchain(baseImpl); // Generate wallets for both chains const ethWallet = ethereumChain.generateWallet(); const baseWallet = baseChain.generateWallet(); console.log('Ethereum Address:', ethWallet.address); console.log('Base Address:', baseWallet.address); // Same private key will generate same address on both chains const privateKey = ethereumChain.generateKeyPrivate(); const ethPublicKey = ethereumChain.getKeyPublic(privateKey); const basePublicKey = baseChain.getKeyPublic(privateKey); console.log('Same public key?', ethPublicKey === basePublicKey); // true const ethAddress = ethereumChain.getAddress(ethPublicKey); const baseAddress = baseChain.getAddress(basePublicKey); console.log('Same address?', ethAddress === baseAddress); // true ``` -------------------------------- ### Ethereum Address Generation Logic Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/ethereum.md Explains the technical steps involved in generating an Ethereum address from a public key, including Keccak-256 hashing and EIP-55 checksum application. ```APIDOC Ethereum Address Generation: 1. Public Key Processing: - Decompress compressed public keys (33 bytes) to uncompressed (65 bytes). - Remove the first byte (0x04) from the uncompressed public key. 2. Address Derivation: - `hash = Keccak-256(publicKeyWithoutPrefix)` - `address = '0x' + last20Bytes(hash)` (EIP-55 checksum applied) 3. EIP-55 Checksum: - `lowercase = address without '0x' prefix, in lowercase` - `hash = Keccak-256(lowercase)` - Iterate through the address characters: - If the corresponding hash character (hex) is >= 8, make the address character uppercase. - Otherwise, keep the address character lowercase. - Result is the checksummed address. ``` -------------------------------- ### Ubichain Address Generation Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/1.index.md Explains how to generate blockchain addresses from public keys using ubichain. It shows the basic address generation and how to specify different address formats, like P2SH for Bitcoin. ```js // Generate an address from a public key const address = bitcoinChain.getAddress(publicKey); // Some blockchains support multiple address formats const p2shAddress = bitcoinChain.getAddress(publicKey, 'p2sh'); ``` -------------------------------- ### Base Implementation Source Code Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/base.md Highlights the source file location for the Base blockchain implementation and its dependency on the common EVM utility. ```typescript // Base implementation import { createEVMBlockchain } from '../utils/evm'; export default function base() { return createEVMBlockchain("base"); } ``` -------------------------------- ### Generate and Log Bitcoin Address Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/3.addresses.md Generates a Bitcoin key pair and derives the legacy address from the public key, then logs it to the console. Requires the 'ubichain' library and the Bitcoin blockchain module. ```js import { useBlockchain } from 'ubichain'; import bitcoin from 'ubichain/blockchains/bitcoin'; const chain = useBlockchain(bitcoin()); const keys = chain.generateKeys(); const address = chain.getAddress(keys.keys.public); console.log(address); // e.g., '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa' ``` -------------------------------- ### Register New EVM Chain Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/5.evm.md Demonstrates how to add the newly created EVM chain to the central list of blockchains in '_blockchains.ts'. This makes the new chain discoverable and usable within the project. ```typescript export const blockchains = Object.freeze({ // Existing blockchains 'polygon': 'ubichain/blockchains/polygon', }); ``` -------------------------------- ### Generate Same Address Across EVM Chains Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/5.evm.md Shows how the same private key generates the identical address on different EVM chains (e.g., Ethereum and Base) within Ubichain. This highlights the interoperability and consistency provided by the common EVM implementation. ```js import { useBlockchain } from 'ubichain'; import ethereum from 'ubichain/blockchains/ethereum'; import base from 'ubichain/blockchains/base'; // Create blockchain interfaces const ethereumChain = useBlockchain(ethereum()); const baseChain = useBlockchain(base()); // Generate a private key const privateKey = ethereumChain.generateKeyPrivate(); // Generate the same address on both chains const ethAddress = ethereumChain.getAddress( ethereumChain.getKeyPublic(privateKey) ); const baseAddress = baseChain.getAddress( baseChain.getKeyPublic(privateKey) ); console.log('Same address?', ethAddress === baseAddress); // true ``` -------------------------------- ### Bitcoin Key Formats Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/bitcoin.md Describes the formats for Bitcoin private and public keys, including their size and representation. ```APIDOC Key Formats: - Private Key: - Format: 32 random bytes (256 bits) - Representation: 64-character hexadecimal string - Public Key: - Derived from: Private key using secp256k1 elliptic curve - Compressed Format: - Size: 33 bytes (66 hex characters) - Prefix: Starts with '02' or '03' - Uncompressed Format: - Size: 65 bytes (130 hex characters) - Prefix: Starts with '04' ``` -------------------------------- ### Ubichain Ethereum Source Code Source: https://github.com/oritwoen/ubichain/blob/main/docs/2.blockchains/ethereum.md Points to the location of the Ethereum implementation within the Ubichain project and its core EVM functions. ```js // Ethereum implementation import { createEVMBlockchain } from '../utils/evm'; export default function ethereum() { return createEVMBlockchain("ethereum"); } ``` ```js // Core EVM address generation function generateAddress(keyPublic) { // Process public key const publicKeyForHashing = /* process public key */; // Apply Keccak-256 hash const keccakHash = keccak_256(publicKeyForHashing); // Take the last 20 bytes const addressBytes = keccakHash.slice(keccakHash.length - 20); // Convert to hex and add checksum return '0x' + toChecksumAddress(bytesToHex(addressBytes)); } // EIP-55 checksum implementation function toChecksumAddress(address) { const lowercaseAddress = address.toLowerCase(); const addressHash = bytesToHex(keccak_256(lowercaseAddress)); let result = ''; for (let i = 0; i < lowercaseAddress.length; i++) { if (parseInt(addressHash[i], 16) >= 8) { result += lowercaseAddress[i].toUpperCase(); } else { result += lowercaseAddress[i]; } } return result; } ``` -------------------------------- ### Generate Bitcoin Legacy and P2SH Addresses Source: https://github.com/oritwoen/ubichain/blob/main/docs/1.guide/3.addresses.md Demonstrates how to generate both legacy (P2PKH) and P2SH addresses from a given public key using the Bitcoin blockchain module in Ubichain. ```js // Default is legacy address (starting with '1') const legacyAddress = chain.getAddress(publicKey); // Generate a P2SH address (starting with '3') const p2shAddress = chain.getAddress(publicKey, 'p2sh'); ```