-
Notifications
You must be signed in to change notification settings - Fork 26
/
inscriptions_test.go
170 lines (140 loc) · 7.84 KB
/
inscriptions_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package bt
import (
"encoding/base64"
"encoding/hex"
"fmt"
"io/ioutil"
"log"
"mime"
"testing"
"github.com/libsv/go-bk/wif"
"github.com/libsv/go-bt/v2/bscript"
"github.com/stretchr/testify/assert"
)
func TestInscribe(t *testing.T) {
t.Parallel()
s, _ := bscript.NewP2PKHFromAddress("mxAoAyZFXX6LZBWhoam3vjm6xt9NxPQ15f")
tests := map[string]struct {
ia *bscript.InscriptionArgs
}{
"text/plain with 'Hello, world!'": {
ia: &bscript.InscriptionArgs{
LockingScriptPrefix: s,
Data: []byte("Hello, world!"),
ContentType: "text/plain;charset=utf-8",
},
}, "model/gltf-binary 3d model'": {
ia: &bscript.InscriptionArgs{
LockingScriptPrefix: s,
Data: func() []byte {
b, _ := base64.StdEncoding.DecodeString("Z2xURgIAAABobgwAbAcAAEpTT057ImFzc2V0Ijp7ImdlbmVyYXRvciI6Ik1pY3Jvc29mdCBHTFRGIEV4cG9ydGVyIDIuOC4zLjQwIiwidmVyc2lvbiI6IjIuMCJ9LCJhY2Nlc3NvcnMiOlt7ImJ1ZmZlclZpZXciOjAsImNvbXBvbmVudFR5cGUiOjUxMjUsImNvdW50IjozNDgsInR5cGUiOiJTQ0FMQVI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aCJ9LHsibWVzaCI6MCwibmFtZSI6ImNyeXN0YWxMb3c6TWVzaF9sYW1iZXJ0NF8wIn1dLCJzYW1wbGVycyI6W3sibWFnRmlsdGVyIjo5NzI5LCJtaW5GaWx0ZXIiOjk5ODd9XSwic2NlbmVzIjpbeyJub2RlcyI6WzBdfV0sInRleHR1cmVzIjpbeyJzYW1wbGVyIjowLCJzb3VyY2UiOjB9XSwic2NlbmUiOjB94GYMAEJJTgAAAAAAAQAAAAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAgAAAAJAAAACgAAAAsAAAAMAAAADQAAAA4AAAAPAAAAEAAAABEAAAASAAAAEwAAABQAAAAVAAAAFgAAABcAAAAYAAAAGQAAABoAAAAbAAAAHAAAAB0AAAAeAAAAHwAAACAAAAAhAAAAIgAAACMAAAAkAAAAJQAAACYAAAAnAAAAKAAAACkAAAAqAAAAKwAAACwAAAAtAAAALgAAAC8AAAAwAAAAMQAAADIAAAAzAAAANAAAADUAAAA2AAAANwAAADgAAAA5AAAAOgAAADsAAAA8AAAAPQAAAD4AAAA")
return b
}(),
ContentType: "model/gltf-binary",
},
}}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
// Create a new transaction
tx := NewTx()
// Call the Inscribe function with the test data
err := tx.Inscribe(test.ia)
assert.NoError(t, err)
// Check if the transaction has the expected number of outputs
if len(tx.Outputs) != 1 {
t.Fatalf("Inscribe failed: expected 1 output, got %d", len(tx.Outputs))
}
// Check if the output has the expected Satoshis value
if tx.Outputs[0].Satoshis != 1 {
t.Fatalf("Inscribe failed: expected Satoshis value 1, got %d", tx.Outputs[0].Satoshis)
}
// Check if the output has the expected LockingScript value
expectedLockingScript := *s
_ = expectedLockingScript.AppendOpcodes(bscript.OpFALSE, bscript.OpIF)
_ = expectedLockingScript.AppendPushDataString(OrdinalsPrefix)
_ = expectedLockingScript.AppendOpcodes(bscript.Op1)
_ = expectedLockingScript.AppendPushData([]byte(test.ia.ContentType))
_ = expectedLockingScript.AppendOpcodes(bscript.Op0)
_ = expectedLockingScript.AppendPushData(test.ia.Data)
_ = expectedLockingScript.AppendOpcodes(bscript.OpENDIF)
if !tx.Outputs[0].LockingScript.Equals(&expectedLockingScript) {
t.Fatalf("Inscribe failed: expected LockingScript value %s, got %s", expectedLockingScript.String(), tx.Outputs[0].LockingScript.String())
}
})
}
}
func TestMultipleInscriptionsIn1Tx(t *testing.T) {
decodedWif, _ := wif.DecodeWIF("KznpA63DPFrmHecASyL6sFmcRgrNT9oM8Ebso8mwq1dfJF3ZgZ3V")
// get public key bytes and address
pubkey := decodedWif.SerialisePubKey()
addr, _ := bscript.NewAddressFromPublicKeyString(hex.EncodeToString(pubkey), true)
s, _ := bscript.NewP2PKHFromAddress(addr.AddressString)
tx := NewTx()
_ = tx.From(
"39e5954ee335fdb5a1368ab9e851a954ed513f73f6e8e85eff5e31adbb5837e7",
0,
"76a9144bca0c466925b875875a8e1355698bdcc0b2d45d88ac",
500,
)
tx.Inscribe(&bscript.InscriptionArgs{
LockingScriptPrefix: s,
Data: []byte("Hello, world!"),
ContentType: "text/plain;charset=utf-8",
})
tx.Inscribe(&bscript.InscriptionArgs{
LockingScriptPrefix: s,
Data: []byte("Hello, world!"),
ContentType: "text/plain;charset=utf-8",
})
tx.Inscribe(&bscript.InscriptionArgs{
LockingScriptPrefix: s,
Data: []byte("Hello, world!"),
ContentType: "text/plain;charset=utf-8",
})
// Check if the output has the expected LockingScript value
expectedLockingScript := s
_ = expectedLockingScript.AppendOpcodes(bscript.OpFALSE, bscript.OpIF)
_ = expectedLockingScript.AppendPushDataString(OrdinalsPrefix)
_ = expectedLockingScript.AppendOpcodes(bscript.Op1)
_ = expectedLockingScript.AppendPushData([]byte("text/plain;charset=utf-8"))
_ = expectedLockingScript.AppendOpcodes(bscript.Op0)
_ = expectedLockingScript.AppendPushData([]byte("Hello, world!"))
_ = expectedLockingScript.AppendOpcodes(bscript.OpENDIF)
for i := range tx.Outputs {
if !tx.Outputs[i].LockingScript.Equals(expectedLockingScript) {
t.Fatalf("Inscribe failed: expected LockingScript value %s, got %s at index %d", expectedLockingScript.String(), tx.Outputs[i].LockingScript.String(), i)
}
}
log.Println("tx: ", tx.String())
}
func TestInscribeFromFile(t *testing.T) {
decodedWif, _ := wif.DecodeWIF("KznpA63DPFrmHecASyL6sFmcRgrNT9oM8Ebso8mwq1dfJF3ZgZ3V")
// get public key bytes and address
pubkey := decodedWif.SerialisePubKey()
addr, _ := bscript.NewAddressFromPublicKeyString(hex.EncodeToString(pubkey), true)
s, _ := bscript.NewP2PKHFromAddress(addr.AddressString)
tx := NewTx()
_ = tx.From(
"39e5954ee335fdb5a1368ab9e851a954ed513f73f6e8e85eff5e31adbb5837e7",
0,
"76a9144bca0c466925b875875a8e1355698bdcc0b2d45d88ac",
500,
)
// Read the image file
data, err := ioutil.ReadFile("./examples/create_tx_with_inscription/1SatLogoLight.png")
if err != nil {
fmt.Println(err)
return
}
// Get the content type of the image
contentType := mime.TypeByExtension(".png")
tx.Inscribe(&bscript.InscriptionArgs{
LockingScriptPrefix: s,
Data: data,
ContentType: contentType,
})
err = tx.ChangeToAddress("17ujiveRLkf2JQiGR8Sjtwb37evX7vG3WG", NewFeeQuote())
if err != nil {
log.Fatal(err.Error())
}
}