Skip to content

Commit

Permalink
Fix deploy fail
Browse files Browse the repository at this point in the history
  • Loading branch information
catdev2024 committed Oct 18, 2021
1 parent d855fc7 commit 157654c
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 130 deletions.
6 changes: 3 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@
"name": "Debug AnyoneCanSpend",
"program": "${workspaceFolder}/contracts/acs.scrypt",
"constructorArgs": [
"Ripemd160(b'91518afe138835418631d1fd4eaebcfbfc98720d')"
"Ripemd160(b'6d1e8b6fa83b5ff413659c61ad6a8f1be4ab95a2')"
],
"pubFunc": "unlock",
"pubFuncArgs": [
"SigHashPreimage(b'01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005884e5db9de218238671572340b207ee85b628074e7e467096c267266baf77a400000000fd600451014001800176018801a901ac1491518afe138835418631d1fd4eaebcfbfc98720d5579587985577985615979517920ea401e7cedf9c428fbf9b92b75c90dfdd354394e58195d58e82bf79a8de31d622102773aca113a3217b67a95d5b78b69bb6386ed443ea5decf0ba92c00d1792919212108dc7dc8b865cafc4cb5ff38624ba4c5385a3b8d7381f5bb49ba4a55963f10a20021606bfc5df21a9603c63d49e178b0620c9953d37c7ddeddfc12580925da43fcf0002100f0fc43da25095812fcddde7d7cd353990c62b078e1493dc603961af25dfc6b60615679557955795579557955795b795679aa616100790079517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e01007e81517a756157795679567956795679537956795479577995939521414136d08c5ed2bf3ba048afe6dcaebafeffffffffffffffffffffffffffffff0061517951795179517997527a75517a5179009f635179517993527a75517a685179517a75517a7561527a75517a517951795296a0630079527994527a75517a68537982775279827754527993517993013051797e527e53797e57797e527e52797e5579517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7e56797e0079517a75517a75517a75517a75517a75517a75517a75517a75517a75517a75517a75517a756100795779ac517a75517a75517a75517a75517a75517a75517a75517a75517a7561517a75517a75517a75517a75517a75517a75517a756169615179567955797e01147e51797e56797e54797e517a7561615a79616100790079827751795179012c947f7551790134947f77517a75517a7561007901007e81517a7561517a75610079022202946152795179007958806152790079827700517902fd009f63615179515179517951938000795179827751947f75007f77517a75517a75517a7561517a75675179030000019f6301fd615279525179517951938000795179827751947f75007f77517a75517a75517a75617e517a756751790500000000019f6301fe615279545179517951938000795179827751947f75007f77517a75517a75517a75617e517a75675179090000000000000000019f6301ff615279585179517951938000795179827751947f75007f77517a75517a75517a75617e517a7568686868007953797e517a75517a75517a75617e517a75517a75610079aa0079615f79007982775179517958947f7551790128947f77517a75517a756187777777777777777777777777777777a086010000000000ffffffffdcb79768e8b330ecd8ecc76feb6ecbacb1328475262e17f6853312f0d6bd4d2f00000000c1000000')"
"SigHashPreimage(b'01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005884e5db9de218238671572340b207ee85b628074e7e467096c267266baf77a400000000fd600451014001800176018801a901ac146d1e8b6fa83b5ff413659c61ad6a8f1be4ab95a25579587985577985615979517920ea401e7cedf9c428fbf9b92b75c90dfdd354394e58195d58e82bf79a8de31d622102773aca113a3217b67a95d5b78b69bb6386ed443ea5decf0ba92c00d1792919212108dc7dc8b865cafc4cb5ff38624ba4c5385a3b8d7381f5bb49ba4a55963f10a20021606bfc5df21a9603c63d49e178b0620c9953d37c7ddeddfc12580925da43fcf0002100f0fc43da25095812fcddde7d7cd353990c62b078e1493dc603961af25dfc6b60615679557955795579557955795b795679aa616100790079517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e01007e81517a756157795679567956795679537956795479577995939521414136d08c5ed2bf3ba048afe6dcaebafeffffffffffffffffffffffffffffff0061517951795179517997527a75517a5179009f635179517993527a75517a685179517a75517a7561527a75517a517951795296a0630079527994527a75517a68537982775279827754527993517993013051797e527e53797e57797e527e52797e5579517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f517f7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7c7e7e56797e0079517a75517a75517a75517a75517a75517a75517a75517a75517a75517a75517a75517a756100795779ac517a75517a75517a75517a75517a75517a75517a75517a75517a7561517a75517a75517a75517a75517a75517a75517a756169615179567955797e01147e51797e56797e54797e517a7561615a79616100790079827751795179012c947f7551790134947f77517a75517a7561007901007e81517a7561517a7561007902e803946152795179007958806152790079827700517902fd009f63615179515179517951938000795179827751947f75007f77517a75517a75517a7561517a75675179030000019f6301fd615279525179517951938000795179827751947f75007f77517a75517a75517a75617e517a756751790500000000019f6301fe615279545179517951938000795179827751947f75007f77517a75517a75517a75617e517a75675179090000000000000000019f6301ff615279585179517951938000795179827751947f75007f77517a75517a75517a75617e517a7568686868007953797e517a75517a75517a75617e517a75517a75610079aa0079615f79007982775179517958947f7551790128947f77517a75517a756187777777777777777777777777777777a086010000000000ffffffff70450ce8cabd98dfff71836edee419ac19ae34302a360e0cd5f9b8a64f922a1500000000c1000000')"
],
"txContext": {
"hex": "01000000015884e5db9de218238671572340b207ee85b628074e7e467096c267266baf77a40000000000ffffffff017e840100000000001976a91491518afe138835418631d1fd4eaebcfbfc98720d88ac00000000",
"hex": "01000000015884e5db9de218238671572340b207ee85b628074e7e467096c267266baf77a40000000000ffffffff01b8820100000000001976a9146d1e8b6fa83b5ff413659c61ad6a8f1be4ab95a288ac00000000",
"inputSatoshis": 100000,
"inputIndex": 0
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/acs.scrypt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ contract AnyoneCanSpend {

bytes lockingScript = Util.buildPublicKeyHashScript(this.pubKeyHash);
int inputAmount = Util.value(txPreimage);
int outputAmount = inputAmount - 546; // minFee
int outputAmount = inputAmount - 1000; // minFee
bytes output = Util.buildOutput(lockingScript, outputAmount);

Sha256 hashOutputs = hash256(output);
Expand Down
20 changes: 10 additions & 10 deletions contracts/auction.scrypt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import "util.scrypt";

// Auction: highest bid before deadline wins
contract Auction {
@state
Ripemd160 bidder;

PubKey auctioner;
int auctionDeadline;

Expand All @@ -12,14 +15,12 @@ contract Auction {
int highestBid = Util.value(txPreimage);
require(bid > highestBid);

// read previous highest bidder
bytes lockingScript = Util.scriptCode(txPreimage);
int scriptLen = len(lockingScript);
Ripemd160 highestBidder = Ripemd160(lockingScript[scriptLen - Util.PubKeyHashLen :]);
Ripemd160 highestBidder = this.bidder;
this.bidder = bidder;

// auction continues with a higher bidder
bytes auctionScript = lockingScript[: scriptLen - Util.PubKeyHashLen] + bidder;
bytes auctionOutput = Util.buildOutput(auctionScript, bid);
bytes stateScript = this.getStateScript();
bytes auctionOutput = Util.buildOutput(stateScript, bid);

// refund previous highest bidder
bytes refundScript = Util.buildPublicKeyHashScript(highestBidder);
Expand All @@ -28,10 +29,9 @@ contract Auction {
bytes changeScript = Util.buildPublicKeyHashScript(bidder);
bytes changeOutput = Util.buildOutput(changeScript, changeSats);

bytes hashThisOutputs = hash256(auctionOutput + refundOutput + changeOutput);
bytes hashPreimageOutput = Util.hashOutputs(txPreimage);

require(hashThisOutputs == hashPreimageOutput);
bytes output = auctionOutput + refundOutput + changeOutput;

require(hash256(output) == Util.hashOutputs(txPreimage));
}

// withdraw after bidding is over
Expand Down
19 changes: 9 additions & 10 deletions deployments/acs.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,43 @@ const { privateKey } = require('../privateKey');

try {
// initialize contract
const AnyoneCanSpend = buildContractClass(loadDesc('acs_desc.json'));
const AnyoneCanSpend = buildContractClass(loadDesc('acs_debug_desc.json'));
const acs = new AnyoneCanSpend(new Ripemd160(toHex(publicKeyHashX)));

// deploy contract on testnet
const lockingTx = await createLockingTx(privateKey.toAddress(), amount);
lockingTx.outputs[0].setScript(acs.lockingScript);
const lockingTx = await createLockingTx(privateKey.toAddress(), amount, acs.lockingScript);
lockingTx.sign(privateKey);
let lockingTxid = await sendTx(lockingTx);
console.log('funding txid: ', lockingTxid);

// call contract method on testnet
let prevLockingScript = acs.lockingScript.toASM();

const newLockingScript = bsv.Script.buildPublicKeyHashOut(addressX).toASM();
const newLockingScript = bsv.Script.buildPublicKeyHashOut(addressX);

const newAmount = amount - 546; //minFee;
const newAmount = amount - 1000; //minFee;

const unlockingTx = await createUnlockingTx(
lockingTxid,
amount,
prevLockingScript,
acs.lockingScript,
newAmount,
newLockingScript
);

const preimage = getPreimage(
unlockingTx,
prevLockingScript,
acs.lockingScript,
amount,
inputIndex,
sighashType
);


const unlockingScript = acs
.unlock(new SigHashPreimage(toHex(preimage)))
.toScript();
.toScript()
unlockingTx.inputs[0].setScript(unlockingScript);

const unlockingTxid = await sendTx(unlockingTx);
console.log('unlocking txid: ', unlockingTxid);

Expand Down
10 changes: 5 additions & 5 deletions deployments/advancedTokenSale.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function sleep(ms) {
}

// get locking script
const AdvancedTokenSale = buildContractClass(loadDesc('advancedTokenSale_desc.json'))
const AdvancedTokenSale = buildContractClass(loadDesc('advancedTokenSale_debug_desc.json'))
const advTokenSale = new AdvancedTokenSale(satsPerToken)

// append state as passive data
Expand All @@ -67,7 +67,7 @@ function sleep(ms) {


//lock funds to the script
const lockingTx = await createLockingTx(privateKey.toAddress(), amount, FEE)
const lockingTx = await createLockingTx(privateKey.toAddress(), amount, advTokenSale.lockingScript)

lockingTx.outputs[0].setScript(advTokenSale.lockingScript)
lockingTx.sign(privateKey)
Expand Down Expand Up @@ -96,15 +96,15 @@ function sleep(ms) {
// The contract expects/enforces this
const newAmount = amount + spendAmount

let unlockingTx = await createUnlockingTx(lockingTxid, amount, prevLockingScript.toASM(), newAmount, advTokenSale.lockingScript.toASM())
let unlockingTx = await createUnlockingTx(lockingTxid, amount, prevLockingScript, newAmount, advTokenSale.lockingScript)


unlockingTx = await anyOnePayforTx(unlockingTx, privateKey.toAddress(), FEE)


const changeAmount = unlockingTx.inputAmount - FEE - newAmount
const changeAmount = unlockingTx.outputs[unlockingTx.outputs.length -1].satoshis

const preimage = getPreimage(unlockingTx, prevLockingScript.toASM(), amount, 0, sighashType)
const preimage = getPreimage(unlockingTx, prevLockingScript, amount, 0, sighashType)

const unlockingScript = advTokenSale.buy(
new SigHashPreimage(toHex(preimage)), // sighashPreimage
Expand Down
118 changes: 46 additions & 72 deletions deployments/auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ const bidderPKH = new bsv.crypto.Hash.sha256ripemd160(bidderPubKey.toBuffer());/


// initial contract funding - arbitrary amount
let previousAmount = 10000;
let previousAmount = 20000;

const FEE = 2000;
const FEE = 3000;
const BID_INCREASE = 2000;
const SLEEP_TIME = 3000;
const SLEEP_TIME = 5000;

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
Expand All @@ -58,28 +58,22 @@ async function runBid(prevTxid, auction, amountInContract, refundPubKey) {
try {
console.log('prevTxid', prevTxid)

const prevLockingScript = auction.lockingScript

const newState = toHex(bidderPKH);

const newLockingScriptASM = [auction.codePart.toASM(), newState].join(' ')



const unlockingTx = new bsv.Transaction()

//contract input
unlockingTx.addInput(new bsv.Transaction.Input({
prevTxId: prevTxid,
outputIndex: 0,
script: new bsv.Script(), // placeholder
}), bsv.Script.fromASM(prevLockingScript.toASM()), amountInContract)
}), auction.lockingScript, amountInContract)


// input(s) from bidder
let {
data: utxos
} = await axios.get(`https://api.whatsonchain.com/v1/bsv/test/address/` + bidderPrivKey.toAddress() + `/unspent`)

utxos.map(utxo => {
unlockingTx.addInput(new bsv.Transaction.Input({
prevTxId: utxo.tx_hash,
Expand All @@ -88,64 +82,50 @@ async function runBid(prevTxid, auction, amountInContract, refundPubKey) {
}), bsv.Script.buildPublicKeyHashOut(bidderPrivKey.toAddress()).toHex(), utxo.value)
console.log('input 1: ' + utxo.value);
})


let newLockingScript = auction.getStateScript({
bidder: new Ripemd160(toHex(bidderPKH))
})

unlockingTx.addOutput(new bsv.Transaction.Output({
script: bsv.Script.fromASM(newLockingScriptASM),
script: newLockingScript,
satoshis: amountInContract + BID_INCREASE,
}))

unlockingTx.addOutput(new bsv.Transaction.Output({
script: bsv.Script.buildPublicKeyHashOut(refundPubKey.toAddress()),
satoshis: amountInContract,
}))


const changeAmount = unlockingTx.inputAmount - (amountInContract + BID_INCREASE)/*now*/ - amountInContract/*prev*/ - FEE;
unlockingTx.addOutput(new bsv.Transaction.Output({
script: bsv.Script.buildPublicKeyHashOut(bidderPubKey.toAddress()),
satoshis: changeAmount,
}))

unlockingTx.fee(FEE);
const preimage = getPreimage(unlockingTx, prevLockingScript.toASM(), amountInContract, 0, sighashType);


const preimage = getPreimage(unlockingTx, auction.lockingScript, amountInContract, 0, sighashType);
//console.log('preimage: '+preimage);

const unlockingScript = auction.bid(
new Ripemd160(toHex(bidderPKH)), // bidderPKH
(amountInContract + BID_INCREASE),
changeAmount,
new SigHashPreimage(toHex(preimage)) // sighashPreimage
).toScript();


// unlock contract input
unlockingTx.inputs[0].setScript(unlockingScript);

// unlock other p2pkh inputs
for (let index = 1; index < unlockingTx.inputs.length; index++) {

let sig = new bsv.Transaction.Signature({
publicKey: bidderPrivKey.publicKey,
prevTxId: unlockingTx.inputs[index].prevTxId,
outputIndex: unlockingTx.inputs[index].outputIndex,
inputIndex: index,
signature: bsv.Transaction.Sighash.sign(unlockingTx, bidderPrivKey, sighashType,
index,
unlockingTx.inputs[index].output.script,
unlockingTx.inputs[index].output.satoshisBN),
sigtype: sighashType,
});

unlockingTx.inputs[index].setScript(bsv.Script.buildPublicKeyHashIn(
sig.publicKey,
sig.signature.toDER(),
sig.sigtype,
));

unlockP2PKHInput(bidderPrivKey, unlockingTx, index, sighashType)
}

// you can verify before sendTx
const result = auction.bid(
new Ripemd160(toHex(bidderPKH)), // bidderPKH
Expand All @@ -154,20 +134,20 @@ async function runBid(prevTxid, auction, amountInContract, refundPubKey) {
new SigHashPreimage(toHex(preimage)) // sighashPreimage
).verify({ tx: unlockingTx, inputSatoshis: amountInContract, inputIndex: 0 })

if(!result.success) {
if (!result.success) {
console.error(result)
}

await sleep(SLEEP_TIME)
const txid = await sendTx(unlockingTx);
console.log('txid: ', txid);


// change state after succeeded
auction.setDataPart(newState)
console.log('Succeeded on testnet');
auction.bidder = new Ripemd160(toHex(bidderPKH))

console.log('Succeeded on testnet');

resolve(txid);
} catch (error) {
console.log('Failed on testnet');
Expand All @@ -183,40 +163,34 @@ async function runBid(prevTxid, auction, amountInContract, refundPubKey) {
(async () => {

// get locking script
const Auction = buildContractClass(loadDesc('auction_desc.json'))
const auction = new Auction(new PubKey(auctionerPubKey), Math.round(new Date().getTime() / 1000) + 3600)
const Auction = buildContractClass(loadDesc('auction_debug_desc.json'))
const auction = new Auction(new Ripemd160(toHex(auctionerPKH)), new PubKey(auctionerPubKey), Math.round(new Date().getTime() / 1000) + 3600)

// append state as passive data
auction.setDataPart(toHex(auctionerPKH))


try {

//lock funds to the script
const lockingTx = await createLockingTx(auctionerPrivKey.toAddress(), previousAmount, FEE)
lockingTx.outputs[0].setScript(auction.lockingScript)

//lock funds to the script
const lockingTx = await createLockingTx(auctionerPrivKey.toAddress(), previousAmount, auction.lockingScript)
lockingTx.sign(auctionerPrivKey)
let lockingTxid = await sendTx(lockingTx)
console.log('funding txid: ', lockingTxid)

let prevTxid = lockingTxid;

for (let i = 0; i < 5; i++) {

const txid = await runBid(prevTxid, auction, previousAmount + (BID_INCREASE*i), i == 0 ? auctionerPubKey : bidderPubKey);
await sleep(SLEEP_TIME)
const txid = await runBid(prevTxid, auction, previousAmount + (BID_INCREASE * i), i == 0 ? auctionerPubKey : bidderPubKey);

prevTxid = txid;

}


} catch (error) {
console.log('Failed on testnet');
showError(error);
console.log(error.context);
console.log('Failed on testnet');
showError(error);
console.log(error.context);
}




})()
Loading

0 comments on commit 157654c

Please sign in to comment.