BTCMarketsDigest extends BaseParamsDigest { String digest(String url, String nonce, String requestBody, String queryString) { Mac mac = getMac(); if (!url.startsWith("/")) { url = "/" + url; } mac.update(url.getBytes()); mac.update("\n".getBytes()); if (includeQueryString && queryString != null && !queryString.isEmpty()) { mac.update(queryString.getBytes()); mac.update("\n".getBytes()); } mac.update(nonce.getBytes()); mac.update("\n".getBytes()); if (requestBody != null && !requestBody.isEmpty()) { mac.update(requestBody.getBytes()); } return Base64.getEncoder().encodeToString(mac.doFinal()); } BTCMarketsDigest(String secretKey, Boolean includeQueryString); @Override String digestParams(RestInvocation inv);  }
@Test public void shouldEncode() { String expected = "u+WtKtUXd4CkUlfYJvL7Li4kr5LyNluP/m1Xqk4CMmTnsSymWTTpxpnwWD+RTseXJVsXUgrw6fZusGTjfS9knQ=="; String encodedWithoutSlash = btcMarketsDigest.digest("path/to/method", "nonce", "test", null); String encodedWithSlash = btcMarketsDigest.digest("/path/to/method", "nonce", "test", null); assertThat(encodedWithoutSlash).isEqualTo(expected); assertThat(encodedWithSlash).isEqualTo(expected); }
@Test public void shouldEncodeWithoutBody() { String expected = "/LEFVtbNw+pgTFK/thj4xWzKuNz16Tub2+Jm8Ooep4o3XH6tGalk6AQxFiUvnDmN+w3NQpu+qCyoO5ap6OseYQ=="; String encodedEmpty = btcMarketsDigest.digest("/path/to/method", "nonce", "", null); String encodedNull = btcMarketsDigest.digest("/path/to/method", "nonce", null, null); assertThat(encodedEmpty).isEqualTo(expected); assertThat(encodedNull).isEqualTo(expected); }
@Test public void shouldEncodeQueryString() { String expected = "bhjZf81rqrcSTrwVBZ1o34a2gALrO4yXT6u/zuN0PcfEJOehIDk1qnEID7UcRVttT3ePJkDMphixhBeusSqW8Q=="; String encodedEmpty = btcMarketsDigest.digest( "/v2/order/trade/history/ETH/AUD", "nonce", "", "indexForward=true&limit=10&since=698825"); String encodedNull = btcMarketsDigest.digest( "/v2/order/trade/history/ETH/AUD", "nonce", null, "indexForward=true&limit=10&since=698825"); assertThat(encodedEmpty).isEqualTo(expected); assertThat(encodedNull).isEqualTo(expected); }