Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
7d8559b
first create get tx data
KhanhChung2k5 Jun 10, 2023
2d4eba2
Add amount parameter for case B -> C and render nft based on witness
lenhatquang97 Jun 12, 2023
f0360ad
NFT response
lenhatquang97 Jun 12, 2023
363e7a4
Wrapper Error
KhanhChung2k5 Jun 12, 2023
99bdb66
Add get tx ref
KhanhChung2k5 Jun 12, 2023
e21e5e8
Add get tx ref
KhanhChung2k5 Jun 12, 2023
0542fb3
add send tx ref
KhanhChung2k5 Jun 12, 2023
59f4a73
Hardcode NFT for returning and fee calculation
lenhatquang97 Jun 12, 2023
b327749
recalculate fee
KhanhChung2k5 Jun 12, 2023
c01374c
[Big update] Fix bug about too expensive fee, Path from A to B to C. …
lenhatquang97 Jun 13, 2023
406dc3b
Change commit transaction fee multiply with 3
lenhatquang97 Jun 13, 2023
7d5f441
Remove redundant files
lenhatquang97 Jun 13, 2023
5c41355
Add filter for excluding transactions which have NFTs
lenhatquang97 Jun 13, 2023
9799f63
Fix case when calculating balance except NFT UTXO
lenhatquang97 Jun 14, 2023
8844cfb
Fix transferring into array
lenhatquang97 Jun 14, 2023
e0a4c15
get tx size
KhanhChung2k5 Jun 14, 2023
ad60a58
test result
KhanhChung2k5 Jun 14, 2023
5972d37
test result
KhanhChung2k5 Jun 14, 2023
d11d209
test result
KhanhChung2k5 Jun 14, 2023
90d27fd
add send tx ref 2
KhanhChung2k5 Jun 14, 2023
831e12d
Config send inscription so that can ref to originTxId and remove dumm…
lenhatquang97 Jun 14, 2023
3987c8a
Remove comment in main.go
lenhatquang97 Jun 14, 2023
0a0d398
Fix to off-chain
KhanhChung2k5 Jun 14, 2023
91c71df
render tree and fix export proot
KhanhChung2k5 Jun 15, 2023
383ea8e
clean code
KhanhChung2k5 Jun 15, 2023
ac197cd
clean db
KhanhChung2k5 Jun 15, 2023
bf5b14c
clean code
KhanhChung2k5 Jun 15, 2023
b957815
remove un used code
KhanhChung2k5 Jun 15, 2023
82f300f
Remove redundant parameter
lenhatquang97 Jun 15, 2023
0f9202c
Add default address and bug related to commit tx
lenhatquang97 Jun 15, 2023
047fb5e
Change the way we sign the transaction
lenhatquang97 Jun 15, 2023
0a347a2
Remove SignTx manually
lenhatquang97 Jun 15, 2023
5dd8d06
remove un used code
KhanhChung2k5 Jun 15, 2023
448a3ef
remove un used code
KhanhChung2k5 Jun 15, 2023
7f95e32
IPFS link creation and import
lenhatquang97 Jun 15, 2023
4243622
remove un used code
KhanhChung2k5 Jun 15, 2023
4a4d285
Fix dummy bug about fileLink response
lenhatquang97 Jun 15, 2023
c38f9ce
fix render tree with branch node
KhanhChung2k5 Jun 16, 2023
96948ca
Merge branch 'Kiet' of github.com:lenhatquang97/bitcoin_nft_v2 into Kiet
KhanhChung2k5 Jun 16, 2023
3d20af9
DownloadOnIpfs function
lenhatquang97 Jun 16, 2023
fefdb42
Add godotenv to read public key from .env
lenhatquang97 Jun 16, 2023
d065173
fix tree print just one leaf
KhanhChung2k5 Jun 16, 2023
1f76e4f
Merge branch 'Kiet' of github.com:lenhatquang97/bitcoin_nft_v2 into Kiet
KhanhChung2k5 Jun 16, 2023
ffc6dfe
Change name in Makefile
lenhatquang97 Jun 17, 2023
7876b9e
fix send nft just send one leaf
KhanhChung2k5 Jun 18, 2023
96da187
Merge branch 'Kiet' of github.com:lenhatquang97/bitcoin_nft_v2 into Kiet
KhanhChung2k5 Jun 18, 2023
1e91ab7
Change 3 to 2.5 MB
lenhatquang97 Jun 18, 2023
258abb0
fix send nft just send one leaf
KhanhChung2k5 Jun 18, 2023
dba74a0
Merge branch 'Kiet' of github.com:lenhatquang97/bitcoin_nft_v2 into Kiet
KhanhChung2k5 Jun 18, 2023
15a3771
view nft and tx id
KhanhChung2k5 Jun 18, 2023
d181767
add calculate fee mint nft
KhanhChung2k5 Jun 18, 2023
6faf7bf
fix bug
KhanhChung2k5 Jun 18, 2023
b8f65c3
fix bug
KhanhChung2k5 Jun 18, 2023
f222cd2
fix bug
KhanhChung2k5 Jun 18, 2023
8d29302
Send NFT off-chain
lenhatquang97 Jun 18, 2023
d1caded
Remove some redundant parts
lenhatquang97 Jun 26, 2023
c2de5c4
Remove redundant parts in Phase 2
lenhatquang97 Jun 26, 2023
9872d18
Add guide for activating Docker in non-root
lenhatquang97 Jun 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IpfsPublicKey="k51qzi5uqu5dil4952pc6hw4063hgsxyx8lae8txbpxkf2iajvj43u71gwudyu"
27 changes: 15 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ migrate_down:
sqlc:
sqlc generate

run-test-net:
# 1 2 3 4 5 6
# go run . chain_mode network host user pass send_address
go run . off_chain testnet3 localhost:18332 DeW+bgKg011pJHZnaBvgv/lMRks= wD9aohGo2f5LwVg7fdj1ntHQcfY= mntb2RxQhyXqXRZV5GE1bDkP6615EPXLHF

run-off-chain:
# 1 2 3 4 5
# go run . chain_mode network host user pass
go run . off_chain testnet3 localhost:18332 DeW+bgKg011pJHZnaBvgv/lMRks= wD9aohGo2f5LwVg7fdj1ntHQcfY=
run-sim-net:
# 1 2 3 4 5 6
# go run . chain_mode network host user pass send_address
go run . off_chain simnet localhost:18554 youruser SomeDecentp4ssw0rd SfF7WYPTkHnjx1jKweNYAoFGnhZH1Q2291
# 1 2 3 4 5
# go run . chain_mode network host user pass
go run . off_chain simnet localhost:18554 youruser SomeDecentp4ssw0rd

run-on-chain:
# 1 2 3 4 5 6
# go run . chain_mode network host user pass send_address
go run . on_chain testnet3 localhost:18332 DeW+bgKg011pJHZnaBvgv/lMRks= wD9aohGo2f5LwVg7fdj1ntHQcfY= n1Nd8J38uyDRLwh5ShAAPvbNrqBD1wee8v
# 1 2 3 4 5
# go run . chain_mode network host user pass
go run . on_chain testnet3 localhost:18332 DeW+bgKg011pJHZnaBvgv/lMRks= wD9aohGo2f5LwVg7fdj1ntHQcfY=

.PHONY: postgres createdb dropdb migrate_up migrate_down sqlc run-test-net run-sim-net run-on-chain

.PHONY: postgres createdb dropdb migrate_up migrate_down sqlc run-test-net run-sim-net run-on-chain
# Docker not run in case: sudo chmod 777 /var/run/docker.sock
# Permanent: https://stackoverflow.com/questions/48957195/how-to-fix-docker-got-permission-denied-issue
104 changes: 13 additions & 91 deletions business/commit_tx.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package business

import (
"bitcoin_nft_v2/config"
"bitcoin_nft_v2/utils"
"fmt"

Expand All @@ -13,22 +12,21 @@ import (
"github.com/btcsuite/btcd/wire"
)

func ExecuteCommitTransaction(sv *Server, data []byte, amount int64, fee int64) (*chainhash.Hash, *btcutil.WIF, error) {
commitTx, wif, err := CreateCommitTx(amount, sv.client, data, sv.Config, fee)
func ExecuteCommitTransaction(sv *Server, data []byte, isRef bool, txIdRef string, amount int64, fee int64) (*chainhash.Hash, *btcutil.WIF, error) {
commitTx, wif, err := CreateCommitTx(amount, sv.client, data, isRef, txIdRef, fee, sv.mode)
if err != nil {
return nil, nil, err
}

commitTxHash, err := sv.client.SendRawTransaction(commitTx, false)
if err != nil {
return nil, nil, err
}
return commitTxHash, wif, nil
}

func CreateCommitTx(amount int64, client *rpcclient.Client, embeddedData []byte, networkConfig *config.NetworkConfig, fee int64) (*wire.MsgTx, *btcutil.WIF, error) {
func CreateCommitTx(amount int64, client *rpcclient.Client, embeddedData []byte, isRef bool, txIdRef string, fee int64, mode string) (*wire.MsgTx, *btcutil.WIF, error) {
//Step 1: Get private key
defaultAddress, err := utils.GetDefaultAddress(client, networkConfig.SenderAddress, networkConfig.ParamsObject)
defaultAddress, err := client.GetAccountAddress("default")
if err != nil {
return nil, nil, err
}
Expand All @@ -44,45 +42,33 @@ func CreateCommitTx(amount int64, client *rpcclient.Client, embeddedData []byte,
return nil, nil, err
}

sendUtxos := utils.GetManyUtxo(utxos, defaultAddress.EncodeAddress(), float64(amount))
sendUtxos := utils.GetManyUtxo(client, utxos, float64(amount), txIdRef)
if len(sendUtxos) == 0 {
return nil, nil, fmt.Errorf("no utxos")
}

balance, err := utils.GetActualBalance(client, networkConfig.SenderAddress)
if err != nil {
return nil, nil, err
balance := 0
for _, sat := range sendUtxos {
balance += int(sat.Amount * 100_000_000)
}

pkScript, _ := txscript.PayToAddrScript(defaultAddress)

if err != nil {
return nil, nil, err
}

// Step 3: extracting destination address as []byte from function argument (destination string)
hashLockScript, err := utils.CreateInscriptionScript(wif.PrivKey.PubKey(), embeddedData)
hashLockScript, err := utils.CreateInscriptionScriptV2(wif.PrivKey.PubKey(), embeddedData, isRef, mode)
if err != nil {
return nil, nil, fmt.Errorf("error building script: %v", err)
}
outputKey, _, _ := utils.CreateOutputKeyBasedOnScript(wif.PrivKey.PubKey(), hashLockScript)

address, err := btcutil.NewAddressTaproot(schnorr.SerializePubKey(outputKey), networkConfig.ParamsObject)
if err != nil {
return nil, nil, err
}
fmt.Println(address.EncodeAddress())

outputScriptBuilder := txscript.NewScriptBuilder()
outputScriptBuilder.AddOp(txscript.OP_1)
outputScriptBuilder.AddData(schnorr.SerializePubKey(outputKey))
outputScript, _ := outputScriptBuilder.Script()

//Step 4: Create new transaction
redeemTx, err := utils.NewTx()
if err != nil {
return nil, nil, err
}
redeemTx := wire.NewMsgTx(wire.TxVersion)

for _, utxo := range sendUtxos {
utxoHash, err := chainhash.NewHashFromStr(utxo.TxID)
Expand Down Expand Up @@ -113,81 +99,17 @@ func CreateCommitTx(amount int64, client *rpcclient.Client, embeddedData []byte,
}

// now sign the transaction
finalRawTx, err := utils.SignTx(wif, pkScript, redeemTx)
finalRawTx, _, err := client.SignRawTransaction(redeemTx)
if err != nil {
return nil, nil, err
}

return finalRawTx, wif, nil
}

func FakeCommitTxFee(sv *Server, dataSend []byte, amount int64) (int64, error) {
fee, err := EstimateFeeForCommitTx(sv, sv.Config, amount, dataSend)
if err != nil {
return 0, err
}
return fee, nil
}

func EstimateFeeForCommitTx(sv *Server, networkConfig *config.NetworkConfig, amount int64, dataSend []byte) (int64, error) {
defaultAddress, err := utils.GetDefaultAddress(sv.client, networkConfig.SenderAddress, networkConfig.ParamsObject)
if err != nil {
return 0, err
}

wif, err := sv.client.DumpPrivKey(defaultAddress)
func FakeCommitTxFee(sv *Server, dataSend []byte, amount int64, isRef bool) (int64, error) {
fee, err := EstimateFeeForCommitTx(sv, amount, dataSend, isRef)
if err != nil {
return 0, err
}

hashLockScript, err := utils.CreateInscriptionScript(wif.PrivKey.PubKey(), dataSend)
if err != nil {
return 0, fmt.Errorf("error building script: %v", err)
}

outputKey, _, _ := utils.CreateOutputKeyBasedOnScript(wif.PrivKey.PubKey(), hashLockScript)
outputScriptBuilder := txscript.NewScriptBuilder()
outputScriptBuilder.AddOp(txscript.OP_1)
outputScriptBuilder.AddData(schnorr.SerializePubKey(outputKey))
outputScript, _ := outputScriptBuilder.Script()
redeemTx, err := utils.NewTx()
if err != nil {
return 0, err
}
utxos, err := sv.client.ListUnspent()
if err != nil {
return 0, err
}

sendUtxos := utils.GetManyUtxo(utxos, defaultAddress.EncodeAddress(), float64(amount))
for _, utxo := range sendUtxos {
utxoHash, err := chainhash.NewHashFromStr(utxo.TxID)
if err != nil {
return 0, err
}

outPoint := wire.NewOutPoint(utxoHash, utxo.Vout)

// making the input, and adding it to transaction
txIn := wire.NewTxIn(outPoint, nil, nil)
redeemTx.AddTxIn(txIn)
}

redeemTxOut := wire.NewTxOut(amount, outputScript)
redeemTx.AddTxOut(redeemTxOut)

//Fake change money to estimate transaction fee
changeAddressScript, _ := txscript.PayToAddrScript(defaultAddress)
fakeChangeTxOut := wire.NewTxOut(100, changeAddressScript)
redeemTx.AddTxOut(fakeChangeTxOut)

smartFeeRate, err := sv.client.EstimateFee(1)
if err != nil {
return 0, err
}
smartFeeRate = smartFeeRate * 100_000
txSize := int64(redeemTx.SerializeSize())

fee := int64(smartFeeRate * float64(txSize) * 2)
return fee, nil
}
8 changes: 0 additions & 8 deletions business/const.go

This file was deleted.

96 changes: 96 additions & 0 deletions business/fee_calc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package business

import (
"bitcoin_nft_v2/utils"
"fmt"

"github.com/btcsuite/btcd/mempool"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
)

func EstimateFeeForCommitTx(sv *Server, amount int64, dataSend []byte, isRef bool) (int64, error) {
defaultAddress, err := sv.client.GetAccountAddress("default")
if err != nil {
return 0, err
}

wif, err := sv.client.DumpPrivKey(defaultAddress)
if err != nil {
return 0, err
}

hashLockScript, err := utils.CreateInscriptionScriptV2(wif.PrivKey.PubKey(), dataSend, isRef, ON_CHAIN)
if err != nil {
return 0, fmt.Errorf("error building script: %v", err)
}

outputKey, _, _ := utils.CreateOutputKeyBasedOnScript(wif.PrivKey.PubKey(), hashLockScript)
outputScriptBuilder := txscript.NewScriptBuilder()
outputScriptBuilder.AddOp(txscript.OP_1)
outputScriptBuilder.AddData(schnorr.SerializePubKey(outputKey))
outputScript, _ := outputScriptBuilder.Script()
redeemTx := wire.NewMsgTx(wire.TxVersion)
utxos, err := sv.client.ListUnspent()
if err != nil {
return 0, err
}

sendUtxos := utils.GetManyUtxo(sv.client, utxos, float64(amount), "")
for _, utxo := range sendUtxos {
utxoHash, err := chainhash.NewHashFromStr(utxo.TxID)
if err != nil {
return 0, err
}

outPoint := wire.NewOutPoint(utxoHash, utxo.Vout)

// making the input, and adding it to transaction
txIn := wire.NewTxIn(outPoint, nil, nil)
redeemTx.AddTxIn(txIn)
}

redeemTxOut := wire.NewTxOut(amount, outputScript)
redeemTx.AddTxOut(redeemTxOut)

//Fake change money to estimate transaction fee
changeAddressScript, _ := txscript.PayToAddrScript(defaultAddress)
fakeChangeTxOut := wire.NewTxOut(100, changeAddressScript)
redeemTx.AddTxOut(fakeChangeTxOut)

//txSize := int64(tx.SerializeSize())
feeRate, err := sv.client.EstimateFee(1)
if err != nil {
return 0, err
}
txSize := mempool.GetTxVirtualSize(btcutil.NewTx(redeemTx))
fee := txSize * int64(feeRate*100_000) * 3
return fee, nil
}

func EstimatedFeeForRevealTx(client *rpcclient.Client, embeddedData []byte, isRef bool, commitTxHash chainhash.Hash, commitOutput wire.TxOut, txOutIndex uint32, randPriv *btcec.PrivateKey, params *chaincfg.Params, toAddress string, amount int64) (int64, error) {
tx, _, _, _, err := CreateRevealTxObj(client, embeddedData, isRef, commitTxHash, commitOutput, txOutIndex, randPriv, params, toAddress, amount, "on_chain")
if err != nil {
return 0, err
}

//txSize := int64(tx.SerializeSize())
feeRate, err := client.EstimateFee(1)
if err != nil {
return 0, err
}
txSize := mempool.GetTxVirtualSize(btcutil.NewTx(tx))
fee := txSize * int64(feeRate*100_000)

if err != nil {
return 0, err
}
return fee, nil
}
8 changes: 5 additions & 3 deletions business/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
)

type NftData struct {
ID string
Url string
Memo string
ID string `json:"id"`
Url string `json:"url"`
Memo string `json:"memo"`
TxID string `json:"txId"`
Binary string `json:"binary"`
}

type RevealTxInput struct {
Expand Down
Loading