Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
fix pk for sig
Browse files Browse the repository at this point in the history
  • Loading branch information
boomyl committed Oct 22, 2021
1 parent c3610f1 commit ce79e11
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 45 deletions.
84 changes: 42 additions & 42 deletions nft/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,120 +6,120 @@ import (
)

/*
init alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount]
init alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount, publicKey]
buy data stack [buyer, marginAmount, selecter]
edit data stack [newMarginAsset, newMarginAmount, selecter]
*/

func NewContract(platformScript []byte, marginFold uint64) ([]byte, error) {
builder := vmutil.NewBuilder()
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount]
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount, publicKey]
// data statck [...... selecter]
builder.AddJumpIf(0)
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount]
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount, publicKey]
// data statck [marginAsset, buyer, newMarginAmount]
cpAltStack(builder, 0)
cpAltStack(builder, 1)
builder.AddUint64(marginFold)
builder.AddOp(vm.OP_MUL)
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount]
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount, publicKey]
// data statck [marginAsset, buyer, newMarginAmount, payAmount=marginAmount*marginFold]
builder.AddOp(vm.OP_DUP)
builder.AddUint64(100)
builder.AddOp(vm.OP_DIV)
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount]
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount, publicKey]
// data statck [marginAsset, buyer, newMarginAmount, payAmount, platformFee]
builder.AddOp(vm.OP_SWAP)
builder.AddOp(vm.OP_DUP)
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount]
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount, publicKey]
// data statck [marginAsset, buyer, newMarginAmount, platformFee, payAmount, payAmount]
cpAltStack(builder, 4)
cpAltStack(builder, 5)
builder.AddOp(vm.OP_MUL)
builder.AddUint64(10000)
builder.AddOp(vm.OP_DIV)
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount]
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount, publicKey]
// data statck [marginAsset, buyer, newMarginAmount, platformFee, payAmount, createrTax]
builder.AddOp(vm.OP_DUP)
builder.AddUint64(2)
builder.AddOp(vm.OP_SWAP)
cpAltStack(builder, 1)
cpAltStack(builder, 2)
builder.AddUint64(1)
cpAltStack(builder, 5)
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount]
cpAltStack(builder, 6)
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount, publicKey]
// data statck [marginAsset, buyer, newMarginAmount, platformFee, payAmount, createrTax, 2, createrTax, marginAsset, 1, PROGRAM]
builder.AddOp(vm.OP_CHECKOUTPUT)
builder.AddOp(vm.OP_VERIFY)
builder.AddOp(vm.OP_SUB)
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount]
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount, publicKey]
// data statck [marginAsset, buyer, newMarginAmount, platformFee, payAmount-createrTax]
builder.AddOp(vm.OP_SWAP)
builder.AddOp(vm.OP_DUP)
builder.AddUint64(3)
builder.AddOp(vm.OP_SWAP)
cpAltStack(builder, 1)
cpAltStack(builder, 2)
builder.AddUint64(1)
builder.AddData(platformScript)
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount]
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount, publicKey]
// data statck [marginAsset, buyer, newMarginAmount, payAmount-createrTax, platformFee, 3, platformFee, marginAsset, 1, platformScript]
builder.AddOp(vm.OP_CHECKOUTPUT)
builder.AddOp(vm.OP_VERIFY)
builder.AddOp(vm.OP_SUB)
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount]
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount, publicKey]
// data statck [marginAsset, buyer, newMarginAmount, ownerGot]
cpAltStack(builder, 0)
cpAltStack(builder, 1)
builder.AddOp(vm.OP_ADD)
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount]
// data statck [marginAsset, buyer, newMarginAmount, ownerGot+marginAmount]
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount, publicKey]
// data statck [marginAsset, buyer, newMarginAmount, ownerGot+marginAmount, publicKey]
builder.AddUint64(4)
builder.AddOp(vm.OP_SWAP)
cpAltStack(builder, 1)
builder.AddUint64(1)
cpAltStack(builder, 2)
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount]
builder.AddUint64(1)
cpAltStack(builder, 3)
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount, publicKey]
// data statck [marginAsset, buyer, newMarginAmount, 4, ownerGot+marginAmount, marginAsset, 1, owner]
builder.AddOp(vm.OP_CHECKOUTPUT)
builder.AddOp(vm.OP_VERIFY)
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount]
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount, publicKey]
// data statck [marginAsset, buyer, newMarginAmount]
swapAltStack(builder, 1, 2)
swapAltStack(builder, 0, 0)
// alt stack [creater, taxRate, nftAsset, buyer, marginAsset, newMarginAmount]
// data statck [marginAsset]
swapAltStack(builder, 1, 3)
swapAltStack(builder, 0, 1)
// alt stack [creater, taxRate, nftAsset, buyer, marginAsset, newMarginAmount, publicKey]
// data statck [marginAsset]
swapAltStack(builder, 0, 2)
builder.AddJump(1)

builder.SetJumpTarget(0)
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount]
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount, publicKey]
// data statck [ownerSig, newMarginAmount]
swapAltStack(builder, 0, 0)
// alt stack [creater, taxRate, nftAsset, owner, newMarginAsset, newMarginAmount]
swapAltStack(builder, 0, 1)
// alt stack [creater, taxRate, nftAsset, owner, newMarginAsset, newMarginAmount, publicKey]
// data statck [ownerSig]
cpAltStack(builder, 2)
// alt stack [creater, taxRate, nftAsset, owner, newMarginAsset, newMarginAmount]
cpAltStack(builder, 0)
// alt stack [creater, taxRate, nftAsset, owner, newMarginAsset, newMarginAmount, publicKey]
// data statck [ownerSig, owner]
builder.AddOp(vm.OP_TXSIGHASH)
builder.AddOp(vm.OP_SWAP)
// alt stack [creater, taxRate, nftAsset, owner, newMarginAsset, newMarginAmount]
// data statck [ownerSig, txSigHash, owner]
// alt stack [creater, taxRate, nftAsset, owner, newMarginAsset, newMarginAmount, publicKey]
// data statck [ownerSig, txSigHash, publicKey]
builder.AddOp(vm.OP_CHECKSIG)
builder.AddOp(vm.OP_VERIFY)
// alt stack [creater, taxRate, nftAsset, owner, newMarginAsset, newMarginAmount]
// alt stack [creater, taxRate, nftAsset, owner, newMarginAsset, newMarginAmount, publicKey]
// data statck []
builder.SetJumpTarget(1)
builder.AddUint64(0)
builder.AddUint64(1)
cpAltStack(builder, 3)
cpAltStack(builder, 4)
builder.AddUint64(1)
builder.AddOp(vm.OP_PROGRAM)
// alt stack [creater, taxRate, nftAsset, owner, newMarginAsset, newMarginAmount]
// alt stack [creater, taxRate, nftAsset, owner, newMarginAsset, newMarginAmount, publicKey]
// data statck [0, 1, nftAsset, 1, PROGRAM]
builder.AddOp(vm.OP_CHECKOUTPUT)
builder.AddOp(vm.OP_VERIFY)
builder.AddUint64(1)
cpAltStack(builder, 0)
cpAltStack(builder, 1)
cpAltStack(builder, 2)
builder.AddUint64(1)
builder.AddOp(vm.OP_PROGRAM)
// alt stack [creater, taxRate, nftAsset, owner, newMarginAsset, newMarginAmount]
// alt stack [creater, taxRate, nftAsset, owner, newMarginAsset, newMarginAmount, publicKey]
// data statck [1, newMarginAmount, newMarginAsset, 1, PROGRAM]
builder.AddOp(vm.OP_CHECKOUTPUT)
return builder.Build()
Expand All @@ -132,7 +132,7 @@ func NewOffer(nftContract []byte) ([]byte, error) {
builder.AddJump(1)

// need check sig for cancel func
cpAltStack(builder, 2)
cpAltStack(builder, 0)
builder.AddOp(vm.OP_TXSIGHASH)
builder.AddOp(vm.OP_SWAP)
builder.AddOp(vm.OP_CHECKSIG)
Expand All @@ -141,14 +141,14 @@ func NewOffer(nftContract []byte) ([]byte, error) {
builder.SetJumpTarget(0)
builder.AddUint64(0)
builder.AddUint64(1)
cpAltStack(builder, 3)
cpAltStack(builder, 4)
builder.AddUint64(1)
builder.AddData(nftContract)
builder.AddOp(vm.OP_CHECKOUTPUT)
builder.AddOp(vm.OP_VERIFY)
builder.AddUint64(1)
cpAltStack(builder, 0)
cpAltStack(builder, 1)
cpAltStack(builder, 2)
builder.AddUint64(1)
builder.AddData(nftContract)
builder.AddOp(vm.OP_CHECKOUTPUT)
Expand Down
15 changes: 12 additions & 3 deletions nft/nft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ var (
ETH = testutil.MustDecodeAsset("78de44ffa1bce37b757c9eae8925b5f199dc4621b412ef0f3f46168865284a93")

utxoSourceID = testutil.MustDecodeHash("762ec536ea64f71feac5fd4000a4807fc8e9d08d757889bd0206a02b79f9db8e")
ownerScirpt = testutil.MustDecodeHexString("0100000000000000000000000000000000000000000000000000000000000000") //[]byte("ownerScirpt")
buyerScirpt = testutil.MustDecodeHexString("0100000000000000000000000000000000000000000000000000000000000000") //[]byte("buyerScirpt")
ownerScirpt = []byte("ownerScirpt")
buyerScirpt = []byte("buyerScirpt")
publicKey = testutil.MustDecodeHexString("0100000000000000000000000000000000000000000000000000000000000000")
)

// 从2个BTC的押金换成3个BTC的
Expand All @@ -40,6 +41,7 @@ func TestEditMargin(t *testing.T) {
ownerScirpt,
BTC.Bytes(),
vm.Uint64Bytes(200000000),
publicKey,
}

newStateData := [][]byte{
Expand All @@ -49,10 +51,11 @@ func TestEditMargin(t *testing.T) {
ownerScirpt,
BTC.Bytes(),
vm.Uint64Bytes(300000000),
publicKey,
}

arguments := [][]byte{
testutil.MustDecodeHexString("6c25b3220df660b06bf17ca881e6a31811fc4f33f78e5c0597a1e29b5d0030d9494bff417a237b907eac5cdce1dc0dfe1f258103dee32fcc84a2dd5e8c614209"),
testutil.MustDecodeHexString("b90890b349b0cc0d4e86d21efccc517fdf1bcd0038bbbdf518433905e10ec81130906c816298381a6cedacd037b3084b503c146f7cf710fc759c367da1552a09"),
vm.Uint64Bytes(300000000),
vm.Uint64Bytes(1),
}
Expand Down Expand Up @@ -92,6 +95,7 @@ func TestRegularBuy(t *testing.T) {
ownerScirpt,
ETH.Bytes(),
vm.Uint64Bytes(10000000000),
publicKey,
}

newStateData := [][]byte{
Expand All @@ -101,6 +105,7 @@ func TestRegularBuy(t *testing.T) {
buyerScirpt,
ETH.Bytes(),
vm.Uint64Bytes(15000000000),
publicKey,
}

arguments := [][]byte{
Expand Down Expand Up @@ -148,6 +153,7 @@ func TestBuySwapMargin(t *testing.T) {
ownerScirpt,
ETH.Bytes(),
vm.Uint64Bytes(10000000000),
publicKey,
}

newStateData := [][]byte{
Expand All @@ -157,6 +163,7 @@ func TestBuySwapMargin(t *testing.T) {
buyerScirpt,
BTC.Bytes(),
vm.Uint64Bytes(200000000),
publicKey,
}

arguments := [][]byte{
Expand Down Expand Up @@ -210,6 +217,7 @@ func TestOfferBuy(t *testing.T) {
ownerScirpt,
ETH.Bytes(),
vm.Uint64Bytes(10000000000),
publicKey,
}

newStateData := [][]byte{
Expand All @@ -219,6 +227,7 @@ func TestOfferBuy(t *testing.T) {
buyerScirpt,
ETH.Bytes(),
vm.Uint64Bytes(15000000000),
publicKey,
}

arguments := [][]byte{
Expand Down

0 comments on commit ce79e11

Please sign in to comment.