SignatureSpec { public static Signature serialize(final ECDSASignature ecdsaSignature) { assertNotNull(ecdsaSignature, "ECDSASignature must not null"); final BigInteger order = ecParamsN; final BigInteger halfOrder = order.divide(BigInteger.valueOf(2L)); final BigInteger r = ecdsaSignature.getR(); BigInteger s = ecdsaSignature.getS(); if (s.compareTo(halfOrder) > 0) { s = order.subtract(s); } final byte[] rbyteArray = r.toByteArray(); final byte[] sbyteArray = s.toByteArray(); if (logger.isTraceEnabled()) { logger.trace("Canonical r: {}, len: {}", HexUtils.encode(rbyteArray), rbyteArray.length); logger.trace("Canonical s: {}, len: {}", HexUtils.encode(sbyteArray), sbyteArray.length); } final byte[] serialized = new byte[6 + rbyteArray.length + sbyteArray.length]; serialized[0] = SIGN_HEADER_MAGIC; serialized[1] = (byte) (serialized.length - 2); serialized[2] = SIGN_INT_MARKER; serialized[3] = (byte) rbyteArray.length; System.arraycopy(rbyteArray, 0, serialized, 4, rbyteArray.length); final int offset = 4 + rbyteArray.length; serialized[offset] = SIGN_INT_MARKER; serialized[offset + 1] = (byte) sbyteArray.length; System.arraycopy(sbyteArray, 0, serialized, offset + 2, sbyteArray.length); return Signature.newBuilder().sign(BytesValue.of(serialized)).build(); }  static Signature serialize(final ECDSASignature ecdsaSignature); static ECDSASignature deserialize(final Signature signature); static final int SIGN_HEADER_MAGIC; static final int SIGN_INT_MARKER; static final int SIGN_MINIMUM_LENGTH; static final BigInteger ecParamsN; }
@Test public void testSerialize() throws Exception { final ECDSASignature ecdsaSignature = ECDSASignature.of( new BigInteger( "77742016982977049819968937189730099006007209897399569418319639670259283246582"), new BigInteger( "24080111729304174841921585755879357193051484773881703660717104599905026449822")); final Signature signature = SignatureSpec.serialize(ecdsaSignature); assertTrue(Arrays.equals(HexUtils.decode( "3045022100ABE06C1B99DE0C51B4790D24EE52674F532D9057744ED9EEF3F61425F9D1BDF60220353CDC395B12ABB6E297085B4D6F1A9DF7783DB66F95A7E0CE28246FC538219E"), signature.getSign().getValue())); }