/** Checks if the given input passes some of the AreInputsStandard checks. Not complete. */ public static RuleViolation isInputStandard(TransactionInput input) { for (ScriptChunk chunk : input.getScriptSig().getChunks()) { if (chunk.data != null && !chunk.isShortestPossiblePushData()) return RuleViolation.SHORTEST_POSSIBLE_PUSHDATA; if (chunk.isPushData()) { ECDSASignature signature; try { signature = ECKey.ECDSASignature.decodeFromDER(chunk.data); } catch (IllegalArgumentException x) { // Doesn't look like a signature. signature = null; } if (signature != null) { if (!TransactionSignature.isEncodingCanonical(chunk.data)) return RuleViolation.SIGNATURE_CANONICAL_ENCODING; if (!signature.isCanonical()) return RuleViolation.SIGNATURE_CANONICAL_ENCODING; } } } return RuleViolation.NONE; }
/** Checks if the given input passes some of the AreInputsStandard checks. Not complete. */ public static RuleViolation isInputStandard(TransactionInput input) { for (ScriptChunk chunk : input.getScriptSig().getChunks()) { if (chunk.data != null && !chunk.isShortestPossiblePushData()) return RuleViolation.SHORTEST_POSSIBLE_PUSHDATA; if (chunk.isPushData()) { ECDSASignature signature; try { signature = ECKey.ECDSASignature.decodeFromDER(chunk.data); } catch (RuntimeException x) { // Doesn't look like a signature. signature = null; } if (signature != null) { if (!TransactionSignature.isEncodingCanonical(chunk.data)) return RuleViolation.SIGNATURE_CANONICAL_ENCODING; if (!signature.isCanonical()) return RuleViolation.SIGNATURE_CANONICAL_ENCODING; } } } return RuleViolation.NONE; }
@Test public void keyRecovery() throws Exception { ECKey key = new ECKey(); String message = "Hello World!"; Sha256Hash hash = Sha256Hash.of(message.getBytes()); ECKey.ECDSASignature sig = key.sign(hash); key = ECKey.fromPublicOnly(key.getPubKeyPoint()); boolean found = false; for (int i = 0; i < 4; i++) { ECKey key2 = ECKey.recoverFromSignature(i, sig, hash, true); checkNotNull(key2); if (key.equals(key2)) { found = true; break; } } assertTrue(found); }
@Test public void keyRecoveryWithEncryptedKey() throws Exception { ECKey unencryptedKey = new ECKey(); KeyParameter aesKey = keyCrypter.deriveKey(PASSWORD1); ECKey encryptedKey = unencryptedKey.encrypt(keyCrypter, aesKey); String message = "Goodbye Jupiter!"; Sha256Hash hash = Sha256Hash.of(message.getBytes()); ECKey.ECDSASignature sig = encryptedKey.sign(hash, aesKey); unencryptedKey = ECKey.fromPublicOnly(unencryptedKey.getPubKeyPoint()); boolean found = false; for (int i = 0; i < 4; i++) { ECKey key2 = ECKey.recoverFromSignature(i, sig, hash, true); checkNotNull(key2); if (unencryptedKey.equals(key2)) { found = true; break; } } assertTrue(found); }
@Test public void sValue() throws Exception { // Check that we never generate an S value that is larger than half the curve order. This avoids a malleability // issue that can allow someone to change a transaction [hash] without invalidating the signature. final int ITERATIONS = 10; ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(ITERATIONS)); List<ListenableFuture<ECKey.ECDSASignature>> sigFutures = Lists.newArrayList(); final ECKey key = new ECKey(); for (byte i = 0; i < ITERATIONS; i++) { final Sha256Hash hash = Sha256Hash.of(new byte[]{i}); sigFutures.add(executor.submit(new Callable<ECKey.ECDSASignature>() { @Override public ECKey.ECDSASignature call() throws Exception { return key.sign(hash); } })); } List<ECKey.ECDSASignature> sigs = Futures.allAsList(sigFutures).get(); for (ECKey.ECDSASignature signature : sigs) { assertTrue(signature.isCanonical()); } final ECDSASignature first = sigs.get(0); final ECKey.ECDSASignature duplicate = new ECKey.ECDSASignature(first.r, first.s); assertEquals(first, duplicate); assertEquals(first.hashCode(), duplicate.hashCode()); final ECKey.ECDSASignature highS = new ECKey.ECDSASignature(first.r, ECKey.CURVE.getN().subtract(first.s)); assertFalse(highS.isCanonical()); }
public static Script getMultisigInputScript (ECDSASignature client, ECDSASignature server) { ArrayList<TransactionSignature> signList = new ArrayList<TransactionSignature>(); signList.add(new TransactionSignature(client, SigHash.ALL, false)); signList.add(new TransactionSignature(server, SigHash.ALL, false)); Script inputScript = ScriptBuilder.createMultiSigInputScript(signList); /* * Seems there is a bug here, * https://groups.google.com/forum/#!topic/bitcoinj/A9R8TdUsXms */ Script workaround = new Script(inputScript.getProgram()); return workaround; }
/** * Gets the multisig input script. * * @param client the client * @param server the server * @return the multisig input script */ public static Script getMultisigInputScript (ECDSASignature client, ECDSASignature server) { ArrayList<TransactionSignature> signList = new ArrayList<TransactionSignature>(); signList.add(new TransactionSignature(client, SigHash.ALL, false)); signList.add(new TransactionSignature(server, SigHash.ALL, false)); Script inputScript = ScriptBuilder.createMultiSigInputScript(signList); /* * Seems there is a bug here, * https://groups.google.com/forum/#!topic/bitcoinj/A9R8TdUsXms */ Script workaround = new Script(inputScript.getProgram()); return workaround; }
private boolean isSignatureCorrect(ECDSASignature signature, byte[] challengeString) { Sha256Hash hash = Sha256Hash.wrap(Sha256Hash.hash(challengeString)); ECKey verificationKey = ECKey.fromPublicOnly(partnerProof.getScriptPair().getPubKey()); return verificationKey.verify(hash, signature); }
private ECDSASignature sign(Sha256Hash hash, ECKey signingKey) { return signingKey.sign(hash); }
public static TransactionSignature getSignature (Transaction transactionToSign, int index, byte[] outputToSpend, ECKey key) { Sha256Hash hash = transactionToSign.hashForSignature(index, outputToSpend, SigHash.ALL, false); ECDSASignature signature = key.sign(hash).toCanonicalised(); return new TransactionSignature(signature, SigHash.ALL, false); }
public static String sign(ECKey key, String input) throws UnsupportedEncodingException { byte[] data = input.getBytes("UTF8"); Sha256Hash hash = Sha256Hash.of(data); ECDSASignature sig = key.sign(hash, null); byte[] bytes = sig.encodeToDER(); return bytesToHex(bytes); }
/** * Check signature. * * @param transaction the transaction * @param index the index * @param outputToSpend the output to spend * @param key the key * @param signature the signature * @return true, if successful */ public static boolean checkSignature (Transaction transaction, int index, TransactionOutput outputToSpend, ECKey key, byte[] signature) { Sha256Hash hash = transaction.hashForSignature(index, outputToSpend.getScriptBytes(), SigHash.ALL, false); return key.verify(hash, ECDSASignature.decodeFromDER(signature)); }
/** * Instantiates a new transaction wrapper. * * @param transaction the transaction * @param signature the signature */ public TransactionWrapper (Transaction transaction, ECDSASignature signature) { this.transaction = transaction; this.signature = signature; }
/** * Gets the signature. * * @return the signature */ public ECDSASignature getSignature () { return signature; }