72 lines
2.1 KiB
Markdown
72 lines
2.1 KiB
Markdown
# go-electrum
|
|
|
|
A pure Go [Electrum](https://electrum.org/) bitcoin library supporting the latest [ElectrumX](https://github.com/spesmilo/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.
|
|
|
|

|
|
|
|
|
|
|
|
## Usage
|
|
See [example/](https://code.stevenpolley.net/steven/go-electrum/src/branch/master/example) for more.
|
|
|
|
### electrum
|
|
```bash
|
|
$ go get code.stevenpolley.net/steven/go-electrum
|
|
```
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"time"
|
|
|
|
"code.stevenpolley.net/steven/go-electrum/electrum"
|
|
)
|
|
|
|
func main() {
|
|
// Establishing a new SSL connection to an ElectrumX server
|
|
client := electrum.NewClient()
|
|
if err := client.ConnectTCP(context.TODO(), "satoshi.stevenpolley.net:50002"); 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 bc1qng5cyhc06q9pnnldv2jxw7ga7hz5q5g0c30gr4
|
|
// We must use scripthash of the address now as explained in ElectrumX docs
|
|
scripthash, _ := electrum.AddressToElectrumScriptHash("bc1qng5cyhc06q9pnnldv2jxw7ga7hz5q5g0c30gr4")
|
|
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) 2025 Steven Polley
|
|
Copyright (c) 2022 Roman Maklakov
|
|
Copyright (c) 2019 Ian Descôteaux
|
|
Copyright (c) 2015 Tristan Rice
|
|
|
|
Based on Tristan Rice [go-electrum](https://github.com/d4l3k/go-electrum) unmaintained library.
|