use of co.rsk.peg.whitelist.OneOffWhiteListEntry in project rskj by rsksmart.
the class BridgeSupport method addOneOffLockWhitelistAddress.
/**
* Adds the given address to the lock whitelist.
* Returns 1 upon success, or -1 if the address was
* already in the whitelist.
* @param addressBase58 the base58-encoded address to add to the whitelist
* @param maxTransferValue the max amount of satoshis enabled to transfer for this address
* @return 1 upon success, -1 if the address was already
* in the whitelist, -2 if address is invalid
* LOCK_WHITELIST_GENERIC_ERROR_CODE otherwise.
*/
public Integer addOneOffLockWhitelistAddress(Transaction tx, String addressBase58, BigInteger maxTransferValue) {
try {
Address address = getParsedAddress(addressBase58);
Coin maxTransferValueCoin = Coin.valueOf(maxTransferValue.longValueExact());
return this.addLockWhitelistAddress(tx, new OneOffWhiteListEntry(address, maxTransferValueCoin));
} catch (AddressFormatException e) {
logger.warn(INVALID_ADDRESS_FORMAT_MESSAGE, e);
return LOCK_WHITELIST_INVALID_ADDRESS_FORMAT_ERROR_CODE;
}
}
use of co.rsk.peg.whitelist.OneOffWhiteListEntry in project rskj by rsksmart.
the class BridgeSerializationUtils method serializeOneOffLockWhitelist.
/**
* Serializes the data stored in the Tuple.
* @param data data MUST be composed of a list of {@link co.rsk.peg.whitelist.OneOffWhiteListEntry} and the value of disableBlockHeight obtained from {@link co.rsk.peg.whitelist.LockWhitelist}
* @return the serialized data
*/
public static byte[] serializeOneOffLockWhitelist(Pair<List<OneOffWhiteListEntry>, Integer> data) {
List<OneOffWhiteListEntry> entries = data.getLeft();
Integer disableBlockHeight = data.getRight();
int serializationSize = entries.size() * 2 + 1;
byte[][] serializedLockWhitelist = new byte[serializationSize][];
for (int i = 0; i < entries.size(); i++) {
OneOffWhiteListEntry entry = entries.get(i);
serializedLockWhitelist[2 * i] = RLP.encodeElement(entry.address().getHash160());
serializedLockWhitelist[2 * i + 1] = RLP.encodeBigInteger(BigInteger.valueOf(entry.maxTransferValue().longValue()));
}
serializedLockWhitelist[serializationSize - 1] = RLP.encodeBigInteger(BigInteger.valueOf(disableBlockHeight));
return RLP.encodeList(serializedLockWhitelist);
}
use of co.rsk.peg.whitelist.OneOffWhiteListEntry in project rskj by rsksmart.
the class BridgeStorageProviderTest method saveLockWhiteListAfterGetWithData.
@Test
public void saveLockWhiteListAfterGetWithData() {
AtomicReference<Boolean> storageCalled = new AtomicReference<>();
storageCalled.set(Boolean.FALSE);
PowerMockito.mockStatic(BridgeSerializationUtils.class);
Repository repositoryMock = mock(Repository.class);
OneOffWhiteListEntry oneOffEntry = new OneOffWhiteListEntry(getBtcAddress("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), Coin.COIN);
BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getNetworkConstants().getBridgeConstants(), config.getActivationConfig().forBlock(500L));
when(repositoryMock.getStorageBytes(any(RskAddress.class), eq(DataWord.valueOf("lockWhitelist".getBytes(StandardCharsets.UTF_8))))).then((InvocationOnMock invocation) -> new byte[] { (byte) 0xaa });
PowerMockito.when(BridgeSerializationUtils.deserializeOneOffLockWhitelistAndDisableBlockHeight(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> {
HashMap<Address, LockWhitelistEntry> map = new HashMap<>();
map.put(oneOffEntry.address(), oneOffEntry);
return Pair.of(map, 0);
});
PowerMockito.when(BridgeSerializationUtils.serializeOneOffLockWhitelist(any(Pair.class))).thenReturn(new byte[] { (byte) 0xee });
Mockito.doAnswer((InvocationOnMock invocation) -> {
storageCalled.set(Boolean.TRUE);
return null;
}).when(repositoryMock).addStorageBytes(any(RskAddress.class), eq(DataWord.valueOf("lockWhitelist".getBytes(StandardCharsets.UTF_8))), eq(new byte[] { (byte) 0xee }));
Assert.assertTrue(storageProvider.getLockWhitelist().getSize() > 0);
storageProvider.saveLockWhitelist();
Assert.assertTrue(storageCalled.get());
}
use of co.rsk.peg.whitelist.OneOffWhiteListEntry in project rskj by rsksmart.
the class BridgeSerializationUtilsTest method serializeLockWhitelist.
@Test
public void serializeLockWhitelist() throws Exception {
byte[][] addressesBytes = new byte[][] { BtcECKey.fromPrivate(BigInteger.valueOf(100)).getPubKeyHash(), BtcECKey.fromPrivate(BigInteger.valueOf(200)).getPubKeyHash(), BtcECKey.fromPrivate(BigInteger.valueOf(300)).getPubKeyHash(), BtcECKey.fromPrivate(BigInteger.valueOf(400)).getPubKeyHash(), BtcECKey.fromPrivate(BigInteger.valueOf(500)).getPubKeyHash(), BtcECKey.fromPrivate(BigInteger.valueOf(600)).getPubKeyHash() };
Coin maxToTransfer = Coin.CENT;
LockWhitelist lockWhitelist = new LockWhitelist(Arrays.stream(addressesBytes).map(bytes -> new Address(NetworkParameters.fromID(NetworkParameters.ID_REGTEST), bytes)).collect(Collectors.toMap(Function.identity(), k -> new OneOffWhiteListEntry(k, maxToTransfer))), 0);
byte[] result = BridgeSerializationUtils.serializeOneOffLockWhitelist(Pair.of(lockWhitelist.getAll(OneOffWhiteListEntry.class), lockWhitelist.getDisableBlockHeight()));
StringBuilder expectedBuilder = new StringBuilder();
expectedBuilder.append("f897");
Arrays.stream(addressesBytes).sorted(UnsignedBytes.lexicographicalComparator()).forEach(bytes -> {
expectedBuilder.append("94");
expectedBuilder.append(ByteUtil.toHexString(bytes));
expectedBuilder.append("83");
expectedBuilder.append(ByteUtil.toHexString(BigInteger.valueOf(maxToTransfer.value).toByteArray()));
});
expectedBuilder.append("80");
String expected = expectedBuilder.toString();
Assert.assertEquals(expected, ByteUtil.toHexString(result));
}
use of co.rsk.peg.whitelist.OneOffWhiteListEntry in project rskj by rsksmart.
the class BridgeTestPowerMock method getLockWhitelistAddress.
@Test
public void getLockWhitelistAddress() {
BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(constants.getBridgeConstants().getBtcParams()), constants.getBridgeConstants(), activationConfig);
Bridge bridge = new Bridge(PrecompiledContracts.BRIDGE_ADDR, constants, activationConfig, bridgeSupportFactory);
bridge.init(null, getGenesisBlock(), createRepository().startTracking(), null, null, null);
BridgeSupport bridgeSupportMock = mock(BridgeSupport.class);
OneOffWhiteListEntry mockedEntry10 = new OneOffWhiteListEntry(new BtcECKey().toAddress(networkParameters), Coin.COIN);
OneOffWhiteListEntry mockedEntry20 = new OneOffWhiteListEntry(new BtcECKey().toAddress(networkParameters), Coin.COIN);
Whitebox.setInternalState(bridge, "bridgeSupport", bridgeSupportMock);
when(bridgeSupportMock.getLockWhitelistEntryByIndex(10)).then((InvocationOnMock invocation) -> mockedEntry10);
when(bridgeSupportMock.getLockWhitelistEntryByIndex(20)).then((InvocationOnMock invocation) -> mockedEntry20);
Assert.assertEquals(mockedEntry10.address().toBase58(), bridge.getLockWhitelistAddress(new Object[] { BigInteger.valueOf(10) }));
Assert.assertEquals(mockedEntry20.address().toBase58(), bridge.getLockWhitelistAddress(new Object[] { BigInteger.valueOf(20) }));
}
Aggregations