WIP on trade-bot

This commit is contained in:
catbref
2020-06-10 09:15:09 +01:00
parent 65ccb80aa4
commit faa6e82bef
9 changed files with 42 additions and 16 deletions

View File

@@ -24,6 +24,9 @@ public class CrossChainBuildRequest {
@XmlJavaTypeAdapter(value = org.qortal.api.AmountTypeAdapter.class)
public long fundingQortAmount;
@Schema(description = "HASH160 of creator's Bitcoin public key", example = "2daMveGc5pdjRyFacbxBzMksCbyC")
public byte[] bitcoinPublicKeyHash;
@Schema(description = "HASH160 of secret", example = "43vnftqkjxrhb5kJdkU1ZFQLEnWV")
public byte[] secretHash;

View File

@@ -180,7 +180,8 @@ public class CrossChainResource {
try (final Repository repository = RepositoryManager.getRepository()) {
PublicKeyAccount creatorAccount = new PublicKeyAccount(repository, creatorPublicKey);
byte[] creationBytes = BTCACCT.buildQortalAT(creatorAccount.getAddress(), tradeRequest.secretHash, tradeRequest.tradeTimeout, tradeRequest.initialQortAmount, tradeRequest.finalQortAmount, tradeRequest.bitcoinAmount);
byte[] creationBytes = BTCACCT.buildQortalAT(creatorAccount.getAddress(), tradeRequest.bitcoinPublicKeyHash, tradeRequest.secretHash,
tradeRequest.tradeTimeout, tradeRequest.initialQortAmount, tradeRequest.finalQortAmount, tradeRequest.bitcoinAmount);
long txTimestamp = NTP.getTime();
byte[] lastReference = creatorAccount.getLastReference();

View File

@@ -11,15 +11,11 @@ import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.LegacyAddress;
import org.bitcoinj.core.NetworkParameters;
import org.qortal.account.PrivateKeyAccount;
import org.qortal.api.ApiError;
import org.qortal.api.ApiExceptionFactory;
import org.qortal.crosschain.BTC;
import org.qortal.crosschain.BTCACCT;
import org.qortal.crypto.Crypto;
import org.qortal.data.at.ATData;
import org.qortal.data.crosschain.CrossChainTradeData;
import org.qortal.data.crosschain.TradeBotData;
import org.qortal.data.crosschain.CrossChainTradeData.Mode;
import org.qortal.repository.DataException;
import org.qortal.repository.Repository;
import org.qortal.repository.RepositoryManager;

View File

@@ -237,6 +237,7 @@ public class BTCACCT {
* 32-byte secret to the AT, before the AT automatically refunds the AT's creator.
*
* @param qortalCreator Qortal address for AT creator, also used for refunds
* @param bitcoinPublicKeyHash 20-byte HASH160 of creator's bitcoin public key
* @param secretHash 20-byte HASH160 of 32-byte secret
* @param tradeTimeout how many minutes, from start of 'trade mode' until AT auto-refunds AT creator
* @param initialPayout how much QORT to pay trade partner upon switch to 'trade mode'
@@ -244,7 +245,7 @@ public class BTCACCT {
* @param bitcoinAmount how much BTC the AT creator is expecting to trade
* @return
*/
public static byte[] buildQortalAT(String qortalCreator, byte[] secretHash, int tradeTimeout, long initialPayout, long redeemPayout, long bitcoinAmount) {
public static byte[] buildQortalAT(String qortalCreator, byte[] bitcoinPublicKeyHash, byte[] secretHash, int tradeTimeout, long initialPayout, long redeemPayout, long bitcoinAmount) {
// Labels for data segment addresses
int addrCounter = 0;
@@ -255,6 +256,9 @@ public class BTCACCT {
final int addrQortalCreator3 = addrCounter++;
final int addrQortalCreator4 = addrCounter++;
final int addrBitcoinPublickeyHash = addrCounter;
addrCounter += 4;
final int addrSecretHash = addrCounter;
addrCounter += 4;
@@ -303,6 +307,10 @@ public class BTCACCT {
byte[] qortalCreatorBytes = Base58.decode(qortalCreator);
dataByteBuffer.put(Bytes.ensureCapacity(qortalCreatorBytes, 32, 0));
// Bitcoin public key hash
assert dataByteBuffer.position() == addrBitcoinPublickeyHash * MachineState.VALUE_SIZE : "addrBitcoinPublicKeyHash incorrect";
dataByteBuffer.put(Bytes.ensureCapacity(bitcoinPublicKeyHash, 32, 0));
// Hash of secret
assert dataByteBuffer.position() == addrSecretHash * MachineState.VALUE_SIZE : "addrSecretHash incorrect";
dataByteBuffer.put(Bytes.ensureCapacity(secretHash, 32, 0));
@@ -559,6 +567,11 @@ public class BTCACCT {
// Skip AT creator address
dataByteBuffer.position(dataByteBuffer.position() + 32);
// Bitcoin/foreign public key hash
tradeData.foreignPublicKeyHash = new byte[20];
dataByteBuffer.get(tradeData.foreignPublicKeyHash);
dataByteBuffer.position(dataByteBuffer.position() + 32 - 20); // skip to 32 bytes
// Hash of secret
tradeData.secretHash = new byte[20];
dataByteBuffer.get(tradeData.secretHash);

View File

@@ -59,6 +59,8 @@ public class CrossChainTradeData {
@Schema(description = "Suggested Bitcoin P2SH nLockTime based on trade timeout")
public Integer lockTime;
public byte[] foreignPublicKeyHash;
// Constructors
// Necessary for JAXB

View File

@@ -620,12 +620,12 @@ public class HSQLDBDatabaseUpdates {
case 20:
// Trade bot
stmt.execute("CREATE TABLE TradeBotStates (trade_private_key QortalPrivateKey NOT NULL, trade_state TINYINT NOT NULL, "
stmt.execute("CREATE TABLE TradeBotStates (trade_private_key QortalKeySeed NOT NULL, trade_state TINYINT NOT NULL, "
+ "trade_native_public_key QortalPublicKey NOT NULL, trade_native_public_key_hash VARBINARY(32) NOT NULL, "
+ "secret VARBINARY(32) NOT NULL, secret_hash VARBINARY(32) NOT NULL, "
+ "trade_foreign_public_key QortalPublicKey NOT NULL, trade_foreign_public_key_hash VARBINARY(32) NOT NULL, "
+ "at_address QortalAddress, "
+ "last_transaction_signature Signature, PRIMARY KEY (trade_private_key)");
+ "last_transaction_signature Signature, PRIMARY KEY (trade_private_key))");
break;
default: