Skip to content

Commit

Permalink
btcutil: turn address into module
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Mar 16, 2022
1 parent 9b22ef3 commit f0bb656
Show file tree
Hide file tree
Showing 25 changed files with 288 additions and 236 deletions.
6 changes: 3 additions & 3 deletions btcutil/address.go → btcutil/address/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package btcutil
package address

import (
"bytes"
Expand All @@ -12,8 +12,8 @@ import (
"strings"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil/base58"
"github.com/btcsuite/btcd/btcutil/bech32"
"github.com/btcsuite/btcd/btcutil/address/base58"
"github.com/btcsuite/btcd/btcutil/address/bech32"
"github.com/btcsuite/btcd/chaincfg"
"golang.org/x/crypto/ripemd160"
)
Expand Down
190 changes: 95 additions & 95 deletions btcutil/address_test.go → btcutil/address/address_test.go

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions btcutil/base58/README.md → btcutil/address/base58/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ base58

[![Build Status](http://img.shields.io/travis/btcsuite/btcutil.svg)](https://travis-ci.org/btcsuite/btcutil)
[![ISC License](http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)
[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)](http://godoc.org/github.com/btcsuite/btcd/btcutil/base58)
[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)](http://godoc.org/github.com/btcsuite/btcd/btcutil/address/base58)

Package base58 provides an API for encoding and decoding to and from the
modified base58 encoding. It also provides an API to do Base58Check encoding,
Expand All @@ -14,18 +14,18 @@ A comprehensive suite of tests is provided to ensure proper functionality.
## Installation and Updating

```bash
$ go get -u github.com/btcsuite/btcd/btcutil/base58
$ go get -u github.com/btcsuite/btcd/btcutil/address/base58
```

## Examples

* [Decode Example](http://godoc.org/github.com/btcsuite/btcd/btcutil/base58#example-Decode)
* [Decode Example](http://godoc.org/github.com/btcsuite/btcd/btcutil/address/base58#example-Decode)
Demonstrates how to decode modified base58 encoded data.
* [Encode Example](http://godoc.org/github.com/btcsuite/btcd/btcutil/base58#example-Encode)
* [Encode Example](http://godoc.org/github.com/btcsuite/btcd/btcutil/address/base58#example-Encode)
Demonstrates how to encode data using the modified base58 encoding scheme.
* [CheckDecode Example](http://godoc.org/github.com/btcsuite/btcd/btcutil/base58#example-CheckDecode)
* [CheckDecode Example](http://godoc.org/github.com/btcsuite/btcd/btcutil/address/base58#example-CheckDecode)
Demonstrates how to decode Base58Check encoded data.
* [CheckEncode Example](http://godoc.org/github.com/btcsuite/btcd/btcutil/base58#example-CheckEncode)
* [CheckEncode Example](http://godoc.org/github.com/btcsuite/btcd/btcutil/address/base58#example-CheckEncode)
Demonstrates how to encode data using the Base58Check encoding scheme.

## License
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"encoding/hex"
"testing"

"github.com/btcsuite/btcd/btcutil/base58"
"github.com/btcsuite/btcd/btcutil/address/base58"
)

var stringTests = []struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"bytes"
"testing"

"github.com/btcsuite/btcd/btcutil/base58"
"github.com/btcsuite/btcd/btcutil/address/base58"
)

var (
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package base58_test
import (
"testing"

"github.com/btcsuite/btcd/btcutil/base58"
"github.com/btcsuite/btcd/btcutil/address/base58"
)

var checkEncodingStringTests = []struct {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package base58_test
import (
"fmt"

"github.com/btcsuite/btcd/btcutil/base58"
"github.com/btcsuite/btcd/btcutil/address/base58"
)

// This example demonstrates how to decode modified base58 encoded data.
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions btcutil/bech32/README.md → btcutil/address/bech32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ bech32

[![Build Status](http://img.shields.io/travis/btcsuite/btcutil.svg)](https://travis-ci.org/btcsuite/btcutil)
[![ISC License](http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)
[![GoDoc](https://godoc.org/github.com/btcsuite/btcd/btcutil/bech32?status.png)](http://godoc.org/github.com/btcsuite/btcd/btcutil/bech32)
[![GoDoc](https://godoc.org/github.com/btcsuite/btcd/btcutil/address/bech32?status.png)](http://godoc.org/github.com/btcsuite/btcd/btcutil/address/bech32)

Package bech32 provides a Go implementation of the bech32 format specified in
[BIP 173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki).
Expand All @@ -13,14 +13,14 @@ Test vectors from BIP 173 are added to ensure compatibility with the BIP.
## Installation and Updating

```bash
$ go get -u github.com/btcsuite/btcd/btcutil/bech32
$ go get -u github.com/btcsuite/btcd/btcutil/address/bech32
```

## Examples

* [Bech32 decode Example](http://godoc.org/github.com/btcsuite/btcd/btcutil/bech32#example-Bech32Decode)
* [Bech32 decode Example](http://godoc.org/github.com/btcsuite/btcd/btcutil/address/bech32#example-Bech32Decode)
Demonstrates how to decode a bech32 encoded string.
* [Bech32 encode Example](http://godoc.org/github.com/btcsuite/btcd/btcutil/bech32#example-BechEncode)
* [Bech32 encode Example](http://godoc.org/github.com/btcsuite/btcd/btcutil/address/bech32#example-BechEncode)
Demonstrates how to encode data into a bech32 string.

## License
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"encoding/hex"
"fmt"

"github.com/btcsuite/btcd/btcutil/bech32"
"github.com/btcsuite/btcd/btcutil/address/bech32"
)

// This example demonstrates how to decode a bech32 encoded string.
Expand Down
File renamed without changes.
21 changes: 21 additions & 0 deletions btcutil/address/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module github.com/btcsuite/btcd/btcutil/address

require (
github.com/btcsuite/btcd/btcec/v2 v2.1.3
github.com/btcsuite/btcd/chaincfg v1.0.0
github.com/btcsuite/btcd/wire v1.0.0
golang.org/x/crypto v0.0.0-20220210151621-f4118a5b28e2
)

require (
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
)

// TODO(guggero): Remove this as soon as we have a tagged version of chaincfg.
replace github.com/btcsuite/btcd/chaincfg => ../../chaincfg

// TODO(guggero): Remove this as soon as we have a tagged version of wire.
replace github.com/btcsuite/btcd/wire => ../../wire

go 1.17
19 changes: 19 additions & 0 deletions btcutil/address/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
github.com/btcsuite/btcd/btcec/v2 v2.1.3 h1:xM/n3yIhHAhHy04z4i43C8p4ehixJZMsnrVJkgl+MTE=
github.com/btcsuite/btcd/btcec/v2 v2.1.3/go.mod h1:ctjw4H1kknNJmRN4iP1R7bTQ+v3GJkZBd6mui8ZsAZE=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs=
golang.org/x/crypto v0.0.0-20220210151621-f4118a5b28e2 h1:XdAboW3BNMv9ocSCOk/u1MFioZGzCNkiJZ19v9Oe3Ig=
golang.org/x/crypto v0.0.0-20220210151621-f4118a5b28e2/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
2 changes: 1 addition & 1 deletion btcutil/hash160.go → btcutil/address/hash160.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package btcutil
package address

import (
"crypto/sha256"
Expand Down
134 changes: 134 additions & 0 deletions btcutil/address/internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright (c) 2013-2017 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

/*
This test file is part of the btcutil package rather than than the
btcutil_test package so it can bridge access to the internals to properly test
cases which are either not possible or can't reliably be tested via the public
interface. The functions are only exported while the tests are being run.
*/

package address

import (
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil/address/base58"
"github.com/btcsuite/btcd/btcutil/address/bech32"
"golang.org/x/crypto/ripemd160"
)

// TstAddressPubKeyHash makes an AddressPubKeyHash, setting the
// unexported fields with the parameters hash and netID.
func TstAddressPubKeyHash(hash [ripemd160.Size]byte,
netID byte) *AddressPubKeyHash {

return &AddressPubKeyHash{
hash: hash,
netID: netID,
}
}

// TstAddressScriptHash makes an AddressScriptHash, setting the
// unexported fields with the parameters hash and netID.
func TstAddressScriptHash(hash [ripemd160.Size]byte,
netID byte) *AddressScriptHash {

return &AddressScriptHash{
hash: hash,
netID: netID,
}
}

// TstAddressWitnessPubKeyHash creates an AddressWitnessPubKeyHash, initiating
// the fields as given.
func TstAddressWitnessPubKeyHash(version byte, program [20]byte,
hrp string) *AddressWitnessPubKeyHash {

return &AddressWitnessPubKeyHash{
AddressSegWit{
hrp: hrp,
witnessVersion: version,
witnessProgram: program[:],
},
}
}

// TstAddressWitnessScriptHash creates an AddressWitnessScriptHash, initiating
// the fields as given.
func TstAddressWitnessScriptHash(version byte, program [32]byte,
hrp string) *AddressWitnessScriptHash {

return &AddressWitnessScriptHash{
AddressSegWit{
hrp: hrp,
witnessVersion: version,
witnessProgram: program[:],
},
}
}

// TstAddressTaproot creates an AddressTaproot, initiating the fields as given.
func TstAddressTaproot(version byte, program [32]byte,
hrp string) *AddressTaproot {

return &AddressTaproot{
AddressSegWit{
hrp: hrp,
witnessVersion: version,
witnessProgram: program[:],
},
}
}

// TstAddressPubKey makes an AddressPubKey, setting the unexported fields with
// the parameters.
func TstAddressPubKey(serializedPubKey []byte, pubKeyFormat PubKeyFormat,
netID byte) *AddressPubKey {

pubKey, _ := btcec.ParsePubKey(serializedPubKey)
return &AddressPubKey{
pubKeyFormat: pubKeyFormat,
pubKey: pubKey,
pubKeyHashID: netID,
}
}

// TstAddressSAddr returns the expected script address bytes for
// P2PKH and P2SH bitcoin addresses.
func TstAddressSAddr(addr string) []byte {
decoded := base58.Decode(addr)
return decoded[1 : 1+ripemd160.Size]
}

// TstAddressSegwitSAddr returns the expected witness program bytes for
// bech32 encoded P2WPKH and P2WSH bitcoin addresses.
func TstAddressSegwitSAddr(addr string) []byte {
_, data, err := bech32.Decode(addr)
if err != nil {
return []byte{}
}

// First byte is version, rest is base 32 encoded data.
data, err = bech32.ConvertBits(data[1:], 5, 8, false)
if err != nil {
return []byte{}
}
return data
}

// TstAddressTaprootSAddr returns the expected witness program bytes for a
// bech32m encoded P2TR bitcoin address.
func TstAddressTaprootSAddr(addr string) []byte {
_, data, err := bech32.Decode(addr)
if err != nil {
return []byte{}
}

// First byte is version, rest is base 32 encoded data.
data, err = bech32.ConvertBits(data[1:], 5, 8, false)
if err != nil {
return []byte{}
}
return data
}
Loading

0 comments on commit f0bb656

Please sign in to comment.