forked from BitBoxSwiss/bitbox-wallet-app
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backend/btc: refactor pkScript encoding/decoding
We use functions from the btcd library to convert addresses to pubKeyScripts and vice versa. While the address type has support for Taproot addresses, the pubkey script functions in btcd don't handle them yet. See also: btcsuite/btcd#1768 To speed up send-to-taproot support, refactor the pubkey script encoding/decoding into separate unit-tested functions, to which we can easily add taproot support in the next commit.
- Loading branch information
Showing
5 changed files
with
150 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2021 Shift Crypto AG | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/btcsuite/btcd/chaincfg" | ||
"github.com/btcsuite/btcutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPkScriptFromAddress(t *testing.T) { | ||
hash := []byte("\x92\x95\x3b\x69\x91\x29\x70\x02\xfa\xa6\x2a\x1d\xd2\x43\x13\xff\x62\x1e\x10\xab") | ||
net := &chaincfg.MainNetParams | ||
|
||
var address btcutil.Address | ||
|
||
address, err := btcutil.NewAddressPubKeyHash(hash, net) | ||
require.NoError(t, err) | ||
pkScript, err := PkScriptFromAddress(address) | ||
require.NoError(t, err) | ||
require.Equal(t, | ||
[]byte("\x76\xa9\x14\x92\x95\x3b\x69\x91\x29\x70\x02\xfa\xa6\x2a\x1d\xd2\x43\x13\xff\x62\x1e\x10\xab\x88\xac"), | ||
pkScript) | ||
|
||
address, err = btcutil.NewAddressWitnessPubKeyHash(hash, net) | ||
require.NoError(t, err) | ||
pkScript, err = PkScriptFromAddress(address) | ||
require.NoError(t, err) | ||
require.Equal(t, | ||
[]byte("\x00\x14\x92\x95\x3b\x69\x91\x29\x70\x02\xfa\xa6\x2a\x1d\xd2\x43\x13\xff\x62\x1e\x10\xab"), | ||
pkScript) | ||
|
||
address, err = btcutil.NewAddressScriptHashFromHash(hash, net) | ||
require.NoError(t, err) | ||
pkScript, err = PkScriptFromAddress(address) | ||
require.NoError(t, err) | ||
require.Equal(t, | ||
[]byte("\xa9\x14\x92\x95\x3b\x69\x91\x29\x70\x02\xfa\xa6\x2a\x1d\xd2\x43\x13\xff\x62\x1e\x10\xab\x87"), | ||
pkScript) | ||
|
||
scriptHash := []byte("\x4a\xf2\xe4\x54\x9a\x5c\xbb\x73\x6e\x77\xce\xf5\x2f\xe3\x0b\x9d\xf8\x12\x1d\x73\x56\xab\x20\x05\x46\x3e\xcb\x08\x97\x23\x45\x8d") | ||
address, err = btcutil.NewAddressWitnessScriptHash(scriptHash, net) | ||
require.NoError(t, err) | ||
pkScript, err = PkScriptFromAddress(address) | ||
require.NoError(t, err) | ||
require.Equal(t, | ||
[]byte("\x00\x20\x4a\xf2\xe4\x54\x9a\x5c\xbb\x73\x6e\x77\xce\xf5\x2f\xe3\x0b\x9d\xf8\x12\x1d\x73\x56\xab\x20\x05\x46\x3e\xcb\x08\x97\x23\x45\x8d"), | ||
pkScript) | ||
} | ||
|
||
func TestAddressFromPkScript(t *testing.T) { | ||
hash := []byte("\x92\x95\x3b\x69\x91\x29\x70\x02\xfa\xa6\x2a\x1d\xd2\x43\x13\xff\x62\x1e\x10\xab") | ||
net := &chaincfg.MainNetParams | ||
|
||
var address btcutil.Address | ||
|
||
address, err := btcutil.NewAddressPubKeyHash(hash, net) | ||
require.NoError(t, err) | ||
pkScript, err := PkScriptFromAddress(address) | ||
require.NoError(t, err) | ||
recoveredAddres, err := AddressFromPkScript(pkScript, &chaincfg.MainNetParams) | ||
require.NoError(t, err) | ||
require.Equal(t, address.ScriptAddress(), recoveredAddres.ScriptAddress()) | ||
|
||
address, err = btcutil.NewAddressWitnessPubKeyHash(hash, net) | ||
require.NoError(t, err) | ||
pkScript, err = PkScriptFromAddress(address) | ||
require.NoError(t, err) | ||
recoveredAddres, err = AddressFromPkScript(pkScript, &chaincfg.MainNetParams) | ||
require.NoError(t, err) | ||
require.Equal(t, address.ScriptAddress(), recoveredAddres.ScriptAddress()) | ||
|
||
address, err = btcutil.NewAddressScriptHashFromHash(hash, net) | ||
require.NoError(t, err) | ||
pkScript, err = PkScriptFromAddress(address) | ||
require.NoError(t, err) | ||
recoveredAddres, err = AddressFromPkScript(pkScript, &chaincfg.MainNetParams) | ||
require.NoError(t, err) | ||
require.Equal(t, address.ScriptAddress(), recoveredAddres.ScriptAddress()) | ||
|
||
scriptHash := []byte("\x4a\xf2\xe4\x54\x9a\x5c\xbb\x73\x6e\x77\xce\xf5\x2f\xe3\x0b\x9d\xf8\x12\x1d\x73\x56\xab\x20\x05\x46\x3e\xcb\x08\x97\x23\x45\x8d") | ||
address, err = btcutil.NewAddressWitnessScriptHash(scriptHash, net) | ||
require.NoError(t, err) | ||
pkScript, err = PkScriptFromAddress(address) | ||
require.NoError(t, err) | ||
recoveredAddres, err = AddressFromPkScript(pkScript, &chaincfg.MainNetParams) | ||
require.NoError(t, err) | ||
require.Equal(t, address.ScriptAddress(), recoveredAddres.ScriptAddress()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters