Skip to content

Instantly share code, notes, and snippets.

import { Utils, hash160, PubKey, Sig, assert, ByteString, hash256, method, prop, SmartContract } from 'scrypt-ts'
export class Payment extends SmartContract {
static readonly LOCKTIME_BLOCK_HEIGHT_MARKER = 500000000
static readonly UINT_MAX = 0xffffffffn
// withdraw interval limit in seconds
@prop()
withdrawIntervals: bigint;
import { MethodCallOptions, PubKey, PubKeyHash, toHex } from 'scrypt-ts'
import { Pyramid } from '../../src/contracts/pyramid'
import {
getDummySigner,
getDummyUTXO,
randomPrivateKey,
} from '../utils/txHelper'
import { myPublicKeyHash, myAddress } from '../utils/privateKey'
import { expect } from 'chai'
import { assert } from 'console'
import {
PubKey,
PubKeyHash,
SmartContract,
Utils,
hash256,
method,
prop,
hash160,
from typing import Literal
def unsigned_to_bytes(num: int, byteorder: Literal['big', 'little'] = 'big') -> bytes:
"""
convert an unsigned int to the least number of bytes as possible.
"""
# (0).bit_length() == 0
return num.to_bytes((num.bit_length() + 7) // 8 or 1, byteorder)
@gitzhou
gitzhou / rfc6979.py
Created February 1, 2022 22:04
RFC6979 Python demo implementation
import hashlib
import hmac
import math
from typing import Union, Literal
def unsigned_to_bytes(num: int, byteorder: Literal['big', 'little'] = 'big') -> bytes:
"""
convert an unsigned int to the least number of bytes as possible.
"""
import hashlib
def hash_to_int(x: bytes) -> int:
hx = hashlib.sha256(x).digest()
for i in range(11):
hx += hashlib.sha256(hx).digest()
return int.from_bytes(hx, 'little')
from meta import int_to_varint, public_key_to_address, address_to_public_key_hash, public_key_hash
from modular_inverse import modular_multiplicative_inverse
from ec_point_operation import curve, add, scalar_multiply
from sign import hash_to_int, sign_recoverable, verify_signature
from base64 import b64encode, b64decode
def message_bytes(message: str) -> bytes:
msg_bytes = message.encode('utf-8')
return int_to_varint(len(msg_bytes)) + msg_bytes
from ec_point_operation import curve, scalar_multiply
from meta import int_to_varint, address_to_public_key_hash, build_locking_script, deserialize_signature, serialize_signature, serialize_public_key
from crypto import double_sha256
from sign import verify_signature, sign
from collections import namedtuple
from binascii import unhexlify, hexlify
VERSION = 0x01.to_bytes(4, 'little')
SEQUENCE = 0xffffffff.to_bytes(4, byteorder='little')
LOCK_TIME = 0x00.to_bytes(4, byteorder='little')
@gitzhou
gitzhou / sign.py
Last active October 16, 2020 18:56
from ec_point_operation import curve, add, scalar_multiply
from modular_inverse import modular_multiplicative_inverse
from crypto import double_sha256
import random
def hash_to_int(message: bytes) -> int:
"""Calculate the bitcoin double-sha256 hash of the message, return as an integer"""
h = double_sha256(message)
return int.from_bytes(h, byteorder='big')
import collections
from modular_inverse import modular_multiplicative_inverse
EllipticCurve = collections.namedtuple('EllipticCurve', 'name p a b g n h')
curve = EllipticCurve(
name='Secp256k1',
p=0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f,
a=0,
b=7,