Coder Social home page Coder Social logo

go-electrum's Introduction

go-electrum GoDoc

A pure Go Electrum bitcoin library supporting the latest ElectrumX protocol versions.
This makes it easy to write cryptocurrencies based services in a trustless fashion using Go without having to run a full node.

go-electrum

Usage

See example/ for more.

electrum GoDoc

$ go get github.com/checksum0/go-electrum
package main

import (
	"context"
	"log"
	"time"

	"github.com/checksum0/go-electrum/electrum"
)

func main() {
	// Establishing a new SSL connection to an ElectrumX server
	client := electrum.NewClient()
	if err := client.ConnectTCP(context.TODO(), "bch.imaginary.cash:50001"); err != nil {
		log.Fatal(err)
	}
    ctx := context.TODO()
	// Making sure connection is not closed with timed "client.ping" call
	go func() {
		for {
			if err := client.Ping(ctx); err != nil {
				log.Fatal(err)
			}
			time.Sleep(60 * time.Second)
		}
	}()

	// Making sure we declare to the server what protocol we want to use
	if _, _, err := client.ServerVersion(ctx); err != nil {
		log.Fatal(err)
	}

	// Asking the server for the balance of address 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa
	// 8b01df4e368ea28f8dc0423bcf7a4923e3a12d307c875e47a0cfbf90b5c39161
	// We must use scripthash of the address now as explained in ElectrumX docs
	scripthash, _ := electrum.AddressToElectrumScriptHash("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa")
	balance, err := client.GetBalance(ctx, scripthash)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Address confirmed balance:   %+v", balance.Confirmed)
	log.Printf("Address unconfirmed balance: %+v", balance.Unconfirmed)
}

License

go-electrum is licensed under the MIT license. See LICENSE file for more details.

Copyright (c) 2022 Roman Maklakov
Copyright (c) 2019 Ian Descôteaux
Copyright (c) 2015 Tristan Rice

Based on Tristan Rice go-electrum unmaintained library.

go-electrum's People

Contributors

checksum0 avatar steveguo2triplea avatar roman-mc avatar thomasstevens89 avatar nkuba avatar lukasz-zimnoch avatar tomaszslabon avatar leevlad avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.