ui/shared/src/main/java/com/squareup/subzero/shared/SubzeroUtils.java
protected static List<byte[]> validateAndSort(List<ECKey> pubkeys, byte[] hash, Signature sig1,
      Signature sig2) {
    // This needs to be the same sort that ScriptBuilder.createRedeemScript does.
    pubkeys.sort(ECKey.PUBKEY_COMPARATOR);
    List<byte[]> sortedSigs = new ArrayList<>();

    // If this check fails, we've probably got invalid signatures that would fail below, or when
    // broadcast.  However, this lets us distinguish between invalid signatures and signatures over
    // the wrong data.  Useful primarily for debugging when making changes to either piece of code.
    if(sig1.hasHash()) {
      if(!Arrays.equals(sig1.getHash().toByteArray(), hash)) {
        throw new RuntimeException(format(
            "Our calculated hash does not match the HSM provided sig1: %s != %s",
            Hex.toHexString(sig1.getHash().toByteArray()), Hex.toHexString(hash)));
      }
    }

    if(sig2.hasHash()) {
      if(!Arrays.equals(sig2.getHash().toByteArray(), hash)) {
        throw new RuntimeException(format(
            "Our calculated hash does not match the HSM provided sig2: %s != %s",
            Hex.toHexString(sig2.getHash().toByteArray()), Hex.toHexString(hash)));
      }
    }

    // Iterate over pubkeys, and find signatures valid for them.
    // We should expect exactly two verifications to succeed
    for(ECKey pubkey: pubkeys) {
      if(pubkey.verify(hash, sig1.getDer().toByteArray())) {
        sortedSigs.add(sig1.getDer().toByteArray());
      } else if(pubkey.verify(hash, sig2.getDer().toByteArray())) {
        sortedSigs.add(sig2.getDer().toByteArray());
      }
    }

    // Possible cases: Two of the same signature, or invalid signatures.
    if(sortedSigs.size() != 2) {
      throw new RuntimeException(format("Failed validating signatures. Expected 2, got %d", sortedSigs.size()));
    }

    return sortedSigs;
  }
// The hash is the value the signatures are over.
The hash is the value the signatures are over.
The hash is the value the signatures are over .
protected static List<byte[]> validateAndSort(List<ECKey> pubkeys, byte[] hash, Signature sig1,
      Signature sig2) {
    pubkeys.sort(ECKey.PUBKEY_COMPARATOR);
    List<byte[]> sortedSigs = new ArrayList<>();
    if(sig1.hasHash()) {
      if(!Arrays.equals(sig1.getHash().toByteArray(), hash)) {
        throw new RuntimeException(format(
            "Our calculated hash does not match the HSM provided sig1: %s != %s",
            Hex.toHexString(sig1.getHash().toByteArray()), Hex.toHexString(hash)));
      }
    }
    if(sig2.hasHash()) {
      if(!Arrays.equals(sig2.getHash().toByteArray(), hash)) {
        throw new RuntimeException(format(
            "Our calculated hash does not match the HSM provided sig2: %s != %s",
            Hex.toHexString(sig2.getHash().toByteArray()), Hex.toHexString(hash)));
      }
    }
    for(ECKey pubkey: pubkeys) {
      if(pubkey.verify(hash, sig1.getDer().toByteArray())) {
        sortedSigs.add(sig1.getDer().toByteArray());
      } else if(pubkey.verify(hash, sig2.getDer().toByteArray())) {
        sortedSigs.add(sig2.getDer().toByteArray());
      }
    }
    if(sortedSigs.size() != 2) {
      throw new RuntimeException(format("Failed validating signatures. Expected 2, got %d", sortedSigs.size()));
    }
    return sortedSigs;
  }
protected static List < byte [ ] > validateAndSort ( List < ECKey > pubkeys , byte [ ] hash , Signature sig1 , Signature sig2 ) { pubkeys . sort ( ECKey . PUBKEY_COMPARATOR ) ; List < byte [ ] > sortedSigs = new ArrayList < > ( ) ; if ( sig1 . hasHash ( ) ) { if ( ! Arrays . equals ( sig1 . getHash ( ) . toByteArray ( ) , hash ) ) { throw new RuntimeException ( format ( "Our calculated hash does not match the HSM provided sig1: %s != %s" , Hex . toHexString ( sig1 . getHash ( ) . toByteArray ( ) ) , Hex . toHexString ( hash ) ) ) ; } } if ( sig2 . hasHash ( ) ) { if ( ! Arrays . equals ( sig2 . getHash ( ) . toByteArray ( ) , hash ) ) { throw new RuntimeException ( format ( "Our calculated hash does not match the HSM provided sig2: %s != %s" , Hex . toHexString ( sig2 . getHash ( ) . toByteArray ( ) ) , Hex . toHexString ( hash ) ) ) ; } } for ( ECKey pubkey : pubkeys ) { if ( pubkey . verify ( hash , sig1 . getDer ( ) . toByteArray ( ) ) ) { sortedSigs . add ( sig1 . getDer ( ) . toByteArray ( ) ) ; } else if ( pubkey . verify ( hash , sig2 . getDer ( ) . toByteArray ( ) ) ) { sortedSigs . add ( sig2 . getDer ( ) . toByteArray ( ) ) ; } } if ( sortedSigs . size ( ) != 2 ) { throw new RuntimeException ( format ( "Failed validating signatures. Expected 2, got %d" , sortedSigs . size ( ) ) ) ; } return sortedSigs ; }
The hash is the value the signatures are over.
The hash is the value the signatures are over .
// This needs to be the same sort that ScriptBuilder.createRedeemScript does. // If this check fails, we've probably got invalid signatures that would fail below, or when // broadcast.  However, this lets us distinguish between invalid signatures and signatures over // the wrong data.  Useful primarily for debugging when making changes to either piece of code. // Iterate over pubkeys, and find signatures valid for them. // We should expect exactly two verifications to succeed // Possible cases: Two of the same signature, or invalid signatures.