Skip to content

Instantly share code, notes, and snippets.

@ice09
Last active January 14, 2021 13:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ice09/33b46d31c4be7b434f299c0bcacbee56 to your computer and use it in GitHub Desktop.
Save ice09/33b46d31c4be7b434f299c0bcacbee56 to your computer and use it in GitHub Desktop.
uRaiden balance proof with web3j
private void pkDisplay(Web3j web3j) throws Exception {
String privateKey1 = "1587352d059682a7ebb86e4aecc86f73cf418d33f9af4178ce344a3dd41a813b";
Credentials credentials = Credentials.create(privateKey1);
log.info("Address: " + credentials.getAddress());
List<String> labels = Arrays.asList(
"string message_id",
"address receiver",
"uint32 block_created",
"uint192 balance",
"address contract"
);
ByteBuffer buffer = ByteBuffer.allocate(labels.stream().mapToInt(a -> a.getBytes().length).sum());
for (String a : labels) {
buffer.put(a.getBytes());
}
byte[] array = buffer.array();
Tester contract = Tester.deploy(
web3j, credentials,
ManagedTransaction.GAS_PRICE, Contract.GAS_LIMIT).send();
String contractAddress = contract.getContractAddress();
List<byte[]> values = Arrays.asList(
"Sender balance proof signature".getBytes(),
Numeric.hexStringToByteArray(credentials.getAddress()),
Numeric.toBytesPadded(BigInteger.valueOf(400000), 4),
Numeric.toBytesPadded(BigInteger.valueOf(100), 24),
Numeric.hexStringToByteArray(contractAddress)
);
ByteBuffer bufferValues = ByteBuffer.allocate(values.stream().mapToInt(a -> a.length).sum());
for (byte[] a : values) {
bufferValues.put(a);
}
byte[] arrayValues = bufferValues.array();
ByteBuffer byteArrayBuffer = ByteBuffer.allocate(64);
byteArrayBuffer.put(Hash.sha3(array));
byteArrayBuffer.put(Hash.sha3(arrayValues));
byte[] arrayValuesBuffer = byteArrayBuffer.array();
Sign.SignatureData signature = Sign.signMessage(arrayValuesBuffer, credentials.getEcKeyPair());
ByteBuffer sigBuffer = ByteBuffer.allocate(signature.getR().length + signature.getS().length + 1);
sigBuffer.put(signature.getR());
sigBuffer.put(signature.getS());
sigBuffer.put(signature.getV());
log.info("Smart contract deployed to address " + contractAddress);
log.info("View contract at https://rinkeby.etherscan.io/address/" + contractAddress);
log.info("Value stored in remote smart contract: " + contract.extractBalanceProofSignature(
credentials.getAddress(),
BigInteger.valueOf(400000),
BigInteger.valueOf(100), sigBuffer.array()).send());
}
@ice09
Copy link
Author

ice09 commented Mar 20, 2018

Please use the private key with care, it has some ETH on Rinkeby. For creating the balance proof, ETH is not necessary, but for the deployment of the uRaiden smart contract.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment