Files
zeekay 1943f1c093 Hanzo Commerce
Multi-tenant commerce and billing: orders, subscriptions, invoices, plans,
usage metering and a credit ledger. One Go binary. MIT OR Apache-2.0.

Claude-Session: https://claude.ai/code/session_01SAtaDFm8VDiWrhYHNNtxMw
2026-09-23 21:52:16 -07:00

135 lines
3.2 KiB
Go

package bitcoin
import (
"encoding/hex"
"encoding/json"
)
// A Destination is the boiled-back simplistic form of a Bitcoin Output,
// denoting only the basics - where you want the money to end up, and how much
// of it you want there.
type Destination struct {
Value int64
Address string
}
// An Origin is the boiled-back simpistic form of a Bitcoin Input, denoting
// only the basics - where you want the money to come from.
type Origin struct {
TxId string
OutputIndex int
}
// An OriginWithAmount is the boiled-back simpistic form of a Bitcoin Input, denoting
// where you want the money to come from and the amount.
type OriginWithAmount struct {
Origin
Amount int64
}
// An Input is a slightly more complete form of a Bitcoin Input, denoting
// everything the base algorithms need to do their job.
type Input struct {
TxId string
OutputIndex int
ScriptSig []byte // This is not to be confused with the 'Script' of the Output. This is the key to the Script's lock.
}
// An Output is a slightly more complete from of a Bitcoin Output, denoting
// everything the base algorithms need to do their job.
type Output struct {
Value int64
Script []byte
}
type Sender struct {
PrivateKey string
PublicKey string
Address string
}
type GetRawTransactionResponseResult struct {
Txid string
Hash string
Version int
Size int
Vsize int
Locktime int
Vin []GetRawTransactionResponseResultInput
Vout []GetRawTransactionResponseResultOutput
Hex string
Blockhash string
Time int64
Blocktime int64
}
type GetRawTransactionResponseResultInput struct {
Txid string
Vout int
Scriptsig GetRawTransactionResponseResultInputSig
Txinwitness []string
Sequence int64
}
type GetRawTransactionResponseResultInputSig struct {
Asm string
Hex string
}
type GetRawTransactionResponseResultOutput struct {
N int
// Value is json.Number, not float64, so the decimal digits bitcoind sent
// survive to be converted exactly. Through a float64 they do not: 1.15 has
// no exact binary form, so the value is 1.14999999… and int64(value*1e8)
// truncated it to 114999999 — a satoshi short of the output being spent,
// which lands in the change and the fee of a real transaction.
Value json.Number
Scriptpubkey GetRawTransactionResponseResultOutputScriptPubKey
}
type GetRawTransactionResponseResultOutputScriptPubKey struct {
Asm string
Hex string
ReqSigs int
ScriptType string `json:"type"`
Addresses []string
}
type GetRawTransactionResponseError struct {
Code int
Message string
}
func InputToOrigin(in Input) Origin {
return Origin{
TxId: in.TxId,
OutputIndex: in.OutputIndex,
}
}
func OriginToInput(o Origin) Input {
blank, _ := hex.DecodeString("")
return Input{
TxId: o.TxId,
OutputIndex: o.OutputIndex,
ScriptSig: blank,
}
}
func DestinationToOutput(d Destination) Output {
pubKey, err := CreateScriptPubKey(d.Address)
if err != nil {
panic(err)
}
return Output{
Value: d.Value,
Script: pubKey,
}
}
// There's no Output to Destination because half the point of using the
// pay-to-pubkey-hash scripts the way we do is that you can't tell what the
// public key is from looking at it.