Skip to content

Commit

Permalink
feat: Allow etch new rune with logo
Browse files Browse the repository at this point in the history
--story=1
  • Loading branch information
studyzy committed Apr 28, 2024
1 parent 5a49c30 commit a36ade2
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 15 deletions.
44 changes: 43 additions & 1 deletion 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 @@ -81,7 +84,7 @@ func (c Config) GetEtching() (*runestone.Etching, error) {
etching.Spacers = &r.Spacers
if c.Etching.Symbol != nil {
symbolStr := *c.Etching.Symbol
symbol := rune(symbolStr[0])
symbol := ([]rune(symbolStr))[0]
etching.Symbol = &symbol
}
if c.Etching.Premine != nil {
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
}
2 changes: 1 addition & 1 deletion cmd/runestonecli/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func getDefaultLanguage() language.Tag {
langTag := strings.Split(langEnv, ".")[0]
tag, err := language.Parse(langTag)
if err != nil {
return language.Chinese
return language.English
}
return tag
}
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
17 changes: 10 additions & 7 deletions cmd/runestonecli/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,16 @@ func (m MempoolConnector) GetTxByHash(hash string) (*BtcTxInfo, error) {
if err != nil {
return nil, err
}
txInfo := &BtcTxInfo{
Tx: nil,
BlockHeight: resp.Status.BlockHeight,
BlockHash: HexToHash(resp.Status.BlockHash),
BlockTime: uint64(resp.Status.BlockTime),
Confirmations: 0,
TxIndex: 0,
txInfo := &BtcTxInfo{}
if resp.Status.Confirmed {
txInfo.BlockHeight = resp.Status.BlockHeight
txInfo.BlockHash = HexToHash(resp.Status.BlockHash)
txInfo.BlockTime = uint64(resp.Status.BlockTime)
latest, err := m.GetBlockHeight()
if err != nil {
return nil, err
}
txInfo.Confirmations = latest - txInfo.BlockHeight + 1
}
tx, err := m.GetRawTxByHash(hash)
if err != nil {
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 a36ade2

Please sign in to comment.