314 lines
9.2 KiB
Go
314 lines
9.2 KiB
Go
// build unittest
|
|
|
|
package btc
|
|
|
|
import (
|
|
"blockbook/bchain"
|
|
"encoding/hex"
|
|
"math/big"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func Test_GetAddrDescFromAddress(t *testing.T) {
|
|
type args struct {
|
|
address string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "P2PKH",
|
|
args: args{address: "1JKgN43B9SyLuZH19H5ECvr4KcfrbVHzZ6"},
|
|
want: "76a914be027bf3eac907bd4ac8cb9c5293b6f37662722088ac",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "P2SH",
|
|
args: args{address: "321x69Cb9HZLWwAWGiUBT1U81r1zPLnEjL"},
|
|
want: "a9140394b3cf9a44782c10105b93962daa8dba304d7f87",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "P2WPKH",
|
|
args: args{address: "bc1qrsf2l34jvqnq0lduyz0j5pfu2nkd93nnq0qggn"},
|
|
want: "00141c12afc6b2602607fdbc209f2a053c54ecd2c673",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "P2WSH",
|
|
args: args{address: "bc1qqwtn5s8vjnqdzrm0du885c46ypzt05vakmljhasx28shlv5a355sw5exgr"},
|
|
want: "002003973a40ec94c0d10f6f6f0e7a62ba2044b7d19db6ff2bf60651e17fb29d8d29",
|
|
wantErr: false,
|
|
},
|
|
}
|
|
parser := NewBitcoinParser(GetChainParams("main"), &Configuration{})
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := parser.GetAddrDescFromAddress(tt.args.address)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
h := hex.EncodeToString(got)
|
|
if !reflect.DeepEqual(h, tt.want) {
|
|
t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_GetAddressesFromAddrDesc(t *testing.T) {
|
|
type args struct {
|
|
script string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want []string
|
|
want2 bool
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "P2PKH",
|
|
args: args{script: "76a914be027bf3eac907bd4ac8cb9c5293b6f37662722088ac"},
|
|
want: []string{"1JKgN43B9SyLuZH19H5ECvr4KcfrbVHzZ6"},
|
|
want2: true,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "P2SH",
|
|
args: args{script: "a9140394b3cf9a44782c10105b93962daa8dba304d7f87"},
|
|
want: []string{"321x69Cb9HZLWwAWGiUBT1U81r1zPLnEjL"},
|
|
want2: true,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "P2WPKH",
|
|
args: args{script: "00141c12afc6b2602607fdbc209f2a053c54ecd2c673"},
|
|
want: []string{"bc1qrsf2l34jvqnq0lduyz0j5pfu2nkd93nnq0qggn"},
|
|
want2: true,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "P2WSH",
|
|
args: args{script: "002003973a40ec94c0d10f6f6f0e7a62ba2044b7d19db6ff2bf60651e17fb29d8d29"},
|
|
want: []string{"bc1qqwtn5s8vjnqdzrm0du885c46ypzt05vakmljhasx28shlv5a355sw5exgr"},
|
|
want2: true,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
// TODO handle OP_RETURN better
|
|
name: "OP_RETURN",
|
|
args: args{script: "6a0461686f6a"},
|
|
want: []string{},
|
|
want2: false,
|
|
wantErr: false,
|
|
},
|
|
}
|
|
|
|
parser := NewBitcoinParser(GetChainParams("main"), &Configuration{})
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
b, _ := hex.DecodeString(tt.args.script)
|
|
got, got2, err := parser.GetAddressesFromAddrDesc(b)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("outputScriptToAddresses() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got, tt.want)
|
|
}
|
|
if !reflect.DeepEqual(got2, tt.want2) {
|
|
t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got2, tt.want2)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
var (
|
|
testTx1, testTx2 bchain.Tx
|
|
|
|
testTxPacked1 = "0001e2408ba8d7af5401000000017f9a22c9cbf54bd902400df746f138f37bcf5b4d93eb755820e974ba43ed5f42040000006a4730440220037f4ed5427cde81d55b9b6a2fd08c8a25090c2c2fff3a75c1a57625ca8a7118022076c702fe55969fa08137f71afd4851c48e31082dd3c40c919c92cdbc826758d30121029f6da5623c9f9b68a9baf9c1bc7511df88fa34c6c2f71f7c62f2f03ff48dca80feffffff019c9700000000000017a9146144d57c8aff48492c9dfb914e120b20bad72d6f8773d00700"
|
|
testTxPacked2 = "0007c91a899ab7da6a010000000001019d64f0c72a0d206001decbffaa722eb1044534c74eee7a5df8318e42a4323ec10000000017160014550da1f5d25a9dae2eafd6902b4194c4c6500af6ffffffff02809698000000000017a914cd668d781ece600efa4b2404dc91fd26b8b8aed8870553d7360000000017a914246655bdbd54c7e477d0ea2375e86e0db2b8f80a8702473044022076aba4ad559616905fa51d4ddd357fc1fdb428d40cb388e042cdd1da4a1b7357022011916f90c712ead9a66d5f058252efd280439ad8956a967e95d437d246710bc9012102a80a5964c5612bb769ef73147b2cf3c149bc0fd4ecb02f8097629c94ab013ffd00000000"
|
|
)
|
|
|
|
func init() {
|
|
testTx1 = bchain.Tx{
|
|
Hex: "01000000017f9a22c9cbf54bd902400df746f138f37bcf5b4d93eb755820e974ba43ed5f42040000006a4730440220037f4ed5427cde81d55b9b6a2fd08c8a25090c2c2fff3a75c1a57625ca8a7118022076c702fe55969fa08137f71afd4851c48e31082dd3c40c919c92cdbc826758d30121029f6da5623c9f9b68a9baf9c1bc7511df88fa34c6c2f71f7c62f2f03ff48dca80feffffff019c9700000000000017a9146144d57c8aff48492c9dfb914e120b20bad72d6f8773d00700",
|
|
Blocktime: 1519053802,
|
|
Txid: "056e3d82e5ffd0e915fb9b62797d76263508c34fe3e5dbed30dd3e943930f204",
|
|
LockTime: 512115,
|
|
Version: 1,
|
|
Vin: []bchain.Vin{
|
|
{
|
|
ScriptSig: bchain.ScriptSig{
|
|
Hex: "4730440220037f4ed5427cde81d55b9b6a2fd08c8a25090c2c2fff3a75c1a57625ca8a7118022076c702fe55969fa08137f71afd4851c48e31082dd3c40c919c92cdbc826758d30121029f6da5623c9f9b68a9baf9c1bc7511df88fa34c6c2f71f7c62f2f03ff48dca80",
|
|
},
|
|
Txid: "425fed43ba74e9205875eb934d5bcf7bf338f146f70d4002d94bf5cbc9229a7f",
|
|
Vout: 4,
|
|
Sequence: 4294967294,
|
|
},
|
|
},
|
|
Vout: []bchain.Vout{
|
|
{
|
|
ValueSat: *big.NewInt(38812),
|
|
N: 0,
|
|
ScriptPubKey: bchain.ScriptPubKey{
|
|
Hex: "a9146144d57c8aff48492c9dfb914e120b20bad72d6f87",
|
|
Addresses: []string{
|
|
"3AZKvpKhSh1o8t1QrX3UeXG9d2BhCRnbcK",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
testTx2 = bchain.Tx{
|
|
Hex: "010000000001019d64f0c72a0d206001decbffaa722eb1044534c74eee7a5df8318e42a4323ec10000000017160014550da1f5d25a9dae2eafd6902b4194c4c6500af6ffffffff02809698000000000017a914cd668d781ece600efa4b2404dc91fd26b8b8aed8870553d7360000000017a914246655bdbd54c7e477d0ea2375e86e0db2b8f80a8702473044022076aba4ad559616905fa51d4ddd357fc1fdb428d40cb388e042cdd1da4a1b7357022011916f90c712ead9a66d5f058252efd280439ad8956a967e95d437d246710bc9012102a80a5964c5612bb769ef73147b2cf3c149bc0fd4ecb02f8097629c94ab013ffd00000000",
|
|
Blocktime: 1235678901,
|
|
Txid: "474e6795760ebe81cb4023dc227e5a0efe340e1771c89a0035276361ed733de7",
|
|
LockTime: 0,
|
|
Version: 1,
|
|
Vin: []bchain.Vin{
|
|
{
|
|
ScriptSig: bchain.ScriptSig{
|
|
Hex: "160014550da1f5d25a9dae2eafd6902b4194c4c6500af6",
|
|
},
|
|
Txid: "c13e32a4428e31f85d7aee4ec7344504b12e72aaffcbde0160200d2ac7f0649d",
|
|
Vout: 0,
|
|
Sequence: 4294967295,
|
|
},
|
|
},
|
|
Vout: []bchain.Vout{
|
|
{
|
|
ValueSat: *big.NewInt(10000000),
|
|
N: 0,
|
|
ScriptPubKey: bchain.ScriptPubKey{
|
|
Hex: "a914cd668d781ece600efa4b2404dc91fd26b8b8aed887",
|
|
Addresses: []string{
|
|
"2NByHN6A8QYkBATzxf4pRGbCSHD5CEN2TRu",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
ValueSat: *big.NewInt(920081157),
|
|
N: 1,
|
|
ScriptPubKey: bchain.ScriptPubKey{
|
|
Hex: "a914246655bdbd54c7e477d0ea2375e86e0db2b8f80a87",
|
|
Addresses: []string{
|
|
"2MvZguYaGjM7JihBgNqgLF2Ca2Enb76Hj9D",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func Test_PackTx(t *testing.T) {
|
|
type args struct {
|
|
tx bchain.Tx
|
|
height uint32
|
|
blockTime int64
|
|
parser *BitcoinParser
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "btc-1",
|
|
args: args{
|
|
tx: testTx1,
|
|
height: 123456,
|
|
blockTime: 1519053802,
|
|
parser: NewBitcoinParser(GetChainParams("main"), &Configuration{}),
|
|
},
|
|
want: testTxPacked1,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "testnet-1",
|
|
args: args{
|
|
tx: testTx2,
|
|
height: 510234,
|
|
blockTime: 1235678901,
|
|
parser: NewBitcoinParser(GetChainParams("test"), &Configuration{}),
|
|
},
|
|
want: testTxPacked2,
|
|
wantErr: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
h := hex.EncodeToString(got)
|
|
if !reflect.DeepEqual(h, tt.want) {
|
|
t.Errorf("packTx() = %v, want %v", h, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_UnpackTx(t *testing.T) {
|
|
type args struct {
|
|
packedTx string
|
|
parser *BitcoinParser
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want *bchain.Tx
|
|
want1 uint32
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "btc-1",
|
|
args: args{
|
|
packedTx: testTxPacked1,
|
|
parser: NewBitcoinParser(GetChainParams("main"), &Configuration{}),
|
|
},
|
|
want: &testTx1,
|
|
want1: 123456,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "testnet-1",
|
|
args: args{
|
|
packedTx: testTxPacked2,
|
|
parser: NewBitcoinParser(GetChainParams("test"), &Configuration{}),
|
|
},
|
|
want: &testTx2,
|
|
want1: 510234,
|
|
wantErr: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
b, _ := hex.DecodeString(tt.args.packedTx)
|
|
got, got1, err := tt.args.parser.UnpackTx(b)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("unpackTx() got = %v, want %v", got, tt.want)
|
|
}
|
|
if got1 != tt.want1 {
|
|
t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1)
|
|
}
|
|
})
|
|
}
|
|
}
|