-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIlpCryptoLib.java
More file actions
57 lines (42 loc) · 2 KB
/
Copy pathIlpCryptoLib.java
File metadata and controls
57 lines (42 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.l1p.interop.scheme.adapter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.Base64;
import java.util.UUID;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.interledger.InterledgerAddress;
import org.interledger.InterledgerAddressBuilder;
import org.interledger.codecs.CodecContext;
import org.interledger.codecs.CodecContextFactory;
import org.interledger.ipr.InterledgerPaymentRequest;
import org.interledger.ipr.InterledgerPaymentRequestBuilder;
public class IlpCryptoLib {
private static Logger log = LogManager.getLogger(IlpCryptoLib.class);
public String cryptoEncode (String destAddr, String destAmnt){
log.info("received destination address: " + destAddr + " and destination amount: " + destAmnt);
InterledgerAddress destinationAddress = InterledgerAddressBuilder.builder().value(destAddr).build();
long destinationAmount = Long.parseLong(destAmnt);
ZonedDateTime expiresAt = ZonedDateTime.now().plusMinutes(1);
byte[] receiverSecret = new byte[32];
String paymentId = UUID.randomUUID().toString();
InterledgerPaymentRequestBuilder builder = new InterledgerPaymentRequestBuilder(
destinationAddress,
destinationAmount,
expiresAt,
receiverSecret);
builder.setEncrypted(false);
builder.getPskMessageBuilder().addPublicHeader("Payment-Id", paymentId);
CodecContext context = CodecContextFactory.interledger();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
context.write(InterledgerPaymentRequest.class, builder.getIpr(), outputStream);
} catch (IOException e) {
throw new RuntimeException("Error encoding Interledger Packet Request.", e);
}
String cryptoEncode = Base64.getUrlEncoder().encodeToString(outputStream.toByteArray());
log.info("returning the following crypto encode value: " + cryptoEncode);
return cryptoEncode;
}
}