|
| 1 | +package com.bloxbean.cardano.client.address; |
| 2 | + |
| 3 | +import com.bloxbean.cardano.client.exception.AddressRuntimeException; |
| 4 | +import com.bloxbean.cardano.client.util.Tuple; |
| 5 | + |
| 6 | +import java.util.Arrays; |
| 7 | + |
| 8 | +/** |
| 9 | + * PointerAddress class represents Shelley Pointer address. This class is useful to decode Pointer address and get the Pointer |
| 10 | + */ |
| 11 | +public class PointerAddress extends Address { |
| 12 | + private Pointer pointer; |
| 13 | + |
| 14 | + public PointerAddress(String prefix, byte[] bytes) { |
| 15 | + super(prefix, bytes); |
| 16 | + |
| 17 | + if (getAddressType() != AddressType.Ptr) |
| 18 | + throw new AddressRuntimeException("Invalid address type. Expected Pointer address type"); |
| 19 | + |
| 20 | + decodePointer(); |
| 21 | + } |
| 22 | + |
| 23 | + public PointerAddress(String address) { |
| 24 | + super(address); |
| 25 | + |
| 26 | + if (getAddressType() != AddressType.Ptr) |
| 27 | + throw new AddressRuntimeException("Invalid address type. Expected Pointer address type"); |
| 28 | + |
| 29 | + decodePointer(); |
| 30 | + } |
| 31 | + |
| 32 | + public PointerAddress(byte[] addressBytes) { |
| 33 | + super(addressBytes); |
| 34 | + |
| 35 | + if (getAddressType() != AddressType.Ptr) |
| 36 | + throw new AddressRuntimeException("Invalid address type. Expected Pointer address type"); |
| 37 | + |
| 38 | + decodePointer(); |
| 39 | + } |
| 40 | + |
| 41 | + private void decodePointer() { |
| 42 | + byte[] pointerBytes = getDelegationCredentialHash() |
| 43 | + .orElseThrow(() -> new AddressRuntimeException("Delegation credential hash not found")); |
| 44 | + |
| 45 | + int index = 0; |
| 46 | + Tuple<Long, Integer> slot = variableNatDecode(pointerBytes); |
| 47 | + index += slot._2; |
| 48 | + Tuple<Long, Integer> txIndex = variableNatDecode(getSubBytes(pointerBytes, index)); |
| 49 | + index += txIndex._2; |
| 50 | + Tuple<Long, Integer> certIndex = variableNatDecode(getSubBytes(pointerBytes, index)); |
| 51 | + |
| 52 | + pointer = new Pointer(slot._1, txIndex._1.intValue(), certIndex._1.intValue()); |
| 53 | + } |
| 54 | + |
| 55 | + private Tuple<Long, Integer> variableNatDecode(byte[] raw) { |
| 56 | + long output = 0; |
| 57 | + int bytesRead = 0; |
| 58 | + |
| 59 | + for (byte rbyte : raw) { |
| 60 | + output = (output << 7) | (rbyte & 0x7F); |
| 61 | + bytesRead++; |
| 62 | + |
| 63 | + if ((rbyte & 0x80) == 0) { |
| 64 | + return new Tuple<>(output, bytesRead); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + throw new IllegalArgumentException("Invalid variable nat encoding. Unexpected bytes"); |
| 69 | + } |
| 70 | + |
| 71 | + // Get subarray from startPosition to the end of the array |
| 72 | + private byte[] getSubBytes(byte[] array, int startPosition) { |
| 73 | + // Validate startPosition |
| 74 | + if (startPosition < 0 || startPosition > array.length) { |
| 75 | + throw new IllegalArgumentException("Invalid start position"); |
| 76 | + } |
| 77 | + |
| 78 | + return Arrays.copyOfRange(array, startPosition, array.length); |
| 79 | + } |
| 80 | + |
| 81 | + public Pointer getPointer() { |
| 82 | + return pointer; |
| 83 | + } |
| 84 | +} |
0 commit comments