From 6b834992166c70f1a4e029760678e79dfdd707ee Mon Sep 17 00:00:00 2001 From: catbref Date: Tue, 4 Aug 2020 20:35:22 +0100 Subject: [PATCH] WIP: trade-bot: add support for showing trade partner's Qortal receiving address in trade offer summaries --- .../api/model/CrossChainOfferSummary.java | 7 +++++ .../java/org/qortal/crosschain/BTCACCT.java | 29 +++++++++++++++++-- .../data/crosschain/CrossChainTradeData.java | 3 ++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/qortal/api/model/CrossChainOfferSummary.java b/src/main/java/org/qortal/api/model/CrossChainOfferSummary.java index 97522e99..4cabfc37 100644 --- a/src/main/java/org/qortal/api/model/CrossChainOfferSummary.java +++ b/src/main/java/org/qortal/api/model/CrossChainOfferSummary.java @@ -34,6 +34,8 @@ public class CrossChainOfferSummary { private long timestamp; + private String partnerQortalReceivingAddress; + protected CrossChainOfferSummary() { /* For JAXB */ } @@ -46,6 +48,7 @@ public class CrossChainOfferSummary { this.tradeTimeout = crossChainTradeData.tradeTimeout; this.mode = crossChainTradeData.mode; this.timestamp = timestamp; + this.partnerQortalReceivingAddress = crossChainTradeData.qortalPartnerReceivingAddress; } public String getQortalAtAddress() { @@ -76,4 +79,8 @@ public class CrossChainOfferSummary { return this.timestamp; } + public String getPartnerQortalReceivingAddress() { + return this.partnerQortalReceivingAddress; + } + } diff --git a/src/main/java/org/qortal/crosschain/BTCACCT.java b/src/main/java/org/qortal/crosschain/BTCACCT.java index 13184d19..404c9fb0 100644 --- a/src/main/java/org/qortal/crosschain/BTCACCT.java +++ b/src/main/java/org/qortal/crosschain/BTCACCT.java @@ -105,10 +105,10 @@ public class BTCACCT { public static final int SECRET_LENGTH = 32; public static final int MIN_LOCKTIME = 1500000000; - public static final byte[] CODE_BYTES_HASH = HashCode.fromString("fad14381b77ae1a2bfe7e16a1a8b571839c5f405fca0490ead08499ac170f65b").asBytes(); // SHA256 of AT code bytes + public static final byte[] CODE_BYTES_HASH = HashCode.fromString("a472f32a6799ff85ed99567701b42fa228c58a09d38485ab208ad78d0f2cc813").asBytes(); // SHA256 of AT code bytes /** Value offset into AT segment where 'mode' variable (long) is stored. (Multiply by MachineState.VALUE_SIZE for byte offset). */ - private static final int MODE_VALUE_OFFSET = 63; + private static final int MODE_VALUE_OFFSET = 68; /** Byte offset into AT state data where 'mode' variable (long) is stored. */ public static final int MODE_BYTE_OFFSET = MachineState.HEADER_LENGTH + (MODE_VALUE_OFFSET * MachineState.VALUE_SIZE); @@ -199,6 +199,8 @@ public class BTCACCT { final int addrMessageDataPointer = addrCounter++; final int addrMessageDataLength = addrCounter++; + final int addrPartnerReceivingAddressPointer = addrCounter++; + final int addrEndOfConstants = addrCounter; // Variables @@ -238,6 +240,9 @@ public class BTCACCT { final int addrPartnerBitcoinPKH = addrCounter; addrCounter += 4; + final int addrPartnerReceivingAddress = addrCounter; + addrCounter += 4; + final int addrMode = addrCounter++; assert addrMode == MODE_VALUE_OFFSET : "MODE_VALUE_OFFSET does not match addrMode"; @@ -327,6 +332,10 @@ public class BTCACCT { assert dataByteBuffer.position() == addrMessageDataLength * MachineState.VALUE_SIZE : "addrMessageDataLength incorrect"; dataByteBuffer.putLong(32L); + // Pointer into data segment of where to save partner's receiving Qortal address, used by GET_B_IND + assert dataByteBuffer.position() == addrPartnerReceivingAddressPointer * MachineState.VALUE_SIZE : "addrPartnerReceivingAddressPointer incorrect"; + dataByteBuffer.putLong(addrPartnerReceivingAddress); + assert dataByteBuffer.position() == addrEndOfConstants * MachineState.VALUE_SIZE : "dataByteBuffer position not at end of constants"; // Code labels @@ -544,6 +553,8 @@ public class BTCACCT { // Extract Qortal receiving address from next 32 bytes of message from transaction into B register codeByteBuffer.put(OpCode.EXT_FUN_DAT.compile(QortalFunctionCode.PUT_PARTIAL_MESSAGE_FROM_TX_IN_A_INTO_B.value, addrRedeemMessageReceivingAddressOffset)); + // Save B register into data segment starting at addrPartnerReceivingAddress (as pointed to by addrPartnerReceivingAddressPointer) + codeByteBuffer.put(OpCode.EXT_FUN_DAT.compile(FunctionCode.GET_B_IND, addrPartnerReceivingAddressPointer)); // Pay AT's balance to receiving address codeByteBuffer.put(OpCode.EXT_FUN_DAT.compile(FunctionCode.PAY_TO_ADDRESS_IN_B, addrQortAmount)); // Set redeemed mode @@ -700,6 +711,9 @@ public class BTCACCT { // Skip message data length dataByteBuffer.position(dataByteBuffer.position() + 8); + // Skip pointer to partner's receiving address + dataByteBuffer.position(dataByteBuffer.position() + 8); + /* End of constants / begin variables */ // Skip AT creator's address @@ -753,9 +767,17 @@ public class BTCACCT { dataByteBuffer.get(partnerBitcoinPKH); dataByteBuffer.position(dataByteBuffer.position() + 32 - partnerBitcoinPKH.length); // skip to 32 bytes + // Partner's receiving address (if present) + byte[] partnerReceivingAddress = new byte[25]; + dataByteBuffer.get(partnerReceivingAddress); + dataByteBuffer.position(dataByteBuffer.position() + 32 - partnerReceivingAddress.length); // skip to 32 bytes + + // Trade AT's 'mode' long modeValue = dataByteBuffer.getLong(); Mode mode = Mode.valueOf((int) (modeValue & 0xffL)); + /* End of variables */ + if (mode != null && mode != Mode.OFFERING) { tradeData.mode = mode; tradeData.refundTimeout = refundTimeout; @@ -765,6 +787,9 @@ public class BTCACCT { tradeData.partnerBitcoinPKH = partnerBitcoinPKH; tradeData.lockTimeA = lockTimeA; tradeData.lockTimeB = lockTimeB; + + if (mode == Mode.REDEEMED) + tradeData.qortalPartnerReceivingAddress = Base58.encode(partnerReceivingAddress); } else { tradeData.mode = Mode.OFFERING; } diff --git a/src/main/java/org/qortal/data/crosschain/CrossChainTradeData.java b/src/main/java/org/qortal/data/crosschain/CrossChainTradeData.java index bd012cdc..f445f58e 100644 --- a/src/main/java/org/qortal/data/crosschain/CrossChainTradeData.java +++ b/src/main/java/org/qortal/data/crosschain/CrossChainTradeData.java @@ -73,6 +73,9 @@ public class CrossChainTradeData { @Schema(description = "Trade partner's Bitcoin public-key-hash (PKH)") public byte[] partnerBitcoinPKH; + @Schema(description = "Trade partner's Qortal receiving address") + public String qortalPartnerReceivingAddress; + // Constructors // Necessary for JAXB