Skip to content

Instantly share code, notes, and snippets.

@jaonoctus
Last active April 13, 2022 02:57
Show Gist options
  • Save jaonoctus/160bed37f2ebc767b3c29c93fe02eb6f to your computer and use it in GitHub Desktop.
Save jaonoctus/160bed37f2ebc767b3c29c93fe02eb6f to your computer and use it in GitHub Desktop.
#! /usr/bin/env python3
import argparse
import json
from decimal import Decimal, ROUND_DOWN
from random import randrange
COIN = 100000000
SATOSHI = Decimal(0.00000001)
KILO = 1000
def satoshi_round(amount):
return Decimal(amount).quantize(Decimal('0.00000001'), rounding=ROUND_DOWN)
def to_sat(amount):
return int(amount * COIN)
def to_coin(amount):
return satoshi_round(amount / COIN)
parser = argparse.ArgumentParser(description="Generates a simulation scenario from a Bitcoin Core wallet. Requires Bitcoin Core 0.19.0 or later.")
parser.add_argument("inputfile", help="File to read transactions")
parser.add_argument("outputfile", help="File to output to")
args = parser.parse_args()
# Fetch all of the transactions in the wallet
inputfile = open(args.inputfile)
txs = json.load(inputfile)
with open(args.outputfile, "w") as f:
for tx in txs:
# Bitcoin Core gives us the amount as positive for receiving, and negative for sending
amount = abs(to_sat(tx["amount"]))
# To protect user privacy, fuzzify by adding or subtracting a random percentage up to 5%
lower = int(amount * 0.95)
upper = int(amount * 1.05)
amount = to_coin(randrange(lower, upper, 1))
# Get feerate
if tx["category"] == "send":
amount = -amount
# Use a feerate of 1 sat/vb for deposits
feerate = satoshi_round(SATOSHI * KILO)
f.write(f"{amount:.8f},{feerate:.8f}\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment