use of org.xrpl.xrpl4j.codec.addresses.UnsignedByte in project xrpl4j by XRPLF.
the class Ed25519KeyPairService method deriveKeyPair.
private KeyPair deriveKeyPair(UnsignedByteArray seed) {
UnsignedByteArray rawPrivateKey = HashUtils.sha512Half(seed);
Ed25519PrivateKeyParameters privateKey = new Ed25519PrivateKeyParameters(rawPrivateKey.toByteArray(), 0);
Ed25519PublicKeyParameters publicKey = privateKey.generatePublicKey();
// XRPL ED25519 keys are prefixed with 0xED so that the keys are 33 bytes and match the length of sekp256k1 keys.
// Bouncy Castle only deals with 32 byte keys, so we need to manually add the prefix
UnsignedByte prefix = UnsignedByte.of(0xED);
UnsignedByteArray prefixedPrivateKey = UnsignedByteArray.of(prefix).append(UnsignedByteArray.of(privateKey.getEncoded()));
UnsignedByteArray prefixedPublicKey = UnsignedByteArray.of(prefix).append(UnsignedByteArray.of(publicKey.getEncoded()));
return KeyPair.builder().privateKey(prefixedPrivateKey.hexValue()).publicKey(prefixedPublicKey.hexValue()).build();
}
use of org.xrpl.xrpl4j.codec.addresses.UnsignedByte in project xrpl4j by XRPLF.
the class FieldHeaderCodec method decodeFieldId.
protected FieldHeader decodeFieldId(String hex) {
Preconditions.checkNotNull(hex, "hex cannot be null");
Preconditions.checkArgument(hex.length() >= 2, "hex must be at least 2 characters");
List<UnsignedByte> segments = ByteUtils.parse(hex);
Preconditions.checkArgument(segments.size() <= 3, "hex value is too large");
if (segments.size() == 1) {
UnsignedByte first = segments.get(0);
return FieldHeader.builder().typeCode(first.getHighBits()).fieldCode(first.getLowBits()).build();
}
if (segments.size() == 2) {
UnsignedByte first = segments.get(0);
UnsignedByte second = segments.get(1);
if (first.getHighBits() == 0) {
return FieldHeader.builder().fieldCode(first.getLowBits()).typeCode(second.asInt()).build();
} else {
return FieldHeader.builder().typeCode(first.getHighBits()).fieldCode(second.asInt()).build();
}
}
return FieldHeader.builder().typeCode(segments.get(1).asInt()).fieldCode(segments.get(2).asInt()).build();
}
use of org.xrpl.xrpl4j.codec.addresses.UnsignedByte in project xrpl4j by XRPLF.
the class AmountType method toJson.
@Override
public JsonNode toJson() {
if (this.isNative()) {
byte[] rawBytes = toBytes();
rawBytes[0] &= 0x3f;
BigInteger value = new BigInteger(rawBytes);
if (!this.isPositive()) {
value = value.negate();
}
return new TextNode(value.toString());
} else {
BinaryParser parser = new BinaryParser(this.toHex());
UnsignedByteArray mantissa = parser.read(8);
final SerializedType<?> currency = new CurrencyType().fromParser(parser);
final SerializedType<?> issuer = new AccountIdType().fromParser(parser);
UnsignedByte b1 = mantissa.get(0);
UnsignedByte b2 = mantissa.get(1);
boolean isPositive = b1.isNthBitSet(2);
String sign = isPositive ? "" : "-";
int exponent = ((b1.asInt() & 0x3f) << 2) + ((b2.asInt() & 0xff) >> 6) - 97;
mantissa.set(0, UnsignedByte.of(0));
mantissa.set(1, UnsignedByte.of(b2.asInt() & 0x3f));
BigDecimal value = new BigDecimal(new BigInteger(sign + mantissa.hexValue(), 16)).multiply(new BigDecimal("1e" + exponent)).stripTrailingZeros();
assertIouIsValid(value);
Amount amount = Amount.builder().currency(currency.toJson().asText()).issuer(issuer.toJson().asText()).value(value.toPlainString()).build();
return objectMapper.valueToTree(amount);
}
}
use of org.xrpl.xrpl4j.codec.addresses.UnsignedByte in project xrpl4j by XRPLF.
the class AmountType method getAmountBytes.
private UnsignedByteArray getAmountBytes(BigDecimal number) {
BigInteger paddedNumber = MathUtils.toPaddedBigInteger(number, 16);
byte[] amountBytes = ByteUtils.toByteArray(paddedNumber, 8);
amountBytes[0] |= 0x80;
if (number.compareTo(BigDecimal.ZERO) > 0) {
amountBytes[0] |= 0x40;
}
int exponent = MathUtils.getExponent(number);
if (exponent > 80 || exponent < -96) {
throw new IllegalArgumentException("exponent out of range");
}
UnsignedByte exponentByte = UnsignedByte.of(97 + exponent - 15);
amountBytes[0] |= exponentByte.asInt() >>> 2;
amountBytes[1] |= (exponentByte.asInt() & 0x03) << 6;
return UnsignedByteArray.of(amountBytes);
}
Aggregations