Skip to content

Commit

Permalink
feat: Allow etching rune with logo file
Browse files Browse the repository at this point in the history
--story=1
  • Loading branch information
studyzy committed Apr 28, 2024
1 parent caf0a90 commit 19666f2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 7 deletions.
42 changes: 42 additions & 0 deletions cmd/runestonecli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"encoding/hex"
"errors"
"net/http"
"os"
"unicode/utf8"

"github.com/btcsuite/btcd/btcec/v2"
Expand All @@ -22,6 +24,7 @@ type Config struct {
RpcUrl string
Etching *struct {
Rune string
Logo string
Symbol *string
Premine *uint64
Amount *uint64
Expand Down Expand Up @@ -184,3 +187,42 @@ func (c Config) GetPrivateKeyAddr() (*btcec.PrivateKey, string, error) {
address := addr.EncodeAddress()
return privKey, address, nil
}
func (c Config) GetRuneLogo() (mime string, data []byte) {
if c.Etching != nil && c.Etching.Logo != "" {
mime, err := getContentType(c.Etching.Logo)
if err != nil {
return "", nil
}
data, err := getFileBytes(c.Etching.Logo)
if err != nil {
return "", nil
}
return mime, data

}
return "", nil
}

func getContentType(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()

buffer := make([]byte, 512)
_, err = file.Read(buffer)
if err != nil {
return "", err
}

contentType := http.DetectContentType(buffer)
return contentType, nil
}
func getFileBytes(filePath string) ([]byte, error) {
fileBytes, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
return fileBytes, nil
}
3 changes: 2 additions & 1 deletion cmd/runestonecli/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ RpcUrl: "https://mempool.space/testnet/api" #https://mempool.space/api https://m
FeePerByte: 10
UtxoAmount: 666
Etching:
Rune: "GOOD.LUCK.COIN"
Rune: "BXE.LAB.COIN"
Logo: "./logo.png"
Symbol: "🪙"
Premine: 1000000
Amount: 1000
Expand Down
Binary file added cmd/runestonecli/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions cmd/runestonecli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ func BuildEtchingTxs() {
btcConnector := NewMempoolConnector(config)
prvKey, address, _ := config.GetPrivateKeyAddr()
utxos, err := btcConnector.GetUtxos(address)

cTx, rTx, err := BuildRuneEtchingTxs(prvKey, utxos, data, commitment, config.GetFeePerByte(), config.GetUtxoAmount(), config.GetNetwork(), address)
var cTx, rTx []byte
mime, logoData := config.GetRuneLogo()
if len(mime) == 0 {
cTx, rTx, err = BuildRuneEtchingTxs(prvKey, utxos, data, commitment, config.GetFeePerByte(), config.GetUtxoAmount(), config.GetNetwork(), address)
} else {
cTx, rTx, err = BuildInscriptionTxs(prvKey, utxos, mime, logoData, config.GetFeePerByte(), config.GetUtxoAmount(), config.GetNetwork(), commitment, data)
}
if err != nil {
p.Println("BuildRuneEtchingTxs error:", err.Error())
return
Expand Down
6 changes: 3 additions & 3 deletions cmd/runestonecli/ordi-tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ const (
MaxStandardTxWeight = blockchain.MaxBlockWeight / 10
)

func BuildInscriptionTxs(privateKey *btcec.PrivateKey, utxo []*Utxo, mime string, content []byte, feeRate int64, revealValue int64, net *chaincfg.Params) ([]byte, []byte, error) {
func BuildInscriptionTxs(privateKey *btcec.PrivateKey, utxo []*Utxo, mime string, content []byte, feeRate int64, revealValue int64, net *chaincfg.Params, inscriptionAddData []byte, opReturnData []byte) ([]byte, []byte, error) {
//build 2 tx, 1 transfer BTC to taproot address, 2 inscription transfer taproot address to another address
pubKey := privateKey.PubKey()
receiver, err := getP2TRAddress(pubKey, net)
if err != nil {
return nil, nil, err
}
// 1. build inscription script
inscriptionScript, err := CreateInscriptionScript(pubKey, mime, content)
inscriptionScript, err := CreateInscriptionScript(pubKey, mime, content, inscriptionAddData)
if err != nil {
return nil, nil, err
}
Expand All @@ -42,7 +42,7 @@ func BuildInscriptionTxs(privateKey *btcec.PrivateKey, utxo []*Utxo, mime string
}
inscriptionPkScript, _ := txscript.PayToAddrScript(inscriptionAddress)
// 2. build reveal tx
revealTx, totalPrevOutput, err := buildEmptyRevealTx(receiver, inscriptionScript, revealValue, feeRate, nil)
revealTx, totalPrevOutput, err := buildEmptyRevealTx(receiver, inscriptionScript, revealValue, feeRate, opReturnData)
if err != nil {
return nil, nil, err
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/runestonecli/tapescript.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ import (
"github.com/btcsuite/btcd/wire"
)

func CreateInscriptionScript(pk *btcec.PublicKey, contentType string, fileBytes []byte) ([]byte, error) {
func CreateInscriptionScript(pk *btcec.PublicKey, contentType string, fileBytes []byte, inscriptionAddData []byte) ([]byte, error) {
builder := txscript.NewScriptBuilder()
//push pubkey
pk32 := schnorr.SerializePubKey(pk)
log.Printf("put pubkey:%x to tapScript", pk32)
builder.AddData(pk32)
builder.AddOp(txscript.OP_CHECKSIG)
//Ordinals script
if len(inscriptionAddData) > 0 {
builder.AddData(inscriptionAddData)
builder.AddOp(txscript.OP_DROP)
}
builder.AddOp(txscript.OP_FALSE)
builder.AddOp(txscript.OP_IF)
builder.AddData([]byte("ord"))
Expand Down

0 comments on commit 19666f2

Please sign in to comment.