Skip to content

Commit

Permalink
Add backwards-compatibility for UMA v0 from v1 SDK (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyav authored Mar 21, 2024
1 parent bd005eb commit 9beefe1
Show file tree
Hide file tree
Showing 9 changed files with 680 additions and 142 deletions.
183 changes: 177 additions & 6 deletions javatest/src/test/java/me/uma/javatest/UmaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import static java.util.Objects.requireNonNull;
import static org.junit.jupiter.api.Assertions.*;
import static me.uma.protocol.CurrencyUtils.createCurrency;

public class UmaTest {
UmaProtocolHelper umaProtocolHelper = new UmaProtocolHelper(new InMemoryPublicKeyCache(), new TestUmaRequester());
Expand Down Expand Up @@ -82,7 +83,7 @@ public void testGetLnurlpRequest() throws Exception {
}

@Test
public void testGetLnurlpResponse() throws Exception {
public void testGetLnurlpResponse_umaV1() throws Exception {
String lnurlpUrl = umaProtocolHelper.getSignedLnurlpRequestUrl(
privateKeyBytes(),
"[email protected]",
Expand All @@ -108,13 +109,15 @@ public void testGetLnurlpResponse() throws Exception {
)
),
List.of(
new Currency(
createCurrency(
"USD",
"US Dollar",
"$",
34_150,
new CurrencyConvertible(1, 10_000_000),
2
2,
1,
10_000_000,
"1.0"
)
),
KycStatus.VERIFIED
Expand All @@ -125,13 +128,133 @@ public void testGetLnurlpResponse() throws Exception {
LnurlpResponse parsedResponse = umaProtocolHelper.parseAsLnurlpResponse(responseJson);
assertNotNull(parsedResponse);
assertEquals(lnurlpResponse, parsedResponse);
assertNotNull(parsedResponse.asUmaResponse());
assertEquals(1, parsedResponse.asUmaResponse().getCurrencies().get(0).minSendable());
assertEquals(10_000_000, parsedResponse.asUmaResponse().getCurrencies().get(0).maxSendable());
assertTrue(umaProtocolHelper.verifyLnurlpResponseSignature(
requireNonNull(parsedResponse.asUmaResponse()), new PubKeyResponse(publicKeyBytes(), publicKeyBytes()),
new InMemoryNonceCache(1L)));
}

@Test
public void testGetPayReqResponseSync() throws Exception {
public void testGetLnurlpResponse_umaV0() throws Exception {
String lnurlpUrl = umaProtocolHelper.getSignedLnurlpRequestUrl(
privateKeyBytes(),
"[email protected]",
"https://vasp.com",
true);
LnurlpRequest request = umaProtocolHelper.parseLnurlpRequest(lnurlpUrl);
assertNotNull(request);
LnurlpResponse lnurlpResponse = umaProtocolHelper.getLnurlpResponse(
request,
privateKeyBytes(),
true,
"https://vasp2.com/callback",
"encoded metadata",
1,
10_000_000,
CounterPartyData.createCounterPartyDataOptions(
Map.of(
"name", false,
"email", false,
"identity", true,
"compliance", true
)
),
List.of(
createCurrency(
"USD",
"US Dollar",
"$",
34_150,
2,
1,
10_000_000,
"0.3"
)
),
KycStatus.VERIFIED
);
assertNotNull(lnurlpResponse);
String responseJson = lnurlpResponse.toJson();
System.out.println(responseJson);
LnurlpResponse parsedResponse = umaProtocolHelper.parseAsLnurlpResponse(responseJson);
assertNotNull(parsedResponse);
assertEquals(lnurlpResponse, parsedResponse);
assertNotNull(parsedResponse.asUmaResponse());
assertEquals(1, parsedResponse.asUmaResponse().getCurrencies().get(0).minSendable());
assertEquals(10_000_000, parsedResponse.asUmaResponse().getCurrencies().get(0).maxSendable());
assertTrue(umaProtocolHelper.verifyLnurlpResponseSignature(
parsedResponse.asUmaResponse(), new PubKeyResponse(publicKeyBytes(), publicKeyBytes()),
new InMemoryNonceCache(1L)));
}

@Test
public void testGetPayRequest_umaV1() throws Exception {
PayRequest request = umaProtocolHelper.getPayRequest(
publicKeyBytes(),
privateKeyBytes(),
"USD",
100L,
true,
"[email protected]",
KycStatus.VERIFIED,
"",
null,
null,
null,
"payerName",
"payerEmail",
null,
null,
"comment",
"1.0"
);
assertNotNull(request);
System.out.println(request);
assertTrue(umaProtocolHelper.verifyPayReqSignature(
request, new PubKeyResponse(publicKeyBytes(), publicKeyBytes()),
new InMemoryNonceCache(1L)));
String requestJson = request.toJson();
PayRequest parsedRequest = umaProtocolHelper.parseAsPayRequest(requestJson);
assertNotNull(parsedRequest);
assertEquals(request, parsedRequest);
}

@Test
public void testGetPayRequest_umaV0() throws Exception {
PayRequest request = umaProtocolHelper.getPayRequest(
publicKeyBytes(),
privateKeyBytes(),
"USD",
100L,
true,
"[email protected]",
KycStatus.VERIFIED,
"",
null,
null,
null,
"payerName",
"payerEmail",
null,
null,
"comment",
"0.3"
);
assertNotNull(request);
System.out.println(request);
assertTrue(umaProtocolHelper.verifyPayReqSignature(
request, new PubKeyResponse(publicKeyBytes(), publicKeyBytes()),
new InMemoryNonceCache(1L)));
String requestJson = request.toJson();
PayRequest parsedRequest = umaProtocolHelper.parseAsPayRequest(requestJson);
assertNotNull(parsedRequest);
assertEquals(request, parsedRequest);
}

@Test
public void testGetPayReqResponseSync_umaV1() throws Exception {
PayRequest request = umaProtocolHelper.getPayRequest(
publicKeyBytes(),
privateKeyBytes(),
Expand All @@ -154,14 +277,62 @@ public void testGetPayReqResponseSync() throws Exception {
null,
"",
privateKeyBytes(),
PayeeData.createPayeeData(null, "[email protected]")
PayeeData.createPayeeData(null, "[email protected]"),
null,
null,
"1.0"
);
assertNotNull(response);
assertEquals("lnbc12345", response.getEncodedInvoice());
System.out.println(response);
assertTrue(umaProtocolHelper.verifyPayReqResponseSignature(
response, new PubKeyResponse(publicKeyBytes(), publicKeyBytes()),
"[email protected]", new InMemoryNonceCache(1L)));
String responseJson = response.toJson();
PayReqResponse parsedResponse = umaProtocolHelper.parseAsPayReqResponse(responseJson);
assertNotNull(parsedResponse);
assertEquals(response, parsedResponse);
}

@Test
public void testGetPayReqResponseSync_umaV0() throws Exception {
PayRequest request = umaProtocolHelper.getPayRequest(
publicKeyBytes(),
privateKeyBytes(),
"USD",
100L,
true,
"[email protected]",
KycStatus.VERIFIED,
""
);
PayReqResponse response = umaProtocolHelper.getPayReqResponseSync(
request,
new TestSyncUmaInvoiceCreator(),
"metadata",
"USD",
2,
12345.0,
0L,
List.of(),
null,
"",
privateKeyBytes(),
PayeeData.createPayeeData(null, "[email protected]"),
null,
null,
"0.3"
);
assertNotNull(response);
assertEquals("lnbc12345", response.getEncodedInvoice());
System.out.println(response);
assertTrue(umaProtocolHelper.verifyPayReqResponseSignature(
response, new PubKeyResponse(publicKeyBytes(), publicKeyBytes()),
"[email protected]", new InMemoryNonceCache(1L)));
String responseJson = response.toJson();
PayReqResponse parsedResponse = umaProtocolHelper.parseAsPayReqResponse(responseJson);
assertNotNull(parsedResponse);
assertEquals(response, parsedResponse);
}

@Test
Expand Down
Loading

0 comments on commit 9beefe1

Please sign in to comment.