Skip to content

Commit

Permalink
Merge pull request #140 from K-Singh/additional-accessor-methods
Browse files Browse the repository at this point in the history
New Accessor Methods
  • Loading branch information
aslesarenko authored Feb 22, 2022
2 parents 71491ae + 0f73945 commit d90135c
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ object DhtUtils {
val txB = ctx.newTxBuilder
val outBox = txB.outBoxBuilder
.value(dhtBox.getValue)
.contract(new ErgoTreeContract(receiver.getErgoAddress.script))
.contract(new ErgoTreeContract(receiver.getErgoAddress.script, receiver.getNetworkType))
.registers(ErgoValue.of(g_y), ErgoValue.of(g_xy))
.build

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class TxBuilderSpec extends PropSpec with Matchers
val recipient = address

val amountToSend = 1000000
val pkContract = new ErgoTreeContract(recipient.getErgoAddress.script)
val pkContract = new ErgoTreeContract(recipient.getErgoAddress.script, recipient.getNetworkType)

val senders = Arrays.asList(storage.getAddressFor(NetworkType.MAINNET))
val unsigned = BoxOperations.createForSenders(senders).withAmountToSpend(amountToSend)
Expand Down Expand Up @@ -337,7 +337,7 @@ class TxBuilderSpec extends PropSpec with Matchers
val recipient = address

val amountToSend = 1000000
val pkContract = new ErgoTreeContract(recipient.getErgoAddress.script)
val pkContract = new ErgoTreeContract(recipient.getErgoAddress.script, recipient.getNetworkType)

val senders = Arrays.asList(storage.getAddressFor(NetworkType.MAINNET))
val unsigned = BoxOperations.createForSenders(senders).withAmountToSpend(amountToSend)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,13 @@ public interface ErgoContract {
*/
ErgoContract substConstant(String name, Object value);

/**
* Returns the underlying ErgoTree used by this contract
*/
Values.ErgoTree getErgoTree();

/**
* Get the base58 encoded address that represents this contract
*/
Address getAddress();
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package org.ergoplatform.appkit.impl;

import org.ergoplatform.appkit.Constants;
import org.ergoplatform.appkit.ErgoContract;
import org.ergoplatform.appkit.JavaHelpers;
import org.ergoplatform.appkit.NetworkType;
import org.ergoplatform.appkit.*;
import sigmastate.Values;


public class ErgoScriptContract implements ErgoContract {
final private Constants _constants;
final private String _code;
private NetworkType _networkType;
private final NetworkType _networkType;

private ErgoScriptContract(Constants constants, String code, NetworkType networkType) {
_constants = constants;
Expand Down Expand Up @@ -49,4 +46,10 @@ public Values.ErgoTree getErgoTree() {
_constants, _code, _networkType.networkPrefix);
return ergoTree;
}

@Override
public Address getAddress() {
return Address.fromErgoTree(getErgoTree(), _networkType);
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package org.ergoplatform.appkit.impl;

import org.ergoplatform.ErgoAddress;
import org.ergoplatform.appkit.Address;
import org.ergoplatform.appkit.Constants;
import org.ergoplatform.appkit.ErgoContract;
import org.ergoplatform.appkit.NetworkType;
import sigmastate.Values;

public class ErgoTreeContract implements ErgoContract {
private final Values.ErgoTree _ergoTree;

public ErgoTreeContract(Values.ErgoTree ergoTree) {
private final NetworkType _networkType;
public ErgoTreeContract(Values.ErgoTree ergoTree, NetworkType networkType) {
_ergoTree = ergoTree;
_networkType = networkType;
}

@Override
Expand All @@ -30,4 +34,8 @@ public ErgoTreeContract substConstant(String name, Object value) {
public Values.ErgoTree getErgoTree() {
return _ergoTree;
}

@Override
public Address getAddress() { return Address.fromErgoTree(_ergoTree, _networkType); }

}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public String send(
BlockchainContext ctx,
Address recipient) {

ErgoContract contract = new ErgoTreeContract(recipient.getErgoAddress().script());
ErgoContract contract = new ErgoTreeContract(recipient.getErgoAddress().script(), recipient.getNetworkType());
SignedTransaction signed = putToContractTx(ctx, contract);
ctx.sendTransaction(signed);
return signed.toJson(true);
Expand Down Expand Up @@ -232,7 +232,7 @@ public static SignedTransaction spendBoxesTx(
ErgoProver sender, Address recipient, long amount, long fee) {
OutBox newBox = txB.outBoxBuilder()
.value(amount)
.contract(new ErgoTreeContract(recipient.getErgoAddress().script()))
.contract(new ErgoTreeContract(recipient.getErgoAddress().script(), recipient.getNetworkType()))
.build();

UnsignedTransaction tx = txB.boxesToSpend(boxes)
Expand Down
5 changes: 5 additions & 0 deletions lib-api/src/main/java/org/ergoplatform/appkit/InputBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,10 @@ public interface InputBox {
*/
String toJson(boolean prettyPrint, boolean formatJson);

/**
* Returns the serialized bytes representing this InputBox, including transaction reference data
*/
byte[] getBytes();

}

22 changes: 20 additions & 2 deletions lib-api/src/main/java/org/ergoplatform/appkit/OutBox.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.ergoplatform.appkit;

import org.ergoplatform.ErgoBoxCandidate;
import sigmastate.Values;

import java.util.List;

/**
* This interface is used to represent output boxes on newly created transactions.
Expand All @@ -23,9 +26,24 @@ public interface OutBox {
int getCreationHeight();

/**
* Returns a token with the given id.
* Returns list of tokens held in R2 of the current OutBox.
*/
List<ErgoToken> getTokens();

/**
* Returns list of additional registers that were set in this OutBox
*/
List<ErgoValue<?>> getRegisters();

/**
* Returns the ErgoTree of the script guarding the box
*/
Values.ErgoTree getErgoTree();

/**
* Returns the serialized bytes of this output box without any transaction reference data.
*/
ErgoToken token(ErgoId id);
byte[] getBytesWithNoRef();

/**
* Converts this box candidate into a new instance of {@link InputBox} by
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.ergoplatform.appkit;

import org.ergoplatform.ErgoAddress;

import java.util.List;

/**
* This interface is used to represent unsigned transactions after they are
* built using {@link UnsignedTransactionBuilder}.
Expand All @@ -8,4 +12,23 @@
* @see SignedTransaction
*/
public interface UnsignedTransaction {
/**
* Gets unsigned input boxes that will be used in this transaction
*/
List<InputBox> getInputs();

/**
* Gets output boxes that will be created by this transaction
*/
List<OutBox> getOutputs();

/**
* Gets data inputs that will be used in this transaction
*/
List<InputBox> getDataInputs();

/**
* Returns the change address associated with this unsigned transaction
*/
ErgoAddress getChangeAddress();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public BlockchainContextBase(NetworkType networkType) {

@Override
public ErgoContract newContract(Values.ErgoTree ergoTree) {
return new ErgoTreeContract(ergoTree);
return new ErgoTreeContract(ergoTree, _networkType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public String toJson(boolean prettyPrint, boolean formatJson) {
String json = gson.toJson(data);
return json;
}
@Override
public byte[] getBytes() { return _ergoBox.bytes(); }

public ErgoBox getErgoBox() {
return _ergoBox;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
package org.ergoplatform.appkit.impl

import org.ergoplatform.ErgoBoxCandidate
import org.ergoplatform.appkit.{ErgoId, ErgoToken, OutBox}
import org.ergoplatform.appkit.{ErgoToken, ErgoValue, InputBox, Iso, OutBox}
import scorex.util.ModifierId
import sigmastate.Values

class OutBoxImpl(_ctx: BlockchainContextImpl,
_ergoBoxCandidate: ErgoBoxCandidate) extends OutBox {
import java.util

class OutBoxImpl(_ctx: BlockchainContextImpl, _ergoBoxCandidate: ErgoBoxCandidate) extends OutBox {
override def getValue: Long = _ergoBoxCandidate.value

override def getCreationHeight: Int = _ergoBoxCandidate.creationHeight

override def token(id: ErgoId): ErgoToken = null
override def getTokens: util.List[ErgoToken] = Iso.isoTokensListToPairsColl.from(_ergoBoxCandidate.additionalTokens)

override def getRegisters: util.List[ErgoValue[_]] = {
val registers = _ergoBoxCandidate.additionalRegisters.values.toIndexedSeq
Iso.JListToIndexedSeq(Iso.isoErgoValueToSValue).from(registers)
}

override def getBytesWithNoRef: Array[Byte] = _ergoBoxCandidate.bytesWithNoRef

override def getErgoTree: Values.ErgoTree = _ergoBoxCandidate.ergoTree

private[impl] def getErgoBoxCandidate: ErgoBoxCandidate = _ergoBoxCandidate

override def convertToInputWith(txId: String, boxIndex: Short) = {
override def convertToInputWith(txId: String, boxIndex: Short): InputBox = {
val box = _ergoBoxCandidate.toBox(ModifierId @@ txId, boxIndex)
new InputBoxImpl(_ctx, box)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class UnsignedTransactionBuilderImpl(val _ctx: BlockchainContextImpl) extends Un
)

val stateContext = createErgoLikeStateContext
new UnsignedTransactionImpl(txWithExtensions, boxesToSpend, dataInputBoxes, stateContext)
new UnsignedTransactionImpl(txWithExtensions, boxesToSpend, dataInputBoxes, outputCandidates, changeAddress, stateContext, _ctx)
}

private def createErgoLikeStateContext: ErgoLikeStateContext = new ErgoLikeStateContext() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
package org.ergoplatform.appkit.impl;

import org.ergoplatform.ErgoAddress;
import org.ergoplatform.ErgoBox;
import org.ergoplatform.ErgoBoxCandidate;
import org.ergoplatform.UnsignedErgoLikeTransaction;
import org.ergoplatform.appkit.ExtendedInputBox;
import org.ergoplatform.appkit.UnsignedTransaction;
import org.ergoplatform.appkit.*;
import org.ergoplatform.wallet.protocol.context.ErgoLikeStateContext;

import java.util.List;
import java.util.stream.Collectors;

public class UnsignedTransactionImpl implements UnsignedTransaction {
private final UnsignedErgoLikeTransaction _tx;
private List<ExtendedInputBox> _boxesToSpend;
private List<ErgoBox> _dataBoxes;
private List<ErgoBoxCandidate> _outputs;
private ErgoAddress _changeAddress;
private ErgoLikeStateContext _stateContext;
private BlockchainContextImpl _ctx;


public UnsignedTransactionImpl(
UnsignedErgoLikeTransaction tx, List<ExtendedInputBox> boxesToSpend,
List<ErgoBox> dataBoxes, ErgoLikeStateContext stateContext) {
UnsignedErgoLikeTransaction tx, List<ExtendedInputBox> boxesToSpend,
List<ErgoBox> dataBoxes, List<ErgoBoxCandidate> outputs, ErgoAddress changeAddress, ErgoLikeStateContext stateContext, BlockchainContextImpl ctx) {
_tx = tx;
_boxesToSpend = boxesToSpend;
_dataBoxes = dataBoxes;
_outputs = outputs;
_changeAddress = changeAddress;
_stateContext = stateContext;
_ctx = ctx;
}

UnsignedErgoLikeTransaction getTx() {
Expand All @@ -38,4 +47,24 @@ public List<ErgoBox> getDataBoxes() {
public ErgoLikeStateContext getStateContext() {
return _stateContext;
}

@Override
public List<InputBox> getInputs() {
return _boxesToSpend.stream().map(b -> new InputBoxImpl(_ctx, b.box())).collect(Collectors.toList());
}

@Override
public List<OutBox> getOutputs() {
return _outputs.stream().map(b -> new OutBoxImpl(_ctx, b)).collect(Collectors.toList());
}

@Override
public List<InputBox> getDataInputs() {
return _dataBoxes.stream().map(b -> new InputBoxImpl(_ctx, b)).collect(Collectors.toList());
}

@Override
public ErgoAddress getChangeAddress() {
return _changeAddress;
}
}

0 comments on commit d90135c

Please sign in to comment.