tronwallet

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2025 License: MIT Imports: 11 Imported by: 0

README

TronWallet
🇮🇩 Bahasa Indonesia · English
coverage license

Description

TronWallet is a small Go library for creating and restoring TRON (TRX) wallets using BIP39/BIP32/BIP44. The library provides simple helpers to:

  • Generate a BIP39 mnemonic (12 or 24 words)
  • Restore a wallet from a mnemonic
  • Derive a secp256k1 private key using the BIP44 path for TRON (m/44'/195'/0'/0/index)
  • Convert a private key into a TRON address (Base58)

Key features

  • NewWallet(length ...) — create a new mnemonic wallet (default 12 words; 24 words supported)
  • RestoreWallet(mnemonic) — validate and restore a wallet from a mnemonic
  • (*TronWallet).Derive(index) — derive the private key for an account index
  • PrivateKeyToHex(priv) — convert a private key to a hex string
  • TronAddressFromPrivate(priv) — convert a private key to a TRON address (Base58)

Example

A short example is available under example/generate.

package main

import (
    "fmt"

    "github.com/ryanbekhen/tronwallet"
)

func main() {
    w, _ := tronwallet.NewWallet()
    fmt.Println("Mnemonic:", w.Mnemonic)

    priv, _ := w.Derive(0)
    addr := tronwallet.TronAddressFromPrivate(priv)

    fmt.Println("Private (hex):", tronwallet.PrivateKeyToHex(priv))
    fmt.Println("Address:", addr)
}

Installation

Make sure you have Go (1.20+) installed. Add this module to your project:

go get github.com/ryanbekhen/tronwallet

Or add it to your go.mod:

require github.com/ryanbekhen/tronwallet latest

Then import and use the library as shown in the example.

API Overview

  • type TronWallet
    • Mnemonic string
    • Seed []byte
  • func NewWallet(length ...MnemonicLength) (*TronWallet, error)
  • func RestoreWallet(mnemonic string) (*TronWallet, error)
  • func (w *TronWallet) Derive(index uint32) (*ecdsa.PrivateKey, error)
  • func PrivateKeyToHex(priv *ecdsa.PrivateKey) string
  • func TronAddressFromPrivate(priv *ecdsa.PrivateKey) string

Security & Disclaimer

  • Never store mnemonics or private keys in public repositories.
  • Example code is provided for educational purposes only. Use this library with caution in production.
  • The author is not responsible for any loss resulting from using this library.

Contributing

Contributions are welcome — please open an issue or a pull request. A few guidelines:

  • Add tests for new features.
  • Follow Go formatting (gofmt).
  • Keep dependencies minimal.

License

This library is released under the MIT License. See LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrivateKeyToBytes

func PrivateKeyToBytes(priv *ecdsa.PrivateKey) []byte

PrivateKeyToBytes returns the 32-byte big-endian representation of the private key's D value, left-padded with zeros if necessary.

func PrivateKeyToHex

func PrivateKeyToHex(priv *ecdsa.PrivateKey) string

PrivateKeyToHex returns the hexadecimal encoding of the private key.

func TronAddressFromPrivate

func TronAddressFromPrivate(priv *ecdsa.PrivateKey) string

TronAddressFromPrivate returns the Base58-encoded Tron address for the provided ECDSA private key. The function computes the uncompressed public key, hashes the X||Y bytes with Keccak-256, takes the last 20 bytes, prefixes with the Tron version byte 0x41, appends a 4-byte checksum (double SHA-256), and encodes the result with Base58.

Types

type ExtKey

type ExtKey struct {
	Key       []byte
	ChainCode []byte
}

ExtKey represents an extended key (private key and chain code) used in BIP32 hierarchical deterministic wallets.

func (*ExtKey) Derive

func (k *ExtKey) Derive(i uint32) (*ExtKey, error)

Derive derives the i-th non-hardened child extended key from k using the public key and the chain code as specified by BIP32.

func (*ExtKey) DeriveHardened

func (k *ExtKey) DeriveHardened(i uint32) (*ExtKey, error)

DeriveHardened derives the i-th hardened child extended key from k. It follows BIP32 hardened derivation where the index is i + 0x80000000.

type MnemonicLength

type MnemonicLength int
const (
	Mnemonic12Words MnemonicLength = 12
	Mnemonic24Words MnemonicLength = 24
)

type TronWallet

type TronWallet struct {
	// Mnemonic is the BIP39 mnemonic phrase for the wallet.
	Mnemonic string
	// Seed is the binary seed derived from the mnemonic (BIP39 seed).
	Seed []byte
}

TronWallet represents a TRON wallet with a mnemonic and seed.

func NewWallet

func NewWallet(length ...MnemonicLength) (*TronWallet, error)

NewWallet creates a new TronWallet with a randomly generated mnemonic. The optional length argument can be used to request a 12- or 24-word mnemonic (default is 12 words).

func RestoreWallet

func RestoreWallet(mnemonic string) (*TronWallet, error)

RestoreWallet validates a BIP39 mnemonic string and returns the corresponding TronWallet with the derived seed.

func (*TronWallet) Derive

func (w *TronWallet) Derive(index uint32) (*ecdsa.PrivateKey, error)

Derive returns the ECDSA private key for the given account index following the Tron/BIP44 derivation implemented in deriveTronPrivateKey.

Directories

Path Synopsis
example
generate command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL