Java Code Examples for org.bitcoinj.core.Sha256Hash#wrap()

The following examples show how to use org.bitcoinj.core.Sha256Hash#wrap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Util.java    From jelectrum with MIT License 6 votes vote down vote up
public static Sha256Hash hashDoubleBs(ByteString bs)
{
    try
    {

        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(bs.toByteArray());

        byte[] pass = md.digest();
        md.update(pass);

        return Sha256Hash.wrap(md.digest());
    }
    catch(java.security.NoSuchAlgorithmException e)
    {
        throw new RuntimeException(e);
    }

}
 
Example 2
Source File: Util.java    From jelectrum with MIT License 6 votes vote down vote up
public static Sha256Hash doubleHash(Sha256Hash a)
{
    try
    {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(a.getBytes());

        byte[] pass = md.digest();
        md = MessageDigest.getInstance("SHA-256");
        md.update(pass);

        return Sha256Hash.wrap(md.digest());
    }
    catch(java.security.NoSuchAlgorithmException e)
    {
        throw new RuntimeException(e);
    }

}
 
Example 3
Source File: BitcoinRPC.java    From jelectrum with MIT License 6 votes vote down vote up
public Sha256Hash getBlockHash(int height)
    throws java.io.IOException, org.json.JSONException
{
  
    Random rnd = new Random();
    JSONObject msg = new JSONObject();
    msg.put("id", "" + rnd.nextInt());
    msg.put("method","getblockhash");
    JSONArray params = new JSONArray();
    params.put(height);
    msg.put("params", params);
    JSONObject reply = sendPost(msg);

    return Sha256Hash.wrap(reply.getString("result"));

}
 
Example 4
Source File: MeritConsensus.java    From bisq with GNU Affero General Public License v3.0 6 votes vote down vote up
@VisibleForTesting
private static boolean isSignatureValid(byte[] signatureFromMerit, String pubKeyAsHex, String blindVoteTxId) {
    // We verify if signature of hash of blindVoteTxId is correct. EC key from first input for blind vote tx is
    // used for signature.
    if (pubKeyAsHex == null) {
        log.error("Error at isSignatureValid: pubKeyAsHex is null");
        return false;
    }

    boolean result = false;
    try {
        ECKey pubKey = ECKey.fromPublicOnly(Utilities.decodeFromHex(pubKeyAsHex));
        ECKey.ECDSASignature signature = ECKey.ECDSASignature.decodeFromDER(signatureFromMerit).toCanonicalised();
        Sha256Hash msg = Sha256Hash.wrap(blindVoteTxId);
        result = pubKey.verify(msg, signature);
    } catch (Throwable t) {
        log.error("Signature verification of issuance failed: " + t.toString());
    }
    if (!result) {
        log.error("Signature verification of issuance failed: blindVoteTxId={}, pubKeyAsHex={}",
                blindVoteTxId, pubKeyAsHex);
    }
    return result;
}
 
Example 5
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private void setUTXO(List<UTXOItem> utxoList) {
    Address a;
    if (restFromWif) {
        a = wallet.getImportedKeys().get(0).toAddress(params);
    } else {
        a = wallet.currentReceiveAddress();
    }

    final List<UTXO> utxos = new ArrayList<>();

    for (UTXOItem utxo : utxoList) {
        Sha256Hash hash = Sha256Hash.wrap(utxo.getTxHash());
        utxos.add(new UTXO(hash, utxo.getTxOutputN(), Coin.valueOf(utxo.getSatoshiValue()),
                0, false, ScriptBuilder.createOutputScript(a)));
    }

    UTXOProvider utxoProvider = new UTXOProvider() {
        @Override
        public List<UTXO> getOpenTransactionOutputs(List<Address> addresses) throws UTXOProviderException {
            return utxos;
        }

        @Override
        public int getChainHeadHeight() throws UTXOProviderException {
            return Integer.MAX_VALUE;
        }

        @Override
        public NetworkParameters getParams() {
            return wallet.getParams();
        }
    };
    wallet.setUTXOProvider(utxoProvider);
}
 
Example 6
Source File: Sha256HashDeserializer.java    From consensusj with Apache License 2.0 5 votes vote down vote up
@Override
public Sha256Hash deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    JsonToken token = p.getCurrentToken();
    switch (token) {
        case VALUE_STRING:
            return Sha256Hash.wrap(p.getValueAsString());
        default:
            return (Sha256Hash) ctxt.handleUnexpectedToken(Sha256Hash.class, p);
    }
}
 
Example 7
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private void setUTXO(List<UTXOItem> utxoList) {

        Address a = wallet.currentReceiveAddress();
        final List<UTXO> utxos = new ArrayList<>();

        for (UTXOItem utxo : utxoList) {
            Sha256Hash hash = Sha256Hash.wrap(utxo.getTxHash());
            utxos.add(new UTXO(hash, utxo.getTxOutputN(), Coin.valueOf(utxo.getSatoshiValue()),
                    0, false, ScriptBuilder.createOutputScript(a)));
        }

        UTXOProvider utxoProvider = new UTXOProvider() {
            @Override
            public List<UTXO> getOpenTransactionOutputs(List<Address> addresses) throws UTXOProviderException {
                return utxos;
            }

            @Override
            public int getChainHeadHeight() throws UTXOProviderException {
                return Integer.MAX_VALUE;
            }

            @Override
            public NetworkParameters getParams() {
                return wallet.getParams();
            }
        };
        wallet.setUTXOProvider(utxoProvider);
    }
 
Example 8
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private void setUTXO(List<UTXOItem> utxoList) {
    Address a;
    if (restFromWif) {
        a = wallet.getImportedKeys().get(0).toAddress(params);
    } else {
        a = wallet.currentReceiveAddress();
    }

    final List<UTXO> utxos = new ArrayList<>();

    for (UTXOItem utxo : utxoList) {
        Sha256Hash hash = Sha256Hash.wrap(utxo.getTxHash());
        utxos.add(new UTXO(hash, utxo.getTxOutputN(), Coin.valueOf(utxo.getSatoshiValue()),
                0, false, ScriptBuilder.createOutputScript(a)));
    }

    UTXOProvider utxoProvider = new UTXOProvider() {
        @Override
        public List<UTXO> getOpenTransactionOutputs(List<Address> addresses) throws UTXOProviderException {
            return utxos;
        }

        @Override
        public int getChainHeadHeight() throws UTXOProviderException {
            return Integer.MAX_VALUE;
        }

        @Override
        public NetworkParameters getParams() {
            return wallet.getParams();
        }
    };
    wallet.setUTXOProvider(utxoProvider);
}
 
Example 9
Source File: LevelDBFullPrunedBlockStore.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private void initFromDb() throws BlockStoreException {
    Sha256Hash hash = Sha256Hash.wrap(batchGet(getKey(KeyType.CHAIN_HEAD_SETTING)));
    this.chainHeadBlock = get(hash);
    this.chainHeadHash = hash;
    if (this.chainHeadBlock == null) {
        throw new BlockStoreException("corrupt database block store - head block not found");
    }

    hash = Sha256Hash.wrap(batchGet(getKey(KeyType.VERIFIED_CHAIN_HEAD_SETTING)));
    this.verifiedChainHeadBlock = get(hash);
    this.verifiedChainHeadHash = hash;
    if (this.verifiedChainHeadBlock == null) {
        throw new BlockStoreException("corrupt databse block store - verified head block not found");
    }
}
 
Example 10
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private void setUTXO(List<UTXOListResponse.UTXOItem> utxoList) {
    Address a;
    if (restFromWif) {
        a = wallet.getImportedKeys().get(0).toAddress(params);
    } else {
        a = wallet.currentReceiveAddress();
    }

    final List<UTXO> utxos = new ArrayList<>();

    Log.d("svcom", "resp size - " + utxoList.size());
    for (UTXOListResponse.UTXOItem utxo : utxoList) {
        Sha256Hash hash = Sha256Hash.wrap(utxo.getTxHash());
        utxos.add(new UTXO(hash, utxo.getTxOutputN(), Coin.valueOf(utxo.getSatoshiValue()),
                0, false, ScriptBuilder.createOutputScript(a)));
    }
    for (UTXO u : utxos) {
        Log.d("svcom", "u - " + u.getValue().toFriendlyString());
    }

    UTXOProvider utxoProvider = new UTXOProvider() {
        @Override
        public List<UTXO> getOpenTransactionOutputs(List<Address> addresses) throws UTXOProviderException {
            return utxos;
        }

        @Override
        public int getChainHeadHeight() throws UTXOProviderException {
            return Integer.MAX_VALUE;
        }

        @Override
        public NetworkParameters getParams() {
            return wallet.getParams();
        }
    };
    wallet.setUTXOProvider(utxoProvider);
}
 
Example 11
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private void setUTXO(List<UTXOItem> utxoList) {
    if (wallet == null) return;

    Address a;
    if (restFromWif) {
        a = wallet.getImportedKeys().get(0).toAddress(params);
    } else {
        a = wallet.currentReceiveAddress();
    }

    final List<UTXO> utxos = new ArrayList<>();

    for (UTXOItem utxo : utxoList) {
        Sha256Hash hash = Sha256Hash.wrap(utxo.getTxHash());
        utxos.add(new UTXO(hash, utxo.getTxOutputN(), Coin.valueOf(utxo.getSatoshiValue()),
                0, false, ScriptBuilder.createOutputScript(a)));
    }

    UTXOProvider utxoProvider = new UTXOProvider() {
        @Override
        public List<UTXO> getOpenTransactionOutputs(List<Address> addresses) throws UTXOProviderException {
            return utxos;
        }

        @Override
        public int getChainHeadHeight() throws UTXOProviderException {
            return Integer.MAX_VALUE;
        }

        @Override
        public NetworkParameters getParams() {
            return wallet.getParams();
        }
    };
    wallet.setUTXOProvider(utxoProvider);
}
 
Example 12
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private void setUTXO(List<UTXOItem> utxoList) {
    if (wallet == null) return;

    Address a;
    if (restFromWif) {
        a = wallet.getImportedKeys().get(0).toAddress(params);
    } else {
        a = wallet.currentReceiveAddress();
    }

    final List<UTXO> utxos = new ArrayList<>();

    for (UTXOItem utxo : utxoList) {
        Sha256Hash hash = Sha256Hash.wrap(utxo.getTxHash());
        utxos.add(new UTXO(hash, utxo.getTxOutputN(), Coin.valueOf(utxo.getSatoshiValue()),
                0, false, ScriptBuilder.createOutputScript(a)));
    }

    UTXOProvider utxoProvider = new UTXOProvider() {
        @Override
        public List<UTXO> getOpenTransactionOutputs(List<Address> addresses) throws UTXOProviderException {
            return utxos;
        }

        @Override
        public int getChainHeadHeight() throws UTXOProviderException {
            return Integer.MAX_VALUE;
        }

        @Override
        public NetworkParameters getParams() {
            return wallet.getParams();
        }
    };
    wallet.setUTXOProvider(utxoProvider);
}
 
Example 13
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private void setUTXO(List<UTXOItem> utxoList) {
    if (wallet == null) {
        return;
    }

    Address a = wallet.getImportedKeys().get(0).toAddress(params);
    final List<UTXO> utxos = new ArrayList<>();

    for (UTXOItem utxo : utxoList) {
        Sha256Hash hash = Sha256Hash.wrap(utxo.getTxHash());
        utxos.add(new UTXO(hash, utxo.getTxOutputN(), Coin.valueOf(utxo.getSatoshiValue()),
                0, false, ScriptBuilder.createOutputScript(a)));
    }

    UTXOProvider utxoProvider = new UTXOProvider() {
        @Override
        public List<UTXO> getOpenTransactionOutputs(List<Address> addresses) throws UTXOProviderException {
            return utxos;
        }

        @Override
        public int getChainHeadHeight() throws UTXOProviderException {
            return Integer.MAX_VALUE;
        }

        @Override
        public NetworkParameters getParams() {
            return wallet.getParams();
        }
    };
    wallet.setUTXOProvider(utxoProvider);
}
 
Example 14
Source File: TransactionUtil.java    From chain33-sdk-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static Signature btcCoinSign(byte[] data, byte[] privateKey) {
	byte[] sha256 = TransactionUtil.Sha256(data);
	Sha256Hash sha256Hash = Sha256Hash.wrap(sha256);
	ECKey ecKey = ECKey.fromPrivate(privateKey);
	ECKey.ECDSASignature ecdsas = ecKey.sign(sha256Hash);
	byte[] signByte = ecdsas.encodeToDER();
	Signature signature = new Signature();
	signature.setPubkey(ecKey.getPubKey());
	signature.setSignature(signByte);
	signature.setTy(SignType.SECP256K1.getType());
	return signature;
}
 
Example 15
Source File: LevelDBFullPrunedBlockStore.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private void initFromDb() throws BlockStoreException {
    Sha256Hash hash = Sha256Hash.wrap(batchGet(getKey(KeyType.CHAIN_HEAD_SETTING)));
    this.chainHeadBlock = get(hash);
    this.chainHeadHash = hash;
    if (this.chainHeadBlock == null) {
        throw new BlockStoreException("corrupt database block store - head block not found");
    }

    hash = Sha256Hash.wrap(batchGet(getKey(KeyType.VERIFIED_CHAIN_HEAD_SETTING)));
    this.verifiedChainHeadBlock = get(hash);
    this.verifiedChainHeadHash = hash;
    if (this.verifiedChainHeadBlock == null) {
        throw new BlockStoreException("corrupt databse block store - verified head block not found");
    }
}
 
Example 16
Source File: JSONMap.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
public Sha256Hash getHash(final String k) {
    final String v = getString(k);
    return v == null ? null : Sha256Hash.wrap(v);
}
 
Example 17
Source File: ScriptPattern.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Retrieves the hash from a segwit commitment (in an output of the coinbase transaction).
 */
public static Sha256Hash extractSegwitCommitmentHash(Script script) {
    return Sha256Hash.wrap(Arrays.copyOfRange(script.chunks.get(1).data, 4, 36));
}
 
Example 18
Source File: JSONMap.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public Sha256Hash getHash(final String k) {
    final String v = getString(k);
    return v == null ? null : Sha256Hash.wrap(v);
}
 
Example 19
Source File: GrindTest.java    From jelectrum with MIT License 4 votes vote down vote up
public static Sha256Hash randomHash(Random rnd)
{
  byte[] b=new byte[32];
  rnd.nextBytes(b);
  return Sha256Hash.wrap(b);
}
 
Example 20
Source File: WalletProtobufSerializer.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
public static Sha256Hash byteStringToHash(ByteString bs) {
    return Sha256Hash.wrap(bs.toByteArray());
}