Commit graph

324 commits

Author SHA1 Message Date
Roy Lee
38a9f69d1e [lbry] ci: add golangci-lint action 2021-12-14 22:08:54 -08:00
Roy Lee
44869b8b01 [lbry] ci: fix linting errors 2021-12-14 22:08:54 -08:00
Brannon King
ee79ce7992 [lbry] remove pbst submodule 2021-12-14 22:08:50 -08:00
Brannon King
bea5c62644 [lbry] renamed package to lbcutil, updated refs to it 2021-12-14 22:08:50 -08:00
Roy Lee
0ef993d30c lbry: enlarge MaxSatoshi from 2e6 to 2e9 2021-12-14 22:08:50 -08:00
Olaoluwa Osuntokun
9c4bbabe7a
Merge pull request #206 from guggero/makefile
ci: add Makefile, use golangci-lint, parallelize CI actions
2021-11-29 10:29:20 -08:00
Olaoluwa Osuntokun
9cdf59f60c
Merge pull request #205 from guggero/schnorr-taproot
address: add AddressTaproot
2021-09-29 16:32:59 -07:00
Oliver Gugger
12d7d04b31
multi: fix linter issues 2021-09-28 10:30:21 +02:00
Oliver Gugger
97c3fdfaa2
ci: add Makefile, use golangci-lint
With this commit we add a Makefile with the goal of removing the
goclean.sh script. We also use the more powerful golangci-lint linter
and split the GitHub actions into multiple sections to parallelize them.
2021-09-28 10:30:21 +02:00
Oliver Gugger
c5b2b0b979
address: add AddressTaproot 2021-09-27 13:40:07 +02:00
Oliver Gugger
4a0c04f989
address: refactor common code into AddressSegWit
To avoid a lot of code duplication, we extract the common parts of the
AddressWitnessPubKeyHash and AddressWitnessScriptHash types into a
common base type called AddressSegWit.
2021-09-27 13:40:05 +02:00
Olaoluwa Osuntokun
43b172ec93
Merge pull request #202 from Roasbeef/bech32m
bech32: add new EncodeM and DecodeGeneric functions for bech32
2021-09-21 17:25:32 -07:00
Olaoluwa Osuntokun
23db973afa
bech32: add new EncodeM and DecodeGeneric functions for bech32
In this commit, we add two new package level functions: `EncodeM`, and
`DecodeGeneric`. The new encode method is intended to allow callers to
specify that they want to use the new bech32m checksum. This should be
used when encoding segwit addresses with version 1 and beyond. The new
`DecodeGeneric` function allows a caller to decode a bech32 and bech32m
string with a single function. A new return value is added which is the
version of the returned bech32 string, which allows callers to perform
additional segwit addr validation (v1+ should use bech32m etc).

We opted to add new functions rather than modifying the existing
functions to not cause a breaking API change, as most uses in the wild
can just use the existing functions, and only taproot related logic/code
needs to worry about the new methods.

A series of tests have been added to ensure that `DecodeGeneric`
extracts the proper bech version, and we've also adopted the bech32m
tests from BIP 350.
2021-09-21 17:11:48 -07:00
Olaoluwa Osuntokun
14f90e5946
bech32: add additional field to ErrInvalidChecksum (bech32m version)
In this commit, we add an additional field to the ErrInvalidChecksum,
the bech32m version of a checksum. When decoding, we don't now what
version they actually _intended_ to use, so we'll opt to include both
checksums to aide in debugging and error reporting.
2021-09-21 17:11:43 -07:00
Olaoluwa Osuntokun
3ecfc35771
bech32: add new set of constants/versions to be used for bech32m 2021-09-21 17:11:38 -07:00
Olaoluwa Osuntokun
e2ba6805a8
Merge pull request #198 from guggero/stack-overflow-fix
Fix infinite recursion when printing error
2021-05-27 10:08:13 -07:00
Oliver Gugger
4ebb6a9960
address: fix stack overflow bug when printing error
Commit 24e673ae introduced a bug that cause the Error() function to
call itself recursively forever, causing a stack overflow. We explicitly
cast the error to its base type to avoid the recursion and add a small
test case that would've triggered the bug before.
2021-05-27 10:08:03 +02:00
Oliver Gugger
90ffd449a4
mod: update go.sum for golang 1.16.x
The version 1.16 of golang is more diligent when it comes to the go.sum
file and seems to always check whether all dependencies are contained.
Previous versions seem to have ignored this. This change should be
backward compatible though so we might as well commit it.
2021-05-27 10:06:01 +02:00
Olaoluwa Osuntokun
faeebcb9ab
Merge pull request #196 from Crypt-iQ/psbt_panic_fix_05142021
psbt: bounds check SumUtxoInputValues with NonWitness.TxOut indexing
2021-05-14 16:40:26 -07:00
eugene
9c91ffc684
psbt: bounds check SumUtxoInputValues with NonWitness.TxOut indexing
Otherwise, a malformed packet would panic on this check since the
NonWitness.TxOut field did not have the required TxOuts necessary.
2021-05-14 12:23:16 -04:00
Josh Bleecher Snyder
a53e38424c base58: allocate less in Encode
* calculate maximum output length more precisely
  to avoid allocation in the append
* use big.Int.Sign instead of needing bigZero

name                 old time/op    new time/op    delta
Base58Encode_5K-8      5.86ms ± 3%    5.79ms ± 2%   -1.27%  (p=0.035 n=9+10)
Base58Encode_100K-8     2.23s ± 1%     2.23s ± 0%     ~     (p=0.074 n=9+8)
Base58Decode_5K-8       281µs ± 1%     282µs ± 1%     ~     (p=0.720 n=9+10)
Base58Decode_100K-8    89.4ms ± 7%    88.3ms ± 7%     ~     (p=0.123 n=10+10)

name                 old speed      new speed      delta
Base58Encode_5K-8     854kB/s ± 3%   864kB/s ± 2%     ~     (p=0.134 n=9+10)
Base58Encode_100K-8  40.0kB/s ± 0%  40.0kB/s ± 0%     ~     (all equal)
Base58Decode_5K-8    24.3MB/s ± 1%  24.2MB/s ± 1%     ~     (p=0.644 n=9+10)
Base58Decode_100K-8  1.53MB/s ± 7%  1.55MB/s ± 7%     ~     (p=0.218 n=10+10)

name                 old alloc/op   new alloc/op   delta
Base58Encode_5K-8      28.7kB ± 0%    19.2kB ± 0%  -33.03%  (p=0.000 n=10+10)
Base58Encode_100K-8     557kB ± 0%     385kB ± 0%  -30.88%  (p=0.000 n=10+10)
Base58Decode_5K-8       349kB ± 0%     349kB ± 0%     ~     (all equal)
Base58Decode_100K-8     133MB ± 0%     133MB ± 0%     ~     (p=0.183 n=10+10)

name                 old allocs/op  new allocs/op  delta
Base58Encode_5K-8        5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.000 n=10+10)
Base58Encode_100K-8      5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.000 n=10+10)
Base58Decode_5K-8         129 ± 0%       129 ± 0%     ~     (all equal)
Base58Decode_100K-8     2.51k ± 0%     2.51k ± 0%     ~     (p=0.321 n=10+10)

When Go 1.16 is released, performance will improve
significantly due to improvements to math/big.Int's division implementation.
2020-12-08 09:37:02 -05:00
Dave Collins
d63d9f2b44 bech32: Add base256 conversion convenience funcs.
Since bech32 itself works with data encoded with 5 bits per byte (aka
base32) padded out to the nearest byte boundary, the existing functions
for Encode and Decode accept and return data encoded that way.

However, the most common way to use bech32 is to encode data that is
already encoded with 8 bits per byte (aka base256) without padding which
means it is up to the caller to use the ConvertBits function properly to
convert between the two encodings.

Consequently, this introduces two convenience functions for working
directly with base256-encoded data named EncodeFromBase256 and
DecodeToBase256 along with a full set of tests to ensure they work
expected.
2020-12-01 09:42:36 -05:00
Dave Collins
36377a3c8c bech32: Ensure HRP is lowercase when encoding.
BIP173 specifically calls out that encoders must always output an all
lowercase bech32 string and that the lowercase form is used when
determining a character's value for calculating the checksum.

Currently, the implementation does not respect either of those
requirements.

This modifies the Encode function to convert the provided HRP to
lowercase to ensure the requirements are satisfied and adds tests
accordingly.
2020-12-01 09:42:36 -05:00
Anirudha Bose
f281d151bb bech32: back port improvements from decred/dcrd@9b88dd0
This commit brings a host of improvements to the bech32 package. The
public interface of the package remains unchanged.

Summary of changes:
* Improved error handling using dedicated error types. Programmatically
  detect if the errors produced are the expected ones.
* Improve test coverage to test more corner cases. Added test vectors
  from Bitcoin Core.
* Add a benchmark for a full encode/decode cycle of a bech32 string.
* Add a new function DecodeNoLimit, for decoding large bech32 encoded
  strings. It does NOT validate against the BIP-173 maximum length
  allowed for bech32 strings.
* Automatically convert the HRP to lowercase in Encode function.
* Improve performance of encode/decode functions by using
  strings.Builder.
* Improve memory allocation in ConvertBits function.
* Updated documentation.

Credits: @matheusd

Closes #152 and #168.
2020-12-01 09:42:36 -05:00
Egon Elbre
4031bdc69d base58: add new testcases from github.com/bitcoin 2020-11-24 13:21:44 -05:00
Egon Elbre
594180da5d base58: optimize Decode
Before:
    BenchmarkBase58Decode_5K-32     266      4373774 ns/op  1.56 MB/s
    BenchmarkBase58Decode_100K-32   1     1516196700 ns/op  0.09 MB/s

After:
    BenchmarkBase58Decode_5K-32     3868      277944 ns/op  24.57 MB/s
    BenchmarkBase58Decode_100K-32   13      83772100 ns/op   1.63 MB/s
2020-11-24 13:21:44 -05:00
Egon Elbre
ed1fc7ad99 base58: optimize Encode
Before:
    BenchmarkBase58Encode_5K-32     46      23934763 ns/op  0.21 MB/s
    BenchmarkBase58Encode_100K-32    1    9351948600 ns/op  0.01 MB/s

After:
    BenchmarkBase58Encode_5K-32    501       2419129 ns/op  2.07 MB/s
    BenchmarkBase58Encode_100K-32    2     923507950 ns/op  0.11 MB/s
2020-11-24 13:21:44 -05:00
Olaoluwa Osuntokun
a21f014935
Merge pull request #182 from lightning-signer/bip32-zeros
Correct BIP-32 derivation issue
2020-11-03 16:44:01 -08:00
David Hill
88fd8b3dc6 go.mod: bump to go 1.14, latest supported version 2020-10-26 09:48:59 -04:00
David Hill
d32feb0a8e build: replace travis with github ci 2020-10-26 09:48:59 -04:00
Devrandom
dde9e31e50 hdkeychain: correct BIP-32 derivation issue
fixes issue #172
2020-10-21 13:21:55 +02:00
Pascal S. de Kloe
24e673ae72 FIX: integers incorrectly encoded as a rune with append. Go no longer allows it. 2020-10-15 09:23:55 -04:00
Anirudha Bose
063c4115b3 hdkeychain: add CloneWithVersion to set custom HD version bytes
This adds a new method to the ExtendedKey type that allows cloning the
extended key with custom HD version bytes. It does not mutate the
original extended key on which the method is called.

Added some tests to demonstrate the utility of this method, i.e.,
conversion between standard and SLIP-0132 extended keys.
2020-09-21 09:54:55 -04:00
gipsy
4232759481 Tiny grammar correction
This function should only `by` used by applications that need to create custom ExtendedKeys. => This function should only `be` used by applications that need to create custom ExtendedKeys
2020-08-31 09:38:46 -04:00
Olaoluwa Osuntokun
5f93e33af2
Merge pull request #178 from guggero/psbt-vuln-fix
psbt: restore compatibility with wallets that patch CVE-2020-14199
2020-08-26 12:48:09 -07:00
Oliver Gugger
afbd53ee7e
psbt: test full scenario of CVE-2020-14199 patched wallet
We add a test that makes sure the full signing scenario of a wallet that
has the CVE-2020-14199 vulnerability patched is supported by this
library.
2020-07-20 15:02:06 +02:00
Oliver Gugger
c7b6a5aace
psbt: also check witness UTXO if both are set
A wallet that has patched the CVE-2020-14199 vulnerability will always
include a non-witness UTXO, even for witness inputs. In the signer, we
detect that the input we spend is a witness input and copy over the
TxOut to the witness UTXO field. Therefore it is possible that both UTXO
fields are set at the same time. We need to adjust the sanity checks
when adding a partial signature to account for that.
2020-07-20 15:02:05 +02:00
Oliver Gugger
b283b0eb92
psbt: don't remove non-witness UTXO for segwit v0
As a countermeasure to CVE-2020-14199 new HW wallet firmwares require
the full non-witness UTXO to be set even for witness inputs.
We therefore shouldn't remove it when signing.
2020-07-20 15:02:03 +02:00
Oliver Gugger
c5f199e40f
psbt: remove UTXO sanity check to allow fix for CVE
As described in CVE-2020-14199 it is unsafe to only rely on witness
UTXO information when signing. Hardware wallets fixed this by also
requiring the full non-witness UTXO to be present for a witness input.
To be compatible with those newer hardware wallet firmware, we need to
remove the sanity checks that disallowed setting witness and non-witness
UTXOs at the same time.
See https://github.com/bitcoin/bitcoin/pull/19215 for comparison which
removed the sanity checks in Bitcoin Core.
2020-07-20 15:02:02 +02:00
michael1011
4649e4b73b add test case for litecoin bech32 addresses 2020-07-13 09:59:11 -04:00
Anirudha Bose
f648594deb hdkeychain: Fix small wording issue in ChainCode() tests 2020-07-13 09:53:07 -04:00
Anirudha Bose
6c6f3fd3b7 hdkeychain: Remove duplicate ChildNum() method 2020-07-13 09:53:07 -04:00
Marko Bencun
5fadf96a72 hdkeychain: extend API with ChainCode(), ChildNum()
No way to access them otherwise. Handy for interoperability.
2020-07-13 09:53:07 -04:00
Anirudha Bose
c53e27f6f1 Fix OutOfRangeError checks in block_test.go 2020-07-13 09:48:38 -04:00
Yuchuan
e3017c9aab Update block.go
Fix bound check in block.tx(txNum int)
2020-07-13 09:48:38 -04:00
Kalle Rosenbaum
0af7fd2595 Assign returned values in correct order
This is basically a copy of PR #154, which is now obsolete due to refactorings. It also adds a test case for this issue.
2020-07-13 09:45:52 -04:00
Steven Roose
4443e04cb8 Fix error in base58check test 2020-07-13 09:35:42 -04:00
Anirudha Bose
6065135ba8 Improve test coverage of WIF decoder/encoder
Use subtests to split cases, and add new ones, notably for decoding
invalid WIFs.
2020-07-06 10:32:46 -04:00
shuai.qi
c78a7129bf Fix typo 2020-07-06 10:08:39 -04:00
johnta0
35f6653e0a hdkeychain: fix trivial typo 2020-07-06 10:08:01 -04:00