|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "errors" |
| 6 | + "unicode/utf8" |
| 7 | + |
| 8 | + "github.com/btcsuite/btcd/btcec/v2" |
| 9 | + "github.com/btcsuite/btcd/btcec/v2/schnorr" |
| 10 | + "github.com/btcsuite/btcd/btcutil" |
| 11 | + "github.com/btcsuite/btcd/chaincfg" |
| 12 | + "github.com/btcsuite/btcd/txscript" |
| 13 | + "github.com/bxelab/runestone" |
| 14 | + "lukechampine.com/uint128" |
| 15 | +) |
| 16 | + |
| 17 | +type Config struct { |
| 18 | + PrivateKey string |
| 19 | + FeePerByte int64 |
| 20 | + UtxoAmount int64 |
| 21 | + Network string |
| 22 | + RpcUrl string |
| 23 | + Etching *struct { |
| 24 | + Rune string |
| 25 | + Symbol *string |
| 26 | + Premine *uint64 |
| 27 | + Amount *uint64 |
| 28 | + Cap *uint64 |
| 29 | + Divisibility *int |
| 30 | + HeightStart *int |
| 31 | + HeightEnd *int |
| 32 | + HeightOffsetStart *int |
| 33 | + HeightOffsetEnd *int |
| 34 | + } |
| 35 | + Mint *struct { |
| 36 | + RuneId string |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func DefaultConfig() Config { |
| 41 | + return Config{ |
| 42 | + FeePerByte: 5, |
| 43 | + UtxoAmount: 1000, |
| 44 | + Network: "mainnet", |
| 45 | + RpcUrl: "https://mempool.space/api", |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | +func (c Config) GetFeePerByte() int64 { |
| 50 | + if c.FeePerByte == 0 { |
| 51 | + return 5 |
| 52 | + } |
| 53 | + return c.FeePerByte |
| 54 | +} |
| 55 | +func (c Config) GetUtxoAmount() int64 { |
| 56 | + if c.UtxoAmount == 0 { |
| 57 | + return 666 |
| 58 | + } |
| 59 | + return c.UtxoAmount |
| 60 | +} |
| 61 | + |
| 62 | +func (c Config) GetEtching() (*runestone.Etching, error) { |
| 63 | + if c.Etching == nil { |
| 64 | + return nil, errors.New("Etching config is required") |
| 65 | + } |
| 66 | + if c.Etching.Rune == "" { |
| 67 | + return nil, errors.New("Rune is required") |
| 68 | + } |
| 69 | + if c.Etching.Symbol != nil { |
| 70 | + runeCount := utf8.RuneCountInString(*c.Etching.Symbol) |
| 71 | + if runeCount != 1 { |
| 72 | + return nil, errors.New("Symbol must be a single character") |
| 73 | + } |
| 74 | + } |
| 75 | + etching := &runestone.Etching{} |
| 76 | + r, err := runestone.SpacedRuneFromString(c.Etching.Rune) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + etching.Rune = &r.Rune |
| 81 | + etching.Spacers = &r.Spacers |
| 82 | + if c.Etching.Symbol != nil { |
| 83 | + symbolStr := *c.Etching.Symbol |
| 84 | + symbol := rune(symbolStr[0]) |
| 85 | + etching.Symbol = &symbol |
| 86 | + } |
| 87 | + if c.Etching.Premine != nil { |
| 88 | + premine := uint128.From64(*c.Etching.Premine) |
| 89 | + etching.Premine = &premine |
| 90 | + } |
| 91 | + if c.Etching.Amount != nil { |
| 92 | + amount := uint128.From64(*c.Etching.Amount) |
| 93 | + if etching.Terms == nil { |
| 94 | + etching.Terms = &runestone.Terms{} |
| 95 | + } |
| 96 | + etching.Terms.Amount = &amount |
| 97 | + } |
| 98 | + if c.Etching.Cap != nil { |
| 99 | + cap := uint128.From64(*c.Etching.Cap) |
| 100 | + etching.Terms.Cap = &cap |
| 101 | + } |
| 102 | + if c.Etching.Divisibility != nil { |
| 103 | + d := uint8(*c.Etching.Divisibility) |
| 104 | + etching.Divisibility = &d |
| 105 | + } |
| 106 | + if c.Etching.HeightStart != nil { |
| 107 | + h := uint64(*c.Etching.HeightStart) |
| 108 | + if etching.Terms == nil { |
| 109 | + etching.Terms = &runestone.Terms{} |
| 110 | + } |
| 111 | + etching.Terms.Height[0] = &h |
| 112 | + } |
| 113 | + if c.Etching.HeightEnd != nil { |
| 114 | + h := uint64(*c.Etching.HeightEnd) |
| 115 | + if etching.Terms == nil { |
| 116 | + etching.Terms = &runestone.Terms{} |
| 117 | + } |
| 118 | + etching.Terms.Height[1] = &h |
| 119 | + } |
| 120 | + if c.Etching.HeightOffsetStart != nil { |
| 121 | + h := uint64(*c.Etching.HeightOffsetStart) |
| 122 | + if etching.Terms == nil { |
| 123 | + etching.Terms = &runestone.Terms{} |
| 124 | + } |
| 125 | + etching.Terms.Offset[0] = &h |
| 126 | + } |
| 127 | + if c.Etching.HeightOffsetEnd != nil { |
| 128 | + h := uint64(*c.Etching.HeightOffsetEnd) |
| 129 | + if etching.Terms == nil { |
| 130 | + etching.Terms = &runestone.Terms{} |
| 131 | + } |
| 132 | + etching.Terms.Offset[1] = &h |
| 133 | + } |
| 134 | + return etching, nil |
| 135 | +} |
| 136 | +func (c Config) GetMint() (*runestone.RuneId, error) { |
| 137 | + if c.Mint == nil { |
| 138 | + return nil, errors.New("Mint config is required") |
| 139 | + } |
| 140 | + if c.Mint.RuneId == "" { |
| 141 | + return nil, errors.New("RuneId is required") |
| 142 | + } |
| 143 | + runeId, err := runestone.RuneIdFromString(c.Mint.RuneId) |
| 144 | + if err != nil { |
| 145 | + return nil, err |
| 146 | + } |
| 147 | + return runeId, nil |
| 148 | +} |
| 149 | +func (c Config) GetNetwork() *chaincfg.Params { |
| 150 | + if c.Network == "mainnet" { |
| 151 | + return &chaincfg.MainNetParams |
| 152 | + } |
| 153 | + if c.Network == "testnet" { |
| 154 | + return &chaincfg.TestNet3Params |
| 155 | + } |
| 156 | + if c.Network == "regtest" { |
| 157 | + return &chaincfg.RegressionNetParams |
| 158 | + } |
| 159 | + if c.Network == "signet" { |
| 160 | + return &chaincfg.SigNetParams |
| 161 | + } |
| 162 | + panic("unknown network") |
| 163 | +} |
| 164 | + |
| 165 | +func (c Config) GetPrivateKeyAddr() (*btcec.PrivateKey, string, error) { |
| 166 | + if c.PrivateKey == "" { |
| 167 | + return nil, "", errors.New("PrivateKey is required") |
| 168 | + } |
| 169 | + pkBytes, err := hex.DecodeString(c.PrivateKey) |
| 170 | + if err != nil { |
| 171 | + return nil, "", err |
| 172 | + } |
| 173 | + privKey, pubKey := btcec.PrivKeyFromBytes(pkBytes) |
| 174 | + if err != nil { |
| 175 | + return nil, "", err |
| 176 | + } |
| 177 | + tapKey := txscript.ComputeTaprootKeyNoScript(pubKey) |
| 178 | + addr, err := btcutil.NewAddressTaproot( |
| 179 | + schnorr.SerializePubKey(tapKey), c.GetNetwork(), |
| 180 | + ) |
| 181 | + if err != nil { |
| 182 | + return nil, "", err |
| 183 | + } |
| 184 | + address := addr.EncodeAddress() |
| 185 | + return privKey, address, nil |
| 186 | +} |
0 commit comments