Skip to content

Instantly share code, notes, and snippets.

@Sajjon
Created February 1, 2021 16:31
Show Gist options
  • Save Sajjon/ea03eebf3891cd92bf3f150ae1e7a711 to your computer and use it in GitHub Desktop.
Save Sajjon/ea03eebf3891cd92bf3f150ae1e7a711 to your computer and use it in GitHub Desktop.
CBOREncodingParticlesTest.java
/*
* (C) Copyright 2020 Radix DLT Ltd
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the “Software”),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package com.radixdlt.client.atommodel.tokens;
import com.google.common.collect.ImmutableMap;
import com.radixdlt.client.application.translate.tokens.TokenUnitConversions;
import com.radixdlt.client.atommodel.rri.RRIParticle;
import com.radixdlt.identifiers.RRI;
import com.radixdlt.identifiers.RadixAddress;
import com.radixdlt.serialization.DsonOutput;
import com.radixdlt.serialization.Serialization;
import com.radixdlt.serialization.core.ClasspathScanningSerializationPolicy;
import com.radixdlt.serialization.core.ClasspathScanningSerializerIds;
import com.radixdlt.utils.Bytes;
import com.radixdlt.utils.UInt256;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class CBOREncodingParticlesTest {
private final Serialization serialization = Serialization.create(
ClasspathScanningSerializerIds.create(),
ClasspathScanningSerializationPolicy.create()
);
@Test
public void testAddressCBOR() {
var address = RadixAddress.from("9S8khLHZa6FsyGo634xQo9QwLgSHGpXHHW764D5mPYBcrnfZV6RT");
var expected = "582704390279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798b1186a1e";
var actual = serialization.toDson(address, DsonOutput.Output.ALL);
assertEquals(expected, Bytes.toHexString(actual));
}
@Test
public void testRRICBOR() {
var rri = RRI.from("/9S8khLHZa6FsyGo634xQo9QwLgSHGpXHHW764D5mPYBcrnfZV6RT/FOOBAR");
var expected = "583d062f3953386b684c485a6136467379476f36333478516f3951774c67534847705848485737363444356d50594263726e665a563652542f464f4f424152";
var actual = serialization.toDson(rri, DsonOutput.Output.ALL);
assertEquals(expected, Bytes.toHexString(actual));
}
@Test
public void testAmountCBOR() {
var amount = TokenUnitConversions.unitsToSubunits(6L);
assertEquals("6000000000000000000", amount.toString(10));
assertEquals("53444835ec580000", amount.toString(16));
// assertEquals("7", UInt256.from(7).toString(10));
var expected = "58210500000000000000000000000000000000000000000000000053444835ec580000";
var actual = serialization.toDson(amount, DsonOutput.Output.ALL);
assertEquals(expected, Bytes.toHexString(actual));
}
@Test
public void testAmountCBOR2() {
var amount = UInt256.from(7);
var expected = "5821050000000000000000000000000000000000000000000000000000000000000007";
var actual = serialization.toDson(amount, DsonOutput.Output.ALL);
assertEquals(expected, Bytes.toHexString(actual));
}
@Test
public void testNonceCBOR() {
var rri = RRI.from("/9S8khLHZa6FsyGo634xQo9QwLgSHGpXHHW764D5mPYBcrnfZV6RT/FOOBAR");
var nonce = Long.MAX_VALUE;
var rriParticle = new RRIParticle(rri, nonce);
var expected = "bf6c64657374696e6174696f6e73815102b1cd0a4eb6d1cea5eb288fb4474ac403656e6f6e63651b7fffffffffffffff63727269583d062f3953386b684c485a6136467379476f36333478516f3951774c67534847705848485737363444356d50594263726e665a563652542f464f4f4241526a73657269616c697a65727372616469782e7061727469636c65732e7272696776657273696f6e1864ff";
var actual = serialization.toDson(rriParticle, DsonOutput.Output.ALL);
assertEquals(expected, Bytes.toHexString(actual));
}
@Test
public void test_ttp_cbor() {
var address = RadixAddress.from("9S8khLHZa6FsyGo634xQo9QwLgSHGpXHHW764D5mPYBcrnfZV6RT");
var rri = RRI.of(address, "FOOBAR");
var permissions = ImmutableMap.of(
MutableSupplyTokenDefinitionParticle.TokenTransition.BURN, TokenPermission.ALL,
MutableSupplyTokenDefinitionParticle.TokenTransition.MINT, TokenPermission.ALL
);
var ttp = new TransferrableTokensParticle(
UInt256.SIX,
UInt256.THREE,
address,
1337,
rri,
permissions
);
var dsonBytes = ttp.toDson();
var actual = Bytes.toHexString(dsonBytes);
var expected = "bf6761646472657373582704390279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798b1186a1e66616d6f756e7458210500000000000000000000000000000000000000000000000000000000000000066b6772616e756c61726974795821050000000000000000000000000000000000000000000000000000000000000003656e6f6e63651905396b7065726d697373696f6e73bf646275726e63616c6c646d696e7463616c6cff6a73657269616c697a6572782472616469782e7061727469636c65732e7472616e736665727261626c655f746f6b656e737818746f6b656e446566696e6974696f6e5265666572656e6365583d062f3953386b684c485a6136467379476f36333478516f3951774c67534847705848485737363444356d50594263726e665a563652542f464f4f4241526776657273696f6e1864ff";
assertEquals(expected, actual);
var expectedJSON = "{\"serializer\":\"radix.particles.transferrable_tokens\",\"version\":100,\"address\":\":adr:9S8khLHZa6FsyGo634xQo9QwLgSHGpXHHW764D5mPYBcrnfZV6RT\",\"tokenDefinitionReference\":\":rri:/9S8khLHZa6FsyGo634xQo9QwLgSHGpXHHW764D5mPYBcrnfZV6RT/FOOBAR\",\"granularity\":\":u20:3\",\"nonce\":1337,\"amount\":\":u20:6\",\"permissions\":{\"burn\":\":str:all\",\"mint\":\":str:all\"}}";
var json = serialization.toJson(ttp, DsonOutput.Output.ALL);
assertEquals(expectedJSON, json);
}
@Test
public void test_uatp_cbor() {
var address = RadixAddress.from("9S8khLHZa6FsyGo634xQo9QwLgSHGpXHHW764D5mPYBcrnfZV6RT");
var rri = RRI.of(address, "FOOBAR");
var permissions = ImmutableMap.of(
MutableSupplyTokenDefinitionParticle.TokenTransition.BURN, TokenPermission.ALL,
MutableSupplyTokenDefinitionParticle.TokenTransition.MINT, TokenPermission.ALL
);
var amount = UInt256.SIX;
var granularity = UInt256.THREE;
var nonce = 1337L;
var uatp = new UnallocatedTokensParticle(amount, granularity, nonce, rri, permissions);
var dsonBytes = uatp.toDson();
var actual = Bytes.toHexString(dsonBytes);
var expected = "bf66616d6f756e7458210500000000000000000000000000000000000000000000000000000000000000066b6772616e756c61726974795821050000000000000000000000000000000000000000000000000000000000000003656e6f6e63651905396b7065726d697373696f6e73bf646275726e63616c6c646d696e7463616c6cff6a73657269616c697a6572782272616469782e7061727469636c65732e756e616c6c6f63617465645f746f6b656e737818746f6b656e446566696e6974696f6e5265666572656e6365583d062f3953386b684c485a6136467379476f36333478516f3951774c67534847705848485737363444356d50594263726e665a563652542f464f4f4241526776657273696f6e1864ff";
assertEquals(expected, actual);
}
@Test
public void test_rip_cbor() {
var address = RadixAddress.from("9S8khLHZa6FsyGo634xQo9QwLgSHGpXHHW764D5mPYBcrnfZV6RT");
var rri = RRI.of(address, "FOOBAR");
// var nonce = 1337L;
var rip = new RRIParticle(rri); //, nonce);
var dsonBytes = rip.toDson();
var actual = Bytes.toHexString(dsonBytes);
var expected = "bf656e6f6e63650063727269583d062f3953386b684c485a6136467379476f36333478516f3951774c67534847705848485737363444356d50594263726e665a563652542f464f4f4241526a73657269616c697a65727372616469782e7061727469636c65732e7272696776657273696f6e1864ff";
assertEquals(expected, actual);
}
@Test
public void test_fixed_sup_def_part() {
var address = RadixAddress.from("9S8khLHZa6FsyGo634xQo9QwLgSHGpXHHW764D5mPYBcrnfZV6RT");
var fixedSupTokDefPart = new FixedSupplyTokenDefinitionParticle(address, "Foobar Coin", "ABCD0123456789", "Best coin ever", UInt256.TEN, UInt256.ONE, "https://foobar.com/icon.png", "https://foobar.com");
assertEquals("a",fixedSupTokDefPart.getSupply().toString(16));
assertEquals("1",fixedSupTokDefPart.getGranularity().toString(16));
var dsonBytes = fixedSupTokDefPart.toDson();
var actual = Bytes.toHexString(dsonBytes);
var expected = "bf6b6465736372697074696f6e6e4265737420636f696e20657665726b6772616e756c617269747958210500000000000000000000000000000000000000000000000000000000000000016769636f6e55726c781b68747470733a2f2f666f6f6261722e636f6d2f69636f6e2e706e67646e616d656b466f6f62617220436f696e637272695845062f3953386b684c485a6136467379476f36333478516f3951774c67534847705848485737363444356d50594263726e665a563652542f41424344303132333435363738396a73657269616c697a6572782d72616469782e7061727469636c65732e66697865645f737570706c795f746f6b656e5f646566696e6974696f6e66737570706c79582105000000000000000000000000000000000000000000000000000000000000000a6375726c7268747470733a2f2f666f6f6261722e636f6d6776657273696f6e1864ff";
assertEquals(expected, actual);
}
@Test
public void test_mutable_sup_def_part() {
var address = RadixAddress.from("9S8khLHZa6FsyGo634xQo9QwLgSHGpXHHW764D5mPYBcrnfZV6RT");
var permissionsALL = ImmutableMap.of(
MutableSupplyTokenDefinitionParticle.TokenTransition.BURN, TokenPermission.ALL,
MutableSupplyTokenDefinitionParticle.TokenTransition.MINT, TokenPermission.ALL
);
var mutableSupplyTokenDefinitionParticle = new MutableSupplyTokenDefinitionParticle(address, "Foobar Coin", "ABCD0123456789", "Best coin ever", UInt256.ONE, permissionsALL, "https://foobar.com/icon.png", "https://foobar.com");
var dsonBytes = mutableSupplyTokenDefinitionParticle.toDson();
var actual = Bytes.toHexString(dsonBytes);
var expected = "bf6b6465736372697074696f6e6e4265737420636f696e20657665726b6772616e756c617269747958210500000000000000000000000000000000000000000000000000000000000000016769636f6e55726c781b68747470733a2f2f666f6f6261722e636f6d2f69636f6e2e706e67646e616d656b466f6f62617220436f696e6b7065726d697373696f6e73bf646275726e63616c6c646d696e7463616c6cff637272695845062f3953386b684c485a6136467379476f36333478516f3951774c67534847705848485737363444356d50594263726e665a563652542f41424344303132333435363738396a73657269616c697a6572782f72616469782e7061727469636c65732e6d757461626c655f737570706c795f746f6b656e5f646566696e6974696f6e6375726c7268747470733a2f2f666f6f6261722e636f6d6776657273696f6e1864ff";
assertEquals(expected, actual);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment