-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalize txmaker to most supported policies
- Loading branch information
Showing
4 changed files
with
715 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# from portions of BIP-0341 | ||
# - https://github.com/bitcoin/bips/blob/b3701faef2bdb98a0d7ace4eedbeefa2da4c89ed/bip-0341.mediawiki | ||
# Distributed under the BSD-3-Clause license | ||
|
||
# fmt: off | ||
|
||
# If you want to print values on an individual basis, use | ||
# the pretty() function, e.g., print(pretty(foo)). | ||
import hashlib | ||
import struct | ||
|
||
|
||
# This implementation can be sped up by storing the midstate after hashing | ||
# tag_hash instead of rehashing it all the time. | ||
def tagged_hash(tag: str, msg: bytes) -> bytes: | ||
tag_hash = hashlib.sha256(tag.encode()).digest() | ||
return hashlib.sha256(tag_hash + tag_hash + msg).digest() | ||
|
||
|
||
def ser_compact_size(l): | ||
r = b"" | ||
if l < 253: | ||
r = struct.pack("B", l) | ||
elif l < 0x10000: | ||
r = struct.pack("<BH", 253, l) | ||
elif l < 0x100000000: | ||
r = struct.pack("<BI", 254, l) | ||
else: | ||
r = struct.pack("<BQ", 255, l) | ||
return r | ||
|
||
def deser_compact_size(f): | ||
nit = struct.unpack("<B", f.read(1))[0] | ||
if nit == 253: | ||
nit = struct.unpack("<H", f.read(2))[0] | ||
elif nit == 254: | ||
nit = struct.unpack("<I", f.read(4))[0] | ||
elif nit == 255: | ||
nit = struct.unpack("<Q", f.read(8))[0] | ||
return nit | ||
|
||
def deser_string(f): | ||
nit = deser_compact_size(f) | ||
return f.read(nit) | ||
|
||
def ser_string(s): | ||
return ser_compact_size(len(s)) + s | ||
|
||
def ser_script(s): | ||
return ser_string(s) |
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
Oops, something went wrong.