use of org.ethereum.util.RLPElement in project rskj by rsksmart.
the class BridgeSupportTest method addSignatureCreateEventLog.
@Test
public void addSignatureCreateEventLog() throws Exception {
// Setup
Federation federation = bridgeConstants.getGenesisFederation();
Repository track = new RepositoryImpl(config).startTracking();
BridgeStorageProvider provider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants);
// Build prev btc tx
BtcTransaction prevTx = new BtcTransaction(btcParams);
TransactionOutput prevOut = new TransactionOutput(btcParams, prevTx, Coin.FIFTY_COINS, federation.getAddress());
prevTx.addOutput(prevOut);
// Build btc tx to be signed
BtcTransaction btcTx = new BtcTransaction(btcParams);
btcTx.addInput(prevOut).setScriptSig(PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(federation));
TransactionOutput output = new TransactionOutput(btcParams, btcTx, Coin.COIN, new BtcECKey().toAddress(btcParams));
btcTx.addOutput(output);
// Save btc tx to be signed
final Keccak256 rskTxHash = PegTestUtils.createHash3();
provider.getRskTxsWaitingForSignatures().put(rskTxHash, btcTx);
provider.save();
track.commit();
// Setup BridgeSupport
List<LogInfo> eventLogs = new ArrayList<>();
BridgeEventLogger eventLogger = new BridgeEventLoggerImpl(bridgeConstants, eventLogs);
BridgeSupport bridgeSupport = new BridgeSupport(config, track, eventLogger, contractAddress, null);
// Create signed hash of Btc tx
Script inputScript = btcTx.getInputs().get(0).getScriptSig();
List<ScriptChunk> chunks = inputScript.getChunks();
byte[] program = chunks.get(chunks.size() - 1).data;
Script redeemScript = new Script(program);
Sha256Hash sigHash = btcTx.hashForSignature(0, redeemScript, BtcTransaction.SigHash.ALL, false);
BtcECKey privateKeyToSignWith = ((BridgeRegTestConstants) bridgeConstants).getFederatorPrivateKeys().get(0);
BtcECKey.ECDSASignature sig = privateKeyToSignWith.sign(sigHash);
List derEncodedSigs = Collections.singletonList(sig.encodeToDER());
BtcECKey federatorPubKey = findPublicKeySignedBy(federation.getPublicKeys(), privateKeyToSignWith);
bridgeSupport.addSignature(1, federatorPubKey, derEncodedSigs, rskTxHash.getBytes());
Assert.assertEquals(1, eventLogs.size());
// Assert address that made the log
LogInfo result = eventLogs.get(0);
Assert.assertArrayEquals(PrecompiledContracts.BRIDGE_ADDR.getBytes(), result.getAddress());
// Assert log topics
Assert.assertEquals(1, result.getTopics().size());
Assert.assertEquals(Bridge.ADD_SIGNATURE_TOPIC, result.getTopics().get(0));
// Assert log data
Assert.assertNotNull(result.getData());
List<RLPElement> rlpData = RLP.decode2(result.getData());
Assert.assertEquals(1, rlpData.size());
RLPList dataList = (RLPList) rlpData.get(0);
Assert.assertEquals(3, dataList.size());
Assert.assertArrayEquals(btcTx.getHashAsString().getBytes(), dataList.get(0).getRLPData());
Assert.assertArrayEquals(federatorPubKey.getPubKeyHash(), dataList.get(1).getRLPData());
Assert.assertArrayEquals(rskTxHash.getBytes(), dataList.get(2).getRLPData());
}
use of org.ethereum.util.RLPElement in project rskj by rsksmart.
the class RLPTest method encodeDecodeLongByteArrayWithTwoBytesLengthBorderCaseUsingEncode.
@Test
public void encodeDecodeLongByteArrayWithTwoBytesLengthBorderCaseUsingEncode() {
byte[] bytes = new byte[256 * 256 - 1];
byte[] encoded = RLP.encode(bytes);
Assert.assertNotNull(encoded);
Assert.assertEquals(3 + 256 * 256 - 1, encoded.length);
Assert.assertEquals((byte) (183 + 2), encoded[0]);
Assert.assertEquals((byte) 0xff, encoded[1]);
Assert.assertEquals((byte) 0xff, encoded[2]);
RLPElement element = RLP.decode2OneItem(encoded, 0);
Assert.assertNotNull(element);
byte[] decoded = element.getRLPData();
Assert.assertNotNull(decoded);
Assert.assertArrayEquals(bytes, decoded);
}
use of org.ethereum.util.RLPElement in project rskj by rsksmart.
the class RLPTest method encodeDecodeLongByteArrayWithOneByteLength.
@Test
public void encodeDecodeLongByteArrayWithOneByteLength() {
byte[] bytes = new byte[56];
byte[] encoded = RLP.encodeElement(bytes);
Assert.assertNotNull(encoded);
Assert.assertEquals(2 + 56, encoded.length);
Assert.assertEquals((byte) (183 + 1), encoded[0]);
Assert.assertEquals((byte) 56, encoded[1]);
RLPElement element = RLP.decode2OneItem(encoded, 0);
Assert.assertNotNull(element);
byte[] decoded = element.getRLPData();
Assert.assertNotNull(decoded);
Assert.assertArrayEquals(bytes, decoded);
}
use of org.ethereum.util.RLPElement in project rskj by rsksmart.
the class RLPTest method encodeDecodeShortListWithTwoByteArraysWithTwoBytesLength.
@Test
public void encodeDecodeShortListWithTwoByteArraysWithTwoBytesLength() {
byte[] value1 = new byte[126];
byte[] value2 = new byte[126];
byte[] element1 = RLP.encodeElement(value1);
byte[] element2 = RLP.encodeElement(value2);
byte[] encoded = RLP.encodeList(element1, element2);
Assert.assertNotNull(encoded);
Assert.assertEquals(1 + 2 + 2 + 126 + 2 + 126, encoded.length);
Assert.assertEquals((byte) (247 + 2), encoded[0]);
Assert.assertEquals((byte) (1), encoded[1]);
Assert.assertEquals((byte) (0), encoded[2]);
ArrayList<RLPElement> list = RLP.decode2(encoded);
Assert.assertNotNull(list);
Assert.assertEquals(1, list.size());
RLPList list2 = (RLPList) list.get(0);
Assert.assertNotNull(list2);
Assert.assertEquals(2, list2.size());
Assert.assertArrayEquals(value1, list2.get(0).getRLPData());
Assert.assertArrayEquals(value2, list2.get(1).getRLPData());
}
use of org.ethereum.util.RLPElement in project rskj by rsksmart.
the class RLPTest method encodeDecodeLongByteArrayWithTwoBytesLengthBorderCase.
@Test
public void encodeDecodeLongByteArrayWithTwoBytesLengthBorderCase() {
byte[] bytes = new byte[256 * 256 - 1];
byte[] encoded = RLP.encodeElement(bytes);
Assert.assertNotNull(encoded);
Assert.assertEquals(3 + 256 * 256 - 1, encoded.length);
Assert.assertEquals((byte) (183 + 2), encoded[0]);
Assert.assertEquals((byte) 0xff, encoded[1]);
Assert.assertEquals((byte) 0xff, encoded[2]);
RLPElement element = RLP.decode2OneItem(encoded, 0);
Assert.assertNotNull(element);
byte[] decoded = element.getRLPData();
Assert.assertNotNull(decoded);
Assert.assertArrayEquals(bytes, decoded);
}
Aggregations