pabtc 1.2.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pabtc-1.2.2/.gitignore +4 -0
- pabtc-1.2.2/LICENSE +21 -0
- pabtc-1.2.2/PKG-INFO +123 -0
- pabtc-1.2.2/README.md +93 -0
- pabtc-1.2.2/example/addr.py +24 -0
- pabtc-1.2.2/example/message.py +19 -0
- pabtc-1.2.2/example/regtest.py +29 -0
- pabtc-1.2.2/example/taproot.py +112 -0
- pabtc-1.2.2/example/transfer.py +33 -0
- pabtc-1.2.2/pabtc/__init__.py +12 -0
- pabtc-1.2.2/pabtc/base58.py +60 -0
- pabtc-1.2.2/pabtc/bech32.py +150 -0
- pabtc-1.2.2/pabtc/config.py +57 -0
- pabtc-1.2.2/pabtc/core.py +863 -0
- pabtc-1.2.2/pabtc/denomination.py +2 -0
- pabtc-1.2.2/pabtc/ecdsa.py +52 -0
- pabtc-1.2.2/pabtc/opcode.py +277 -0
- pabtc-1.2.2/pabtc/ripemd160.py +154 -0
- pabtc-1.2.2/pabtc/rpc.py +618 -0
- pabtc-1.2.2/pabtc/schnorr.py +48 -0
- pabtc-1.2.2/pabtc/secp256k1.py +176 -0
- pabtc-1.2.2/pabtc/wallet.py +377 -0
- pabtc-1.2.2/pyproject.toml +17 -0
- pabtc-1.2.2/test/test_base58.py +22 -0
- pabtc-1.2.2/test/test_bech32.py +66 -0
- pabtc-1.2.2/test/test_core.py +195 -0
- pabtc-1.2.2/test/test_example.py +22 -0
- pabtc-1.2.2/test/test_ripemd160.py +17 -0
- pabtc-1.2.2/test/test_rpc.py +21 -0
- pabtc-1.2.2/test/test_schnorr.py +11 -0
- pabtc-1.2.2/test/test_wallet.py +36 -0
pabtc-1.2.2/.gitignore
ADDED
pabtc-1.2.2/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Mohanson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
pabtc-1.2.2/PKG-INFO
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: pabtc
|
|
3
|
+
Version: 1.2.2
|
|
4
|
+
Summary: Plbtc: bitcoin library for humans
|
|
5
|
+
Project-URL: homepage, https://github.com/mohanson/pabtc
|
|
6
|
+
Author-email: Mohanson <mohanson@outlook.com>
|
|
7
|
+
License: MIT License
|
|
8
|
+
|
|
9
|
+
Copyright (c) 2023 Mohanson
|
|
10
|
+
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
12
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
13
|
+
in the Software without restriction, including without limitation the rights
|
|
14
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
15
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
16
|
+
furnished to do so, subject to the following conditions:
|
|
17
|
+
|
|
18
|
+
The above copyright notice and this permission notice shall be included in all
|
|
19
|
+
copies or substantial portions of the Software.
|
|
20
|
+
|
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
22
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
23
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
24
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
25
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
26
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
27
|
+
SOFTWARE.
|
|
28
|
+
Requires-Dist: requests
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
# Pabtc: Bitcoin Library For Humans
|
|
32
|
+
|
|
33
|
+
Pabtc is a project that aims to provide human-friendly interfaces for common btc operations. Using pabtc, you can easily and happily complete everything you want to do on btc.
|
|
34
|
+
|
|
35
|
+
Features:
|
|
36
|
+
|
|
37
|
+
- No third-party dependencies, everything is visible.
|
|
38
|
+
- Incredibly simple, even a cat knows how to use it.
|
|
39
|
+
|
|
40
|
+
## Installation
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
$ pip install pabtc
|
|
44
|
+
# or
|
|
45
|
+
$ git clone https://github.com/mohanson/pabtc
|
|
46
|
+
$ cd pabtc
|
|
47
|
+
$ python -m pip install --editable .
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
By default, pabtc is configured on the develop. To switch networks, use `pabtc.config.current = pabtc.config.mainnet`.
|
|
53
|
+
|
|
54
|
+
**example/addr.py**
|
|
55
|
+
|
|
56
|
+
Calculate the address from a private key.
|
|
57
|
+
|
|
58
|
+
```sh
|
|
59
|
+
$ python example/addr.py --net mainnet --prikey 1
|
|
60
|
+
|
|
61
|
+
# p2pkh 1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH
|
|
62
|
+
# p2sh-p2wpkh 3JvL6Ymt8MVWiCNHC7oWU6nLeHNJKLZGLN
|
|
63
|
+
# p2wpkh bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4
|
|
64
|
+
# p2tr bc1pmfr3p9j00pfxjh0zmgp99y8zftmd3s5pmedqhyptwy6lm87hf5sspknck9
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
**example/message.py**
|
|
68
|
+
|
|
69
|
+
Sign a message with the private key and verify it.
|
|
70
|
+
|
|
71
|
+
```sh
|
|
72
|
+
$ python example/message.py --prikey 1 --msg pybtc
|
|
73
|
+
# ICvzXjwjJVMilSGyMqwlqMTuGF6UMwddFJzVmm0Di5qNnqkBRKP8Pldm3YbOskg3ewV1tszVLy8gVX1u+qFrx6o=
|
|
74
|
+
|
|
75
|
+
$ python example/message.py --prikey 1 --msg pybtc --sig ICvzXjwjJVMilSGyMqwlqMTuGF6UMwddFJzVmm0Di5qNnqkBRKP8Pldm3YbOskg3ewV1tszVLy8gVX1u+qFrx6o=
|
|
76
|
+
# True
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
**example/taproot.py**
|
|
81
|
+
|
|
82
|
+
This example demonstrates how to create a P2TR script with two script spending paths: p2pk and p2ms(2-of-2 multisig).
|
|
83
|
+
|
|
84
|
+
```sh
|
|
85
|
+
$ python example/taproot.py
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**example/transfer.py**
|
|
89
|
+
|
|
90
|
+
Transfer Bitcoin to another account. Pybtc supports four common types of Bitcoin transactions: P2PKH, P2SH-P2WPKH, P2WPKH, and P2TR. For more complex account types, such as P2SH-P2MS, please refer to [test/test_wallet.py](test/test_wallet.py).
|
|
91
|
+
|
|
92
|
+
```sh
|
|
93
|
+
$ python example/transfer.py --net develop --prikey 1 --script-type p2pkh --to mg8Jz5776UdyiYcBb9Z873NTozEiADRW5H --value 0.1
|
|
94
|
+
|
|
95
|
+
# 0x039d1b0fe969d33341a7db9ddd236f632d6851292200603abc5a6c7738bf3079
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Before using this script, you first need to execute the code in the Test section. This is because pybtc requires the bitcoin core wallet to provide an account's utxo set.
|
|
99
|
+
|
|
100
|
+
## Test
|
|
101
|
+
|
|
102
|
+
The testing of this project relies on regtest. You can set up the regtest node using the following steps:
|
|
103
|
+
|
|
104
|
+
```sh
|
|
105
|
+
$ wget https://bitcoincore.org/bin/bitcoin-core-27.1/bitcoin-27.1-x86_64-linux-gnu.tar.gz
|
|
106
|
+
$ tar -xvf bitcoin-27.1-x86_64-linux-gnu.tar.gz
|
|
107
|
+
$ cp -R bitcoin-27.1 ~/app/bitcoin # Install to the target location.
|
|
108
|
+
|
|
109
|
+
$ mkdir ~/.bitcoin
|
|
110
|
+
$ echo "chain=regtest" >> ~/.bitcoin/bitcoin.conf
|
|
111
|
+
$ echo "rpcpassword=pass" >> ~/.bitcoin/bitcoin.conf
|
|
112
|
+
$ echo "rpcuser=user" >> ~/.bitcoin/bitcoin.conf
|
|
113
|
+
$ echo "txindex=1" >> ~/.bitcoin/bitcoin.conf
|
|
114
|
+
|
|
115
|
+
$ bitcoind
|
|
116
|
+
# Create default wallets
|
|
117
|
+
$ python example/regtest.py
|
|
118
|
+
$ pytest -v
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
MIT
|
pabtc-1.2.2/README.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# Pabtc: Bitcoin Library For Humans
|
|
2
|
+
|
|
3
|
+
Pabtc is a project that aims to provide human-friendly interfaces for common btc operations. Using pabtc, you can easily and happily complete everything you want to do on btc.
|
|
4
|
+
|
|
5
|
+
Features:
|
|
6
|
+
|
|
7
|
+
- No third-party dependencies, everything is visible.
|
|
8
|
+
- Incredibly simple, even a cat knows how to use it.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
$ pip install pabtc
|
|
14
|
+
# or
|
|
15
|
+
$ git clone https://github.com/mohanson/pabtc
|
|
16
|
+
$ cd pabtc
|
|
17
|
+
$ python -m pip install --editable .
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
By default, pabtc is configured on the develop. To switch networks, use `pabtc.config.current = pabtc.config.mainnet`.
|
|
23
|
+
|
|
24
|
+
**example/addr.py**
|
|
25
|
+
|
|
26
|
+
Calculate the address from a private key.
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
$ python example/addr.py --net mainnet --prikey 1
|
|
30
|
+
|
|
31
|
+
# p2pkh 1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH
|
|
32
|
+
# p2sh-p2wpkh 3JvL6Ymt8MVWiCNHC7oWU6nLeHNJKLZGLN
|
|
33
|
+
# p2wpkh bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4
|
|
34
|
+
# p2tr bc1pmfr3p9j00pfxjh0zmgp99y8zftmd3s5pmedqhyptwy6lm87hf5sspknck9
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
**example/message.py**
|
|
38
|
+
|
|
39
|
+
Sign a message with the private key and verify it.
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
$ python example/message.py --prikey 1 --msg pybtc
|
|
43
|
+
# ICvzXjwjJVMilSGyMqwlqMTuGF6UMwddFJzVmm0Di5qNnqkBRKP8Pldm3YbOskg3ewV1tszVLy8gVX1u+qFrx6o=
|
|
44
|
+
|
|
45
|
+
$ python example/message.py --prikey 1 --msg pybtc --sig ICvzXjwjJVMilSGyMqwlqMTuGF6UMwddFJzVmm0Di5qNnqkBRKP8Pldm3YbOskg3ewV1tszVLy8gVX1u+qFrx6o=
|
|
46
|
+
# True
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
**example/taproot.py**
|
|
51
|
+
|
|
52
|
+
This example demonstrates how to create a P2TR script with two script spending paths: p2pk and p2ms(2-of-2 multisig).
|
|
53
|
+
|
|
54
|
+
```sh
|
|
55
|
+
$ python example/taproot.py
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**example/transfer.py**
|
|
59
|
+
|
|
60
|
+
Transfer Bitcoin to another account. Pybtc supports four common types of Bitcoin transactions: P2PKH, P2SH-P2WPKH, P2WPKH, and P2TR. For more complex account types, such as P2SH-P2MS, please refer to [test/test_wallet.py](test/test_wallet.py).
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
$ python example/transfer.py --net develop --prikey 1 --script-type p2pkh --to mg8Jz5776UdyiYcBb9Z873NTozEiADRW5H --value 0.1
|
|
64
|
+
|
|
65
|
+
# 0x039d1b0fe969d33341a7db9ddd236f632d6851292200603abc5a6c7738bf3079
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Before using this script, you first need to execute the code in the Test section. This is because pybtc requires the bitcoin core wallet to provide an account's utxo set.
|
|
69
|
+
|
|
70
|
+
## Test
|
|
71
|
+
|
|
72
|
+
The testing of this project relies on regtest. You can set up the regtest node using the following steps:
|
|
73
|
+
|
|
74
|
+
```sh
|
|
75
|
+
$ wget https://bitcoincore.org/bin/bitcoin-core-27.1/bitcoin-27.1-x86_64-linux-gnu.tar.gz
|
|
76
|
+
$ tar -xvf bitcoin-27.1-x86_64-linux-gnu.tar.gz
|
|
77
|
+
$ cp -R bitcoin-27.1 ~/app/bitcoin # Install to the target location.
|
|
78
|
+
|
|
79
|
+
$ mkdir ~/.bitcoin
|
|
80
|
+
$ echo "chain=regtest" >> ~/.bitcoin/bitcoin.conf
|
|
81
|
+
$ echo "rpcpassword=pass" >> ~/.bitcoin/bitcoin.conf
|
|
82
|
+
$ echo "rpcuser=user" >> ~/.bitcoin/bitcoin.conf
|
|
83
|
+
$ echo "txindex=1" >> ~/.bitcoin/bitcoin.conf
|
|
84
|
+
|
|
85
|
+
$ bitcoind
|
|
86
|
+
# Create default wallets
|
|
87
|
+
$ python example/regtest.py
|
|
88
|
+
$ pytest -v
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
|
|
93
|
+
MIT
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import pabtc
|
|
3
|
+
|
|
4
|
+
# Calculate the address from a private key.
|
|
5
|
+
|
|
6
|
+
parser = argparse.ArgumentParser()
|
|
7
|
+
parser.add_argument('--net', type=str, choices=['develop', 'mainnet', 'testnet'], default='develop')
|
|
8
|
+
parser.add_argument('--prikey', type=str, help='private key')
|
|
9
|
+
args = parser.parse_args()
|
|
10
|
+
|
|
11
|
+
if args.net == 'develop':
|
|
12
|
+
pabtc.config.current = pabtc.config.develop
|
|
13
|
+
if args.net == 'mainnet':
|
|
14
|
+
pabtc.config.current = pabtc.config.mainnet
|
|
15
|
+
if args.net == 'testnet':
|
|
16
|
+
pabtc.config.current = pabtc.config.testnet
|
|
17
|
+
|
|
18
|
+
prikey = pabtc.core.PriKey(int(args.prikey, 0))
|
|
19
|
+
pubkey = prikey.pubkey()
|
|
20
|
+
|
|
21
|
+
print('p2pkh ', pabtc.core.address_p2pkh(pubkey))
|
|
22
|
+
print('p2sh-p2wpkh', pabtc.core.address_p2sh_p2wpkh(pubkey))
|
|
23
|
+
print('p2wpkh ', pabtc.core.address_p2wpkh(pubkey))
|
|
24
|
+
print('p2tr ', pabtc.core.address_p2tr(pubkey, bytearray()))
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import pabtc
|
|
3
|
+
|
|
4
|
+
# Sign a message with the private key and verify it.
|
|
5
|
+
|
|
6
|
+
parser = argparse.ArgumentParser()
|
|
7
|
+
parser.add_argument('--msg', type=str, help='the message to create a signature of')
|
|
8
|
+
parser.add_argument('--prikey', type=str, help='private key')
|
|
9
|
+
parser.add_argument('--sig', type=str, default='', help='the signature of the message encoded in base 64')
|
|
10
|
+
args = parser.parse_args()
|
|
11
|
+
|
|
12
|
+
prikey = pabtc.core.PriKey(int(args.prikey, 0))
|
|
13
|
+
pubkey = prikey.pubkey()
|
|
14
|
+
|
|
15
|
+
if args.sig == '':
|
|
16
|
+
print(pabtc.core.Message(args.msg).sign(prikey))
|
|
17
|
+
|
|
18
|
+
if args.sig != '':
|
|
19
|
+
print(pabtc.core.Message(args.msg).pubkey(args.sig) == pubkey)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import pabtc
|
|
2
|
+
|
|
3
|
+
pub1 = pabtc.core.PriKey(1).pubkey()
|
|
4
|
+
pub2 = pabtc.core.PriKey(2).pubkey()
|
|
5
|
+
|
|
6
|
+
pabtc.rpc.call('createwallet', ['main', True, True, None, None, None, True])
|
|
7
|
+
for d in [
|
|
8
|
+
f'pkh({pub1.sec().hex()})',
|
|
9
|
+
f'pkh({pub2.sec().hex()})',
|
|
10
|
+
f'sh(multi(2,{pub1.sec().hex()},{pub2.sec().hex()}))',
|
|
11
|
+
f'sh(multi(2,{pub2.sec().hex()},{pub1.sec().hex()}))',
|
|
12
|
+
f'sh(wpkh({pub1.sec().hex()}))',
|
|
13
|
+
f'sh(wpkh({pub2.sec().hex()}))',
|
|
14
|
+
f'wpkh({pub1.sec().hex()})',
|
|
15
|
+
f'wpkh({pub2.sec().hex()})',
|
|
16
|
+
f'tr({pub1.sec().hex()})',
|
|
17
|
+
f'tr({pub2.sec().hex()})',
|
|
18
|
+
]:
|
|
19
|
+
d = pabtc.rpc.get_descriptor_info(d)['descriptor']
|
|
20
|
+
pabtc.rpc.import_descriptors([{
|
|
21
|
+
'desc': d,
|
|
22
|
+
'timestamp': 0,
|
|
23
|
+
}])
|
|
24
|
+
pabtc.rpc.generate_to_address(10, pabtc.core.address_p2pkh(pub1))
|
|
25
|
+
pabtc.rpc.generate_to_address(10, pabtc.core.address_p2sh_p2ms(2, [pub1, pub2]))
|
|
26
|
+
pabtc.rpc.generate_to_address(10, pabtc.core.address_p2sh_p2wpkh(pub1))
|
|
27
|
+
pabtc.rpc.generate_to_address(10, pabtc.core.address_p2wpkh(pub1))
|
|
28
|
+
pabtc.rpc.generate_to_address(10, pabtc.core.address_p2tr(pub1, bytearray()))
|
|
29
|
+
pabtc.rpc.generate_to_address(99, pabtc.core.address_p2pkh(pub1))
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import pabtc
|
|
2
|
+
|
|
3
|
+
# This example shows how to create a P2TR script with two unlock conditions: p2pk and p2ms.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# Here created two scripts, one of which is a p2pk script, which requires that it can only be unlocked by private key 2,
|
|
7
|
+
# and the other is an 2-of-2 multisig script.
|
|
8
|
+
mast = pabtc.core.TapNode(
|
|
9
|
+
pabtc.core.TapLeaf(pabtc.core.script([
|
|
10
|
+
pabtc.opcode.op_pushdata(pabtc.core.PriKey(2).pubkey().sec()[1:]),
|
|
11
|
+
pabtc.opcode.op_checksig,
|
|
12
|
+
])),
|
|
13
|
+
pabtc.core.TapLeaf(pabtc.core.script([
|
|
14
|
+
pabtc.opcode.op_pushdata(pabtc.core.PriKey(3).pubkey().sec()[1:]),
|
|
15
|
+
pabtc.opcode.op_checksig,
|
|
16
|
+
pabtc.opcode.op_pushdata(pabtc.core.PriKey(4).pubkey().sec()[1:]),
|
|
17
|
+
pabtc.opcode.op_checksigadd,
|
|
18
|
+
pabtc.opcode.op_n(2),
|
|
19
|
+
pabtc.opcode.op_equal,
|
|
20
|
+
]))
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class Tp2trp2pk:
|
|
25
|
+
def __init__(self, pubkey: pabtc.core.PubKey):
|
|
26
|
+
self.pubkey = pubkey
|
|
27
|
+
self.addr = pabtc.core.address_p2tr(pubkey, mast.hash)
|
|
28
|
+
self.script = pabtc.core.script_pubkey_p2tr(self.addr)
|
|
29
|
+
output_pubkey_byte = bytearray([0x02]) + pabtc.bech32.decode(pabtc.config.current.prefix.bech32, 1, self.addr)
|
|
30
|
+
output_pubkey = pabtc.core.PubKey.sec_decode(output_pubkey_byte)
|
|
31
|
+
# Control byte with leaf version and parity bit.
|
|
32
|
+
if output_pubkey.y & 1:
|
|
33
|
+
self.prefix = 0xc1
|
|
34
|
+
else:
|
|
35
|
+
self.prefix = 0xc0
|
|
36
|
+
|
|
37
|
+
def sign(self, tx: pabtc.core.Transaction):
|
|
38
|
+
for i, e in enumerate(tx.vin):
|
|
39
|
+
m = tx.digest_segwit_v1(i, pabtc.core.sighash_all, mast.l.script)
|
|
40
|
+
s = pabtc.core.PriKey(2).sign_schnorr(m) + bytearray([pabtc.core.sighash_all])
|
|
41
|
+
e.witness[0] = s
|
|
42
|
+
|
|
43
|
+
def txin(self, op: pabtc.core.OutPoint):
|
|
44
|
+
return pabtc.core.TxIn(op, bytearray(), 0xffffffff, [
|
|
45
|
+
bytearray(65),
|
|
46
|
+
mast.l.script,
|
|
47
|
+
bytearray([self.prefix]) + self.pubkey.sec()[1:] + mast.r.hash,
|
|
48
|
+
])
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class Tp2trp2ms:
|
|
52
|
+
def __init__(self, pubkey: pabtc.core.PubKey):
|
|
53
|
+
self.pubkey = pubkey
|
|
54
|
+
self.addr = pabtc.core.address_p2tr(pubkey, mast.hash)
|
|
55
|
+
self.script = pabtc.core.script_pubkey_p2tr(self.addr)
|
|
56
|
+
output_pubkey_byte = bytearray([0x02]) + pabtc.bech32.decode(pabtc.config.current.prefix.bech32, 1, self.addr)
|
|
57
|
+
output_pubkey = pabtc.core.PubKey.sec_decode(output_pubkey_byte)
|
|
58
|
+
# Control byte with leaf version and parity bit.
|
|
59
|
+
if output_pubkey.y & 1:
|
|
60
|
+
self.prefix = 0xc1
|
|
61
|
+
else:
|
|
62
|
+
self.prefix = 0xc0
|
|
63
|
+
|
|
64
|
+
def sign(self, tx: pabtc.core.Transaction):
|
|
65
|
+
for i, e in enumerate(tx.vin):
|
|
66
|
+
m = tx.digest_segwit_v1(i, pabtc.core.sighash_all, mast.r.script)
|
|
67
|
+
e.witness[0] = pabtc.core.PriKey(4).sign_schnorr(m) + bytearray([pabtc.core.sighash_all])
|
|
68
|
+
e.witness[1] = pabtc.core.PriKey(3).sign_schnorr(m) + bytearray([pabtc.core.sighash_all])
|
|
69
|
+
|
|
70
|
+
def txin(self, op: pabtc.core.OutPoint):
|
|
71
|
+
return pabtc.core.TxIn(op, bytearray(), 0xffffffff, [
|
|
72
|
+
bytearray(65),
|
|
73
|
+
bytearray(65),
|
|
74
|
+
mast.r.script,
|
|
75
|
+
bytearray([self.prefix]) + self.pubkey.sec()[1:] + mast.l.hash,
|
|
76
|
+
])
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
mate = pabtc.wallet.Wallet(pabtc.wallet.Tp2pkh(1))
|
|
80
|
+
pabtc.rpc.generate_to_address(10, mate.addr)
|
|
81
|
+
|
|
82
|
+
user_p2tr = pabtc.wallet.Wallet(pabtc.wallet.Tp2tr(1, mast.hash))
|
|
83
|
+
pabtc.rpc.import_descriptors([{
|
|
84
|
+
'desc': pabtc.rpc.get_descriptor_info(f'addr({user_p2tr.addr})')['descriptor'],
|
|
85
|
+
'timestamp': 'now',
|
|
86
|
+
}])
|
|
87
|
+
|
|
88
|
+
# Spending by key path.
|
|
89
|
+
mate.transfer(user_p2tr.script, 1 * pabtc.denomination.bitcoin)
|
|
90
|
+
assert user_p2tr.balance() == pabtc.denomination.bitcoin
|
|
91
|
+
print('main: spending by key path')
|
|
92
|
+
user_p2tr.transfer_all(mate.script)
|
|
93
|
+
assert user_p2tr.balance() == 0
|
|
94
|
+
print('main: spending by key path done')
|
|
95
|
+
|
|
96
|
+
# Spending by script path: pay to public key.
|
|
97
|
+
mate.transfer(user_p2tr.script, 1 * pabtc.denomination.bitcoin)
|
|
98
|
+
assert user_p2tr.balance() == pabtc.denomination.bitcoin
|
|
99
|
+
user_p2pk = pabtc.wallet.Wallet(Tp2trp2pk(user_p2tr.signer.pubkey))
|
|
100
|
+
print('main: spending by script path p2pk')
|
|
101
|
+
user_p2pk.transfer_all(mate.script)
|
|
102
|
+
assert user_p2tr.balance() == 0
|
|
103
|
+
print('main: spending by script path p2pk done')
|
|
104
|
+
|
|
105
|
+
# Spending by script path: pay to 2-of-2 multisig script.
|
|
106
|
+
mate.transfer(user_p2tr.script, 1 * pabtc.denomination.bitcoin)
|
|
107
|
+
assert user_p2tr.balance() == pabtc.denomination.bitcoin
|
|
108
|
+
user_p2ms = pabtc.wallet.Wallet(Tp2trp2ms(user_p2tr.signer.pubkey))
|
|
109
|
+
print('main: spending by script path p2ms')
|
|
110
|
+
user_p2ms.transfer_all(mate.script)
|
|
111
|
+
assert user_p2tr.balance() == 0
|
|
112
|
+
print('main: spending by script path p2ms done')
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import pabtc
|
|
3
|
+
|
|
4
|
+
# Transfer bitcoin to another account.
|
|
5
|
+
|
|
6
|
+
parser = argparse.ArgumentParser()
|
|
7
|
+
parser.add_argument('--net', type=str, choices=['develop', 'mainnet', 'testnet'], default='develop')
|
|
8
|
+
parser.add_argument('--prikey', type=str, help='private key')
|
|
9
|
+
parser.add_argument('--script-type', type=str, choices=['p2pkh', 'p2sh-p2wpkh', 'p2wpkh', 'p2tr'])
|
|
10
|
+
parser.add_argument('--to', type=str, help='bitcoin address')
|
|
11
|
+
parser.add_argument('--value', type=float, help='bitcoin value')
|
|
12
|
+
args = parser.parse_args()
|
|
13
|
+
|
|
14
|
+
if args.net == 'develop':
|
|
15
|
+
pabtc.config.current = pabtc.config.develop
|
|
16
|
+
if args.net == 'mainnet':
|
|
17
|
+
pabtc.config.current = pabtc.config.mainnet
|
|
18
|
+
if args.net == 'testnet':
|
|
19
|
+
pabtc.config.current = pabtc.config.testnet
|
|
20
|
+
|
|
21
|
+
accept_script = pabtc.core.script_pubkey(args.to)
|
|
22
|
+
accept_value = int(args.value * pabtc.denomination.bitcoin)
|
|
23
|
+
prikey = int(args.prikey, 0)
|
|
24
|
+
if args.script_type == 'p2pkh':
|
|
25
|
+
wallet = pabtc.wallet.Wallet(pabtc.wallet.Tp2pkh(prikey))
|
|
26
|
+
if args.script_type == 'p2sh-p2wpkh':
|
|
27
|
+
wallet = pabtc.wallet.Wallet(pabtc.wallet.Tp2shp2wpkh(prikey))
|
|
28
|
+
if args.script_type == 'p2wpkh':
|
|
29
|
+
wallet = pabtc.wallet.Wallet(pabtc.wallet.Tp2wpkh(prikey))
|
|
30
|
+
if args.script_type == 'p2tr':
|
|
31
|
+
wallet = pabtc.wallet.Wallet(pabtc.wallet.Tp2tr(prikey, bytearray()))
|
|
32
|
+
txid = wallet.transfer(accept_script, accept_value)
|
|
33
|
+
print(f'0x{txid.hex()}')
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from . import base58
|
|
2
|
+
from . import bech32
|
|
3
|
+
from . import config
|
|
4
|
+
from . import core
|
|
5
|
+
from . import denomination
|
|
6
|
+
from . import ecdsa
|
|
7
|
+
from . import opcode
|
|
8
|
+
from . import ripemd160
|
|
9
|
+
from . import rpc
|
|
10
|
+
from . import schnorr
|
|
11
|
+
from . import secp256k1
|
|
12
|
+
from . import wallet
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Copyright (C) 2011 Sam Rushing
|
|
2
|
+
# Copyright (C) 2013-2014 The python-bitcoinlib developers
|
|
3
|
+
#
|
|
4
|
+
# This file is part of python-bitcoinlib.
|
|
5
|
+
#
|
|
6
|
+
# It is subject to the license terms in the LICENSE file found in the top-level
|
|
7
|
+
# directory of this distribution.
|
|
8
|
+
#
|
|
9
|
+
# No part of python-bitcoinlib, including this file, may be copied, modified,
|
|
10
|
+
# propagated, or distributed except according to the terms contained in the
|
|
11
|
+
# LICENSE file.
|
|
12
|
+
|
|
13
|
+
# Base58 encoding and decoding
|
|
14
|
+
|
|
15
|
+
B58_DIGITS = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def encode(b: bytearray) -> str:
|
|
19
|
+
# Encode bytes to a base58-encoded string
|
|
20
|
+
assert isinstance(b, bytearray)
|
|
21
|
+
# Convert big-endian bytes to integer
|
|
22
|
+
n = int.from_bytes(b)
|
|
23
|
+
# Divide that integer into bas58
|
|
24
|
+
res = []
|
|
25
|
+
while n > 0:
|
|
26
|
+
n, r = divmod(n, 58)
|
|
27
|
+
res.append(B58_DIGITS[r])
|
|
28
|
+
res = ''.join(res[::-1])
|
|
29
|
+
# Encode leading zeros as base58 zeros
|
|
30
|
+
czero = 0
|
|
31
|
+
pad = 0
|
|
32
|
+
for c in b:
|
|
33
|
+
if c == czero:
|
|
34
|
+
pad += 1
|
|
35
|
+
else:
|
|
36
|
+
break
|
|
37
|
+
return B58_DIGITS[0] * pad + res
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def decode(s: str) -> bytearray:
|
|
41
|
+
# Decode a base58-encoding string, returning bytes.
|
|
42
|
+
if not s:
|
|
43
|
+
return bytearray()
|
|
44
|
+
# Convert the string to an integer
|
|
45
|
+
n = 0
|
|
46
|
+
for c in s:
|
|
47
|
+
n *= 58
|
|
48
|
+
assert c in B58_DIGITS
|
|
49
|
+
digit = B58_DIGITS.index(c)
|
|
50
|
+
n += digit
|
|
51
|
+
# Convert the integer to bytes
|
|
52
|
+
res = bytearray(n.to_bytes(max((n.bit_length() + 7) // 8, 1)))
|
|
53
|
+
# Add padding back.
|
|
54
|
+
pad = 0
|
|
55
|
+
for c in s[:-1]:
|
|
56
|
+
if c == B58_DIGITS[0]:
|
|
57
|
+
pad += 1
|
|
58
|
+
else:
|
|
59
|
+
break
|
|
60
|
+
return bytearray(pad) + res
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# Copyright (c) 2017, 2020 Pieter Wuille
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
# furnished to do so, subject to the following conditions:
|
|
9
|
+
#
|
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
|
11
|
+
# all copies or substantial portions of the Software.
|
|
12
|
+
#
|
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
# THE SOFTWARE.
|
|
20
|
+
#
|
|
21
|
+
# BIP-0173 https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
|
|
22
|
+
# BIP-0350 https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki
|
|
23
|
+
# Derived from https://raw.githubusercontent.com/sipa/bech32/master/ref/python/segwit_addr.py
|
|
24
|
+
#
|
|
25
|
+
# Reference implementation for Bech32/bech32 and segwit addresses.
|
|
26
|
+
|
|
27
|
+
import typing
|
|
28
|
+
|
|
29
|
+
CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l'
|
|
30
|
+
CONST_0 = 1
|
|
31
|
+
CONST_M = 0x2bc830a3
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def bech32_polymod(data: bytearray) -> int:
|
|
35
|
+
# Internal function that computes the Bech32 checksum.
|
|
36
|
+
assert isinstance(data, bytearray)
|
|
37
|
+
gen = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]
|
|
38
|
+
chk = 1
|
|
39
|
+
for val in data:
|
|
40
|
+
top = chk >> 25
|
|
41
|
+
chk = chk & 0x1ffffff
|
|
42
|
+
chk = chk << 5 ^ val
|
|
43
|
+
for i in range(5):
|
|
44
|
+
chk ^= gen[i] if ((top >> i) & 1) else 0
|
|
45
|
+
return chk
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def bech32_hrpconv(hrp: str) -> bytearray:
|
|
49
|
+
# Expand the HRP into values for checksum computation.
|
|
50
|
+
r = bytearray()
|
|
51
|
+
r.extend([ord(x) >> 5 for x in hrp])
|
|
52
|
+
r.append(0)
|
|
53
|
+
r.extend([ord(x) & 31 for x in hrp])
|
|
54
|
+
return r
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def bech32_re_arrange_5(data: bytearray) -> bytearray:
|
|
58
|
+
# Re-arrange those bits into groups of 5, and pad with zeroes at the end if needed.
|
|
59
|
+
assert isinstance(data, bytearray)
|
|
60
|
+
acc = 0
|
|
61
|
+
bit = 0
|
|
62
|
+
ret = bytearray()
|
|
63
|
+
max_val = 0x1f
|
|
64
|
+
max_acc = 0xfff
|
|
65
|
+
for b in data:
|
|
66
|
+
acc = ((acc << 8) | b) & max_acc
|
|
67
|
+
bit += 8
|
|
68
|
+
for _ in range(bit // 5):
|
|
69
|
+
bit -= 5
|
|
70
|
+
ret.append((acc >> bit) & max_val)
|
|
71
|
+
if bit:
|
|
72
|
+
pad_bit = 5 - bit
|
|
73
|
+
ret.append((acc << pad_bit) & max_val)
|
|
74
|
+
return ret
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def bech32_create_checksum(hrp: str, ver: int, data: bytearray) -> bytearray:
|
|
78
|
+
hrpdata = bech32_hrpconv(hrp) + data
|
|
79
|
+
polymod = bech32_polymod(hrpdata + bytearray(6))
|
|
80
|
+
if ver == 0:
|
|
81
|
+
polymod = polymod ^ CONST_0
|
|
82
|
+
if ver >= 1:
|
|
83
|
+
polymod = polymod ^ CONST_M
|
|
84
|
+
return bytearray([(polymod >> 5 * (5 - i)) & 31 for i in range(6)])
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def bech32_verify_checksum(hrp: str, ver: int, data: bytearray) -> bool:
|
|
88
|
+
if ver == 0:
|
|
89
|
+
return bech32_polymod(bech32_hrpconv(hrp) + bytearray(data)) == CONST_0
|
|
90
|
+
if ver >= 1:
|
|
91
|
+
return bech32_polymod(bech32_hrpconv(hrp) + bytearray(data)) == CONST_M
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def bech32_decode(ver: int, bech: str) -> typing.Tuple[str, bytearray]:
|
|
95
|
+
# Validate a string, and determine HRP and data.
|
|
96
|
+
assert len(bech) <= 90
|
|
97
|
+
for c in bech:
|
|
98
|
+
assert ord(c) >= ord('!')
|
|
99
|
+
assert ord(c) <= ord('~')
|
|
100
|
+
bech = bech.lower()
|
|
101
|
+
pos = bech.rfind('1')
|
|
102
|
+
assert pos > 0
|
|
103
|
+
assert pos + 6 < len(bech)
|
|
104
|
+
for c in bech[pos+1:]:
|
|
105
|
+
assert c in CHARSET
|
|
106
|
+
hrp = bech[:pos]
|
|
107
|
+
data = bytearray([CHARSET.find(x) for x in bech[pos+1:]])
|
|
108
|
+
assert bech32_verify_checksum(hrp, ver, data)
|
|
109
|
+
return hrp, data[:-6]
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def bech32_encode(hrp: str, ver: int, data: bytearray) -> str:
|
|
113
|
+
datasum = data + bech32_create_checksum(hrp, ver, data)
|
|
114
|
+
return hrp + '1' + ''.join([CHARSET[d] for d in datasum])
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def bech32_re_arrange_8(data: bytearray) -> bytearray:
|
|
118
|
+
# Re-arrange those bits into groups of 8 bits. Any incomplete group at the end MUST be 4 bits or less, MUST be all
|
|
119
|
+
# zeroes, and is discarded.
|
|
120
|
+
assert isinstance(data, bytearray)
|
|
121
|
+
acc = 0
|
|
122
|
+
bit = 0
|
|
123
|
+
ret = bytearray()
|
|
124
|
+
max_val = 0xff
|
|
125
|
+
max_acc = 0xfff
|
|
126
|
+
for b in data:
|
|
127
|
+
assert b <= 0x1f
|
|
128
|
+
acc = ((acc << 5) | b) & max_acc
|
|
129
|
+
bit += 5
|
|
130
|
+
for _ in range(bit // 8):
|
|
131
|
+
bit -= 8
|
|
132
|
+
ret.append((acc >> bit) & max_val)
|
|
133
|
+
assert bit < 5
|
|
134
|
+
return ret
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def decode(hrp: str, ver: int, addr: str) -> bytearray:
|
|
138
|
+
# Decode a segwit address.
|
|
139
|
+
pre, data = bech32_decode(ver, addr)
|
|
140
|
+
assert pre == hrp
|
|
141
|
+
assert ver == data[0]
|
|
142
|
+
return bech32_re_arrange_8(data[1:])
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def encode(hrp: str, ver: int, prog: bytearray) -> str:
|
|
146
|
+
# Encode a segwit address.
|
|
147
|
+
assert isinstance(prog, bytearray)
|
|
148
|
+
r = bech32_encode(hrp, ver, bytearray([ver]) + bech32_re_arrange_5(prog))
|
|
149
|
+
assert prog == decode(hrp, ver, r)
|
|
150
|
+
return r
|