@Override protected void parse() throws ProtocolException { nSporkID = (int)readUint32(); nValue = readInt64(); nTimeSigned = readInt64(); sig = new MasternodeSignature(params, payload, cursor); cursor += sig.getMessageSize(); length = cursor - offset; }
@Override protected void bitcoinSerializeToStream(OutputStream stream) throws IOException { vin.bitcoinSerialize(stream); stream.write(blockHash.getReversedBytes()); int64ToByteStreamLE(sigTime, stream); vchSig.bitcoinSerialize(stream); }
public boolean equals(Object o) { MasternodePing mnp = (MasternodePing)o; if(mnp.sigTime == this.sigTime && mnp.vin.equals(this.vin) && mnp.vchSig.equals(this.vchSig) && mnp.blockHash.equals(this.blockHash)) { return true; } return false; }
MasternodeSignature duplicate() { MasternodeSignature copy = new MasternodeSignature(params, getBytes(), 0); return copy; }
static public PublicKey recoverCompact(Sha256Hash hash, MasternodeSignature sig) throws SignatureException { if(sig.getBytes().length != 65) throw new SignatureException("signature is wrong size"); int recid = (sig.getBytes()[0] - 27) & 3; boolean comp = ((sig.getBytes()[0] - 27) & 4) != 0; //ECKey.ECDSASignature esig = ECKey.ECDSASignature.decodeFromDER(sig.getBytes()); BigInteger r = new BigInteger(1, Arrays.copyOfRange(sig.getBytes(), 1, 33)); BigInteger s = new BigInteger(1, Arrays.copyOfRange(sig.getBytes(), 33, 65)); ECKey.ECDSASignature esig = new ECKey.ECDSASignature(r, s); ECKey ecKey = ECKey.recoverFromSignature(recid, esig, hash, comp); return new PublicKey (ecKey.getPubKey()); }
public int calculateMessageSizeInBytes() { int cursor = 0; //vin cursor += 36; long scriptLen = vin.getScriptBytes().length; // 4 = length of sequence field (unint32) cursor += scriptLen + 4 + VarInt.sizeOf(scriptLen); //blockHash cursor += 32; //sigTime cursor += 8; //vchSig cursor += vchSig.calculateMessageSizeInBytes(); return cursor; }
protected static int calcLength(byte[] buf, int offset) { VarInt varint; int cursor = offset; //vin cursor += 36; varint = new VarInt(buf, cursor); long scriptLen = varint.value; // 4 = length of sequence field (unint32) cursor += scriptLen + 4 + varint.getOriginalSizeInBytes(); //blockHash cursor += 32; //sigTime cursor += 8; //vchSig cursor = MasternodeSignature.calcLength(buf, cursor); return cursor - offset; }
public static MasternodeSignature signHash(Sha256Hash hash, ECKey key) { ECKey.ECDSASignature signature = key.sign(hash); return new MasternodeSignature(signature.encodeToDER()); }
@Override protected void parse() throws ProtocolException { cursor = offset; bytes = readByteArray(); length = cursor - offset; } @Override
public Masternode(Masternode other) { super(other.params); this.context = other.context; //LOCK(cs); this.vin = other.vin; //TODO: need to make copies of all these? this.address = new MasternodeAddress(other.address.getAddr(), other.address.getPort()); this.pubKeyCollateralAddress = other.pubKeyCollateralAddress.duplicate(); this.pubKeyMasternode = other.pubKeyMasternode.duplicate(); //These are good this.sig = other.sig.duplicate(); this.activeState = other.activeState; this.sigTime = other.sigTime; this.cacheInputAge = other.cacheInputAge; this.cacheInputAgeBlock = other.cacheInputAgeBlock; this.unitTest = other.unitTest; this.allowFreeTx = other.allowFreeTx; this.protocolVersion = other.protocolVersion; this.nLastDsq = other.nLastDsq; this.nScanningErrorCount = other.nScanningErrorCount; this.nLastScanningErrorBlockHeight = other.nLastScanningErrorBlockHeight; }
public static boolean verifyMessage(PublicKey pubkey, MasternodeSignature vchSig, String strMessage, StringBuilder errorMessage) { //int length = Utils.BITCOIN_SIGNED_MESSAGE_HEADER.length()+strMessage.length(); //byte dataToHash [] = (Utils.BITCOIN_SIGNED_MESSAGE_HEADER_BYTES+strMessage).getBytes(); ECKey pubkey2 = null; try { //pubkey2 = PublicKey.recoverCompact(Sha256Hash.twiceOf(dataToHash), vchSig); pubkey2 = ECKey.fromPublicOnly(pubkey.getBytes()); pubkey2.verifyMessage(strMessage.getBytes(), vchSig.getBytes()); //ECKey.verify() //if(DarkCoinSystem.fDebug && !pubkey.getId().equals(pubkey2.getId())) // log.info("DarkSendSigner.verifyMessage -- keys don't match: " + pubkey2.getId().toString()+ " " + pubkey.getId().toString()); //return pubkey.getId().equals(pubkey2.getId()); return true; } catch(SignatureException x) { errorMessage.append("keys don't match - input: "+Utils.HEX.encode(pubkey.getId())); errorMessage.append(", recovered: " + (pubkey2 != null ? Utils.HEX.encode(pubkey2.getPubKeyHash()) : "null")); errorMessage.append(",\nmessage: "+ String.valueOf(strMessage)); errorMessage.append(", sig: \n" + Base64.toBase64String(vchSig.getBytes())+ "\n" + x.getMessage()); return false; } } public static boolean verifyMessage1(PublicKey pubkey, MasternodeSignature vchSig, byte[] message, StringBuilder errorMessage)
public Sha256Hash getHash(){ try { UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(vchSig.calculateMessageSizeInBytes()+8); vin.bitcoinSerialize(bos); Utils.int64ToByteStreamLE(sigTime, bos); return Sha256Hash.wrapReversed(Sha256Hash.hashTwice(bos.toByteArray())); } catch (IOException e) { throw new RuntimeException(e); // Cannot happen. } } }
protected static int calcLength(byte[] buf, int offset) { VarInt varint; int cursor = offset; //vin cursor += 36; varint = new VarInt(buf, cursor); long scriptLen = varint.value; // 4 = length of sequence field (unint32) cursor += scriptLen + 4 + varint.getOriginalSizeInBytes(); //MasternodeAddress address; cursor += MasternodeAddress.MESSAGE_SIZE; //PublicKey pubkey; cursor += PublicKey.calcLength(buf, cursor); //PublicKey pubkey2; cursor += PublicKey.calcLength(buf, cursor); // byte [] sig; cursor += MasternodeSignature.calcLength(buf, cursor); cursor += 4 + 8 + 8; cursor += MasternodeSignature.calcLength(buf, cursor); return cursor - offset; }
public static MasternodeSignature signMessage(String strMessage, StringBuilder errorMessage, ECKey key) { //ECKey ecKey = ECKey.fromPublicOnly(key.getBytes()); try { byte dataToHash [] = (Utils.BITCOIN_SIGNED_MESSAGE_HEADER_BYTES+strMessage).getBytes(Charsets.UTF_8); ECKey.ECDSASignature signature = key.sign(Sha256Hash.twiceOf(dataToHash)); return new MasternodeSignature(signature.encodeToDER()); } catch (KeyCrypterException x) { } errorMessage.append("Sign failed"); return null; } public static boolean verifyMessage(PublicKey pubkey, MasternodeSignature vchSig, String strMessage, StringBuilder errorMessage)
@Override protected void parse() throws ProtocolException { vin = new TransactionInput(params, null, payload, cursor); cursor += vin.getMessageSize(); blockHash = readHash(); sigTime = readInt64(); vchSig = new MasternodeSignature(params, payload, cursor); cursor += vchSig.getMessageSize(); length = cursor - offset; }
public static boolean verifyHash(Sha256Hash hash, byte [] pubkeyId, MasternodeSignature vchSig, StringBuilder strErrorRet) { ECKey pubkeyFromSig; try { pubkeyFromSig = ECKey.signedMessageToKey(hash, vchSig.getBytes()); if (pubkeyFromSig == null) { strErrorRet.append("Error recovering public key."); return false; } if (!Arrays.equals(pubkeyFromSig.getPubKeyHash(), pubkeyId)) { strErrorRet.append(String.format("Keys don't match: pubkey=%s, pubkeyFromSig=%s, hash=%s, vchSig=%s", HEX.encode(pubkeyId), HEX.encode(pubkeyFromSig.getPubKeyHash()), hash.toString(), Base64.toBase64String(vchSig.getBytes()))); return false; } return true; } catch (SignatureException x) { strErrorRet.append("exception: " + x.getMessage()); return false; } }
@Override protected void bitcoinSerializeToStream(OutputStream stream) throws IOException { stream.write(txHash.getBytes()); //writes 32 outpoint.bitcoinSerialize(stream); outpointMasternode.bitcoinSerialize(stream); stream.write(new VarInt(vchMasternodeSignature.length).encode()); vchMasternodeSignature.bitcoinSerialize(stream); }
cursor += sig.calculateMessageSizeInBytes(); //calcLength(buf, cursor);
cursor += MasternodeSignature.calcLength(buf, cursor);
@Override protected void parse() throws ProtocolException { cursor = offset; txHash = readHash(); outpoint = new TransactionOutPoint(params, payload, cursor); cursor += outpoint.getMessageSize(); outpointMasternode = new TransactionOutPoint(params, payload, cursor); cursor+= outpointMasternode.getMessageSize(); vchMasternodeSignature = new MasternodeSignature(params, payload, cursor); cursor += vchMasternodeSignature.getMessageSize(); confirmedHeight = -1; timeCreated = Utils.currentTimeSeconds(); length = cursor - offset; } @Override