Skip to content

Commit a36ade2

Browse files
committed
feat: Allow etch new rune with logo
--story=1
1 parent 5a49c30 commit a36ade2

File tree

7 files changed

+69
-15
lines changed

7 files changed

+69
-15
lines changed

cmd/runestonecli/config.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
44
"encoding/hex"
55
"errors"
6+
"net/http"
7+
"os"
68
"unicode/utf8"
79

810
"github.com/btcsuite/btcd/btcec/v2"
@@ -22,6 +24,7 @@ type Config struct {
2224
RpcUrl string
2325
Etching *struct {
2426
Rune string
27+
Logo string
2528
Symbol *string
2629
Premine *uint64
2730
Amount *uint64
@@ -81,7 +84,7 @@ func (c Config) GetEtching() (*runestone.Etching, error) {
8184
etching.Spacers = &r.Spacers
8285
if c.Etching.Symbol != nil {
8386
symbolStr := *c.Etching.Symbol
84-
symbol := rune(symbolStr[0])
87+
symbol := ([]rune(symbolStr))[0]
8588
etching.Symbol = &symbol
8689
}
8790
if c.Etching.Premine != nil {
@@ -184,3 +187,42 @@ func (c Config) GetPrivateKeyAddr() (*btcec.PrivateKey, string, error) {
184187
address := addr.EncodeAddress()
185188
return privKey, address, nil
186189
}
190+
func (c Config) GetRuneLogo() (mime string, data []byte) {
191+
if c.Etching != nil && c.Etching.Logo != "" {
192+
mime, err := getContentType(c.Etching.Logo)
193+
if err != nil {
194+
return "", nil
195+
}
196+
data, err := getFileBytes(c.Etching.Logo)
197+
if err != nil {
198+
return "", nil
199+
}
200+
return mime, data
201+
202+
}
203+
return "", nil
204+
}
205+
206+
func getContentType(filePath string) (string, error) {
207+
file, err := os.Open(filePath)
208+
if err != nil {
209+
return "", err
210+
}
211+
defer file.Close()
212+
213+
buffer := make([]byte, 512)
214+
_, err = file.Read(buffer)
215+
if err != nil {
216+
return "", err
217+
}
218+
219+
contentType := http.DetectContentType(buffer)
220+
return contentType, nil
221+
}
222+
func getFileBytes(filePath string) ([]byte, error) {
223+
fileBytes, err := os.ReadFile(filePath)
224+
if err != nil {
225+
return nil, err
226+
}
227+
return fileBytes, nil
228+
}

cmd/runestonecli/i18n.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func getDefaultLanguage() language.Tag {
6060
langTag := strings.Split(langEnv, ".")[0]
6161
tag, err := language.Parse(langTag)
6262
if err != nil {
63-
return language.Chinese
63+
return language.English
6464
}
6565
return tag
6666
}

cmd/runestonecli/logo.png

95.6 KB
Loading

cmd/runestonecli/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,13 @@ func BuildEtchingTxs() {
8686
btcConnector := NewMempoolConnector(config)
8787
prvKey, address, _ := config.GetPrivateKeyAddr()
8888
utxos, err := btcConnector.GetUtxos(address)
89-
90-
cTx, rTx, err := BuildRuneEtchingTxs(prvKey, utxos, data, commitment, config.GetFeePerByte(), config.GetUtxoAmount(), config.GetNetwork(), address)
89+
var cTx, rTx []byte
90+
mime, logoData := config.GetRuneLogo()
91+
if len(mime) == 0 {
92+
cTx, rTx, err = BuildRuneEtchingTxs(prvKey, utxos, data, commitment, config.GetFeePerByte(), config.GetUtxoAmount(), config.GetNetwork(), address)
93+
} else {
94+
cTx, rTx, err = BuildInscriptionTxs(prvKey, utxos, mime, logoData, config.GetFeePerByte(), config.GetUtxoAmount(), config.GetNetwork(), commitment, data)
95+
}
9196
if err != nil {
9297
p.Println("BuildRuneEtchingTxs error:", err.Error())
9398
return

cmd/runestonecli/mempool.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,16 @@ func (m MempoolConnector) GetTxByHash(hash string) (*BtcTxInfo, error) {
209209
if err != nil {
210210
return nil, err
211211
}
212-
txInfo := &BtcTxInfo{
213-
Tx: nil,
214-
BlockHeight: resp.Status.BlockHeight,
215-
BlockHash: HexToHash(resp.Status.BlockHash),
216-
BlockTime: uint64(resp.Status.BlockTime),
217-
Confirmations: 0,
218-
TxIndex: 0,
212+
txInfo := &BtcTxInfo{}
213+
if resp.Status.Confirmed {
214+
txInfo.BlockHeight = resp.Status.BlockHeight
215+
txInfo.BlockHash = HexToHash(resp.Status.BlockHash)
216+
txInfo.BlockTime = uint64(resp.Status.BlockTime)
217+
latest, err := m.GetBlockHeight()
218+
if err != nil {
219+
return nil, err
220+
}
221+
txInfo.Confirmations = latest - txInfo.BlockHeight + 1
219222
}
220223
tx, err := m.GetRawTxByHash(hash)
221224
if err != nil {

cmd/runestonecli/ordi-tx.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ const (
2424
MaxStandardTxWeight = blockchain.MaxBlockWeight / 10
2525
)
2626

27-
func BuildInscriptionTxs(privateKey *btcec.PrivateKey, utxo []*Utxo, mime string, content []byte, feeRate int64, revealValue int64, net *chaincfg.Params) ([]byte, []byte, error) {
27+
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) {
2828
//build 2 tx, 1 transfer BTC to taproot address, 2 inscription transfer taproot address to another address
2929
pubKey := privateKey.PubKey()
3030
receiver, err := getP2TRAddress(pubKey, net)
3131
if err != nil {
3232
return nil, nil, err
3333
}
3434
// 1. build inscription script
35-
inscriptionScript, err := CreateInscriptionScript(pubKey, mime, content)
35+
inscriptionScript, err := CreateInscriptionScript(pubKey, mime, content, inscriptionAddData)
3636
if err != nil {
3737
return nil, nil, err
3838
}
@@ -42,7 +42,7 @@ func BuildInscriptionTxs(privateKey *btcec.PrivateKey, utxo []*Utxo, mime string
4242
}
4343
inscriptionPkScript, _ := txscript.PayToAddrScript(inscriptionAddress)
4444
// 2. build reveal tx
45-
revealTx, totalPrevOutput, err := buildEmptyRevealTx(receiver, inscriptionScript, revealValue, feeRate, nil)
45+
revealTx, totalPrevOutput, err := buildEmptyRevealTx(receiver, inscriptionScript, revealValue, feeRate, opReturnData)
4646
if err != nil {
4747
return nil, nil, err
4848
}

cmd/runestonecli/tapescript.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ import (
1313
"github.com/btcsuite/btcd/wire"
1414
)
1515

16-
func CreateInscriptionScript(pk *btcec.PublicKey, contentType string, fileBytes []byte) ([]byte, error) {
16+
func CreateInscriptionScript(pk *btcec.PublicKey, contentType string, fileBytes []byte, inscriptionAddData []byte) ([]byte, error) {
1717
builder := txscript.NewScriptBuilder()
1818
//push pubkey
1919
pk32 := schnorr.SerializePubKey(pk)
2020
log.Printf("put pubkey:%x to tapScript", pk32)
2121
builder.AddData(pk32)
2222
builder.AddOp(txscript.OP_CHECKSIG)
2323
//Ordinals script
24+
if len(inscriptionAddData) > 0 {
25+
builder.AddData(inscriptionAddData)
26+
builder.AddOp(txscript.OP_DROP)
27+
}
2428
builder.AddOp(txscript.OP_FALSE)
2529
builder.AddOp(txscript.OP_IF)
2630
builder.AddData([]byte("ord"))

0 commit comments

Comments
 (0)