public Sha256Hash getHash() { byte [] dataToHash = new byte[pubKeyCollateralAddress.getBytes().length+8]; Utils.uint32ToByteArrayLE(sigTime, dataToHash, 0); System.arraycopy(pubKeyCollateralAddress.getBytes(), 0, dataToHash, 8, pubKeyCollateralAddress.getBytes().length); try { UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(8 + vin.getMessageSize() + pubKeyCollateralAddress.calculateMessageSizeInBytes()); vin.bitcoinSerialize(bos); Utils.int64ToByteStreamLE(sigTime, bos); pubKeyCollateralAddress.bitcoinSerialize(bos); return Sha256Hash.wrapReversed(Sha256Hash.hashTwice((bos.toByteArray()))); } catch (IOException e) { throw new RuntimeException(e); // Cannot happen. } }
Masternode find(PublicKey pubKeyMasternode) { lock.lock(); try { //BOOST_FOREACH(CMasternode & mn, vMasternodes) for (Masternode mn : vMasternodes) { if (mn.pubKeyMasternode.equals(pubKeyMasternode)) return mn; } return null; } finally { lock.unlock(); } } public boolean has(TransactionOutPoint outpoint)
@Override protected void parse() throws ProtocolException { vin = new TransactionInput(params, null, payload, cursor); cursor += vin.getMessageSize(); address = new MasternodeAddress(params, payload, cursor, 0); cursor += address.getMessageSize(); pubKeyCollateralAddress = new PublicKey(params, payload, cursor); cursor += pubKeyCollateralAddress.getMessageSize(); pubKeyMasternode = new PublicKey(params, payload, cursor); cursor += pubKeyMasternode.getMessageSize(); sig = new MasternodeSignature(params, payload, cursor); cursor += sig.getMessageSize(); sigTime = readInt64(); protocolVersion = (int)readUint32(); lastPing = new MasternodePing(params, payload, cursor); cursor += lastPing.getMessageSize(); //nLastDsq = readInt64(); length = cursor - offset; }
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)
UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream((address.toString() + sigTime).length() + pubKeyCollateralAddress.getBytes().length + pubKeyMasternode.getBytes().length + ((Integer) protocolVersion).toString().getBytes().length); bos.write((address.toString() + sigTime).getBytes()); bos.write(pubKeyCollateralAddress.getBytes()); bos.write(pubKeyMasternode.getBytes()); bos.write(((Integer) protocolVersion).toString().getBytes()); strMessage = address.toString() + sigTime + Utils.HEX.encode(Utils.reverseBytes(pubKeyCollateralAddress.getId())) + Utils.HEX.encode(Utils.reverseBytes(pubKeyMasternode.getId())) + protocolVersion; message = strMessage.getBytes(); log.info("CMasternodeBroadcast::VerifySignature - sanitized strMessage: "+Utils.sanitizeString(strMessage)+", pubKeyCollateralAddress address: "+new Address(params, pubKeyCollateralAddress.getId()).toString()+", sig: %s\n" + Base64.toBase64String(sig.getBytes())); pubkeyScript = ScriptBuilder.createOutputScript(new Address(params, pubKeyCollateralAddress.getId())); pubkeyScript2 = ScriptBuilder.createOutputScript(new Address(params, pubKeyMasternode.getId())); if (pmn.pubKeyCollateralAddress.equals(pubKeyCollateralAddress) && !pmn.isBroadcastedWithin(MASTERNODE_MIN_MNB_SECONDS)) {
pubkeyScript = ScriptBuilder.createOutputScript(new Address(params, pubKeyCollateralAddress.getId())); pubkeyScript2 = ScriptBuilder.createOutputScript(new Address(params, pubKeyMasternode.getId())); if (pmn.pubKeyCollateralAddress.equals(pubKeyCollateralAddress) && !pmn.isBroadcastedWithin(MASTERNODE_MIN_MNB_SECONDS)) {
@Override protected void bitcoinSerializeToStream(OutputStream stream) throws IOException { vin.bitcoinSerialize(stream); pubkey.bitcoinSerialize(stream); pubkey2.bitcoinSerialize(stream); stream.write(new VarInt(vchSig.length).encode()); stream.write(vchSig); Utils.int64ToByteStreamLE(sigTime, stream); Utils.uint32ToByteStreamLE(count, stream); Utils.uint32ToByteStreamLE(current, stream); Utils.int64ToByteStreamLE(lastUpdated, stream); Utils.uint32ToByteStreamLE(protocolVersion, stream); }
public Masternode(Context context) { super(context.getParams()); this.context = context; vin = null; address = null; pubKeyCollateralAddress = new PublicKey(); pubKeyMasternode = new PublicKey(); sig = null; activeState = State.MASTERNODE_ENABLED; sigTime = Utils.currentTimeSeconds(); lastPing = MasternodePing.EMPTY; cacheInputAge = 0; cacheInputAgeBlock = 0; unitTest = false; allowFreeTx = true; protocolVersion = CoinDefinition.PROTOCOL_VERSION; nLastDsq = 0; nScanningErrorCount = 0; nLastScanningErrorBlockHeight = 0; lastTimeChecked = 0; }
public byte [] getId() { return getECKey().getPubKeyHash(); }
public static boolean verifyMessage(PublicKey pubkey, MasternodeSignature vchSig, String message, StringBuilder errorMessage) { return MessageSigner.verifyMessage(pubkey.getId(), vchSig, message, errorMessage); }
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 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; }
cursor += pubKeyCollateralAddress.calculateMessageSizeInBytes(); cursor += pubKeyMasternode.calculateMessageSizeInBytes();
public static boolean isVinAssociatedWithPubkey(NetworkParameters params, TransactionInput vin, PublicKey pubkey) { //TODO: This function requires the blockchain! we don't have it Script payee2 = ScriptBuilder.createOutputScript(new Address(params, ECKey.fromPublicOnly(pubkey.getBytes()).getPubKeyHash())); //payee2.SetDestination(pubkey.GetID()); Transaction txVin; Sha256Hash hash; /* if (GetTransaction(vin.prevout.hash, txVin, hash, true)) { for(TransactionOutput out : txVin.vout) { if (out.getValue() == Coin.valueOf(1000)) { if (out.getScriptPubKey() == payee2) return true; } } }*/ return true; //we will assume this is true, we cannot check it. } public static ECKey setKey(String strSecret, StringBuilder errorMessage)
@Override protected void parse() throws ProtocolException { cursor = offset; optimalEncodingMessageSize = 0; TransactionOutPoint outpoint = new TransactionOutPoint(params, payload, cursor); cursor += outpoint.getMessageSize(); int scriptLen = (int) readVarInt(); byte [] scriptBytes = readBytes(scriptLen); long sequence = readUint32(); vin = new TransactionInput(params, null, scriptBytes, outpoint); optimalEncodingMessageSize += outpoint.getMessageSize() + scriptLen + VarInt.sizeOf(scriptLen) +4; pubkey = new PublicKey(params, payload, cursor); cursor += pubkey.getMessageSize(); pubkey2 = new PublicKey(params, payload, cursor); cursor += pubkey.getMessageSize(); vchSig = readByteArray(); sigTime = readInt64(); count = (int)readUint32(); current = (int)readUint32(); lastUpdated = readInt64(); protocolVersion = (int)readUint32(); length = cursor - offset; } @Override
public static boolean verifyMessage1(PublicKey pubkey, MasternodeSignature vchSig, byte[] message, StringBuilder errorMessage) { //int length = Utils.BITCOIN_SIGNED_MESSAGE_HEADER.length()+strMessage.length(); byte dataToHash []; // = (Utils.BITCOIN_SIGNED_MESSAGE_HEADER_BYTES+strMessage).getBytes(); //ByteOutputStream bos = new ByteOutputStream(message.length + Utils.BITCOIN_SIGNED_MESSAGE_HEADER_BYTES.length); //bos.write(Utils.BITCOIN_SIGNED_MESSAGE_HEADER_BYTES); //bos.write(message); dataToHash = Utils.formatMessageForSigning(message);//bos.getBytes(); //PublicKey pubkey2; ECKey pubkey2 = null; try { // pubkey2 = PublicKey.recoverCompact(Sha256Hash.twiceOf(dataToHash), vchSig); //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; pubkey2 = ECKey.fromPublicOnly(pubkey.getBytes()); pubkey2.verifyMessage(message, vchSig.getBytes()); 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(", message: "+ Utils.sanitizeString(new String(message))); errorMessage.append(", sig: not impl!\n" + x.getMessage()); return false; } }
@Override protected void bitcoinSerializeToStream(OutputStream stream) throws IOException { vin.bitcoinSerialize(stream); address.bitcoinSerialize(stream); pubKeyCollateralAddress.bitcoinSerialize(stream); pubKeyMasternode.bitcoinSerialize(stream); sig.bitcoinSerialize(stream); Utils.int64ToByteStreamLE(sigTime, stream); Utils.uint32ToByteStreamLE(protocolVersion, stream); lastPing.bitcoinSerialize(stream); Utils.int64ToByteStreamLE(nLastDsq, stream); }
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()); }
Masternode find(Script payee) { //LOCK(cs); lock.lock(); try { Script payee2; //BOOST_FOREACH(CMasternode& mn, vMasternodes) for (Masternode mn : vMasternodes) { //payee2 = GetScriptForDestination(mn.pubkey.GetID()); payee2 = ScriptBuilder.createOutputScript(mn.pubKeyCollateralAddress.getECKey()); if (payee2 == payee) return mn; } return null; } finally { lock.unlock(); } }