Search in sources :

Example 1 with ValidationException

use of snowblossom.lib.ValidationException in project snowblossom by snowblossomcoin.

the class MsgSigUtil method validateSignedMessage.

public static SignedMessagePayload validateSignedMessage(SignedMessage sm, NetworkParams params) throws ValidationException {
    try {
        SignedMessagePayload payload = SignedMessagePayload.parseFrom(sm.getPayload());
        ByteString signature = sm.getSignature();
        AddressSpec claim = payload.getClaim();
        if ((claim.getRequiredSigners() != 1) || (claim.getSigSpecsCount() != 1)) {
            throw new ValidationException("Multisig not supported");
        }
        MessageDigest md = DigestUtil.getMD();
        byte[] hash = md.digest(sm.getPayload().toByteArray());
        SigSpec sig_spec = claim.getSigSpecs(0);
        if (!SignatureUtil.checkSignature(sig_spec, ByteString.copyFrom(hash), signature)) {
            throw new ValidationException("Signature match failure");
        }
        if (payload.getTimestamp() > params.getMaxClockSkewMs() + System.currentTimeMillis()) {
            throw new ValidationException("Signed message too far into future");
        }
        return payload;
    } catch (com.google.protobuf.InvalidProtocolBufferException e) {
        throw new ValidationException(e);
    }
}
Also used : ValidationException(snowblossom.lib.ValidationException) ByteString(com.google.protobuf.ByteString) SignedMessagePayload(snowblossom.proto.SignedMessagePayload) SigSpec(snowblossom.proto.SigSpec) MessageDigest(java.security.MessageDigest) AddressSpec(snowblossom.proto.AddressSpec)

Example 2 with ValidationException

use of snowblossom.lib.ValidationException in project snowblossom by snowblossomcoin.

the class AddressUtilTest method testAddressChecksumDataChange.

@Test
public void testAddressChecksumDataChange() throws Exception {
    Random rnd = new Random();
    byte[] buff = new byte[Globals.ADDRESS_SPEC_HASH_LEN];
    int checks = 0;
    for (int pass = 0; pass < 10000; pass++) {
        rnd.nextBytes(buff);
        AddressSpecHash spec = new AddressSpecHash(buff);
        String addr = Duck32.encode("d1", spec.getBytes());
        int colon = addr.indexOf(":");
        String without = addr.substring(colon + 1);
        int idx = rnd.nextInt(without.length());
        char replace = Duck32.CHARSET.charAt(rnd.nextInt(32));
        String n = without.substring(0, idx) + replace + without.substring(idx + 1);
        Assert.assertEquals(without.length(), n.length());
        if (!without.equals(n)) {
            System.out.println(without + " " + n);
            try {
                checks++;
                Duck32.decode("d1", n);
                Assert.fail("Should have gotten exception");
            } catch (ValidationException e) {
            }
        }
    }
    System.out.println("Did " + checks + " checksum mutations");
    Assert.assertTrue(checks > 8000);
}
Also used : ValidationException(snowblossom.lib.ValidationException) Random(java.util.Random) ByteString(com.google.protobuf.ByteString) AddressSpecHash(snowblossom.lib.AddressSpecHash) Test(org.junit.Test)

Example 3 with ValidationException

use of snowblossom.lib.ValidationException in project snowblossom by snowblossomcoin.

the class MsgSigUtil method signMessage.

/**
 * @param starting_payload should have of oneof z specified for the body.
 */
public static SignedMessage signMessage(AddressSpec claim, WalletKeyPair wkp, SignedMessagePayload starting_payload) throws ValidationException {
    if ((claim.getRequiredSigners() != 1) || (claim.getSigSpecsCount() != 1)) {
        throw new ValidationException("Multisig not supported");
    }
    SignedMessagePayload.Builder payload = SignedMessagePayload.newBuilder();
    payload.mergeFrom(starting_payload);
    payload.setTimestamp(System.currentTimeMillis());
    payload.setClaim(claim);
    ByteString payload_data = payload.build().toByteString();
    SignedMessage.Builder signed = SignedMessage.newBuilder();
    signed.setPayload(payload_data);
    MessageDigest md = DigestUtil.getMD();
    byte[] hash = md.digest(payload_data.toByteArray());
    signed.setSignature(SignatureUtil.sign(wkp, ByteString.copyFrom(hash)));
    return signed.build();
}
Also used : ValidationException(snowblossom.lib.ValidationException) ByteString(com.google.protobuf.ByteString) SignedMessagePayload(snowblossom.proto.SignedMessagePayload) SignedMessage(snowblossom.proto.SignedMessage) MessageDigest(java.security.MessageDigest)

Example 4 with ValidationException

use of snowblossom.lib.ValidationException in project snowblossom by snowblossomcoin.

the class WalletUtil method testWallet.

public static List<AddressSpecHash> testWallet(WalletDatabase db) throws ValidationException {
    Random rnd = new Random();
    byte[] rnd_bytes = new byte[32];
    rnd.nextBytes(rnd_bytes);
    ChainHash rnd_hash = new ChainHash(rnd_bytes);
    for (WalletKeyPair pair : db.getKeysList()) {
        SigSpec sig_spec = SigSpec.newBuilder().setSignatureType(pair.getSignatureType()).setPublicKey(pair.getPublicKey()).build();
        ByteString sig = SignatureUtil.sign(pair, rnd_hash);
        if (!SignatureUtil.checkSignature(sig_spec, rnd_hash.getBytes(), sig)) {
            throw new ValidationException("Signature check failure on keypair: " + pair);
        }
    }
    LinkedList<AddressSpecHash> addresses = new LinkedList<>();
    for (AddressSpec spec : db.getAddressesList()) {
        addresses.add(AddressUtil.getHashForSpec(spec));
    }
    return addresses;
}
Also used : ChainHash(snowblossom.lib.ChainHash) ValidationException(snowblossom.lib.ValidationException) Random(java.util.Random) ByteString(com.google.protobuf.ByteString) AddressSpecHash(snowblossom.lib.AddressSpecHash) LinkedList(java.util.LinkedList)

Example 5 with ValidationException

use of snowblossom.lib.ValidationException in project snowblossom by snowblossomcoin.

the class ShardUtxoImport method getImportBlock.

/**
 * Gets the ImportedBlock as if *all* the exported shards were to be imported.
 * That way this can be filtered for what is needed but cached as a whole thing.
 */
public ImportedBlock getImportBlock(ChainHash hash) {
    synchronized (cache) {
        ImportedBlock ib = cache.get(hash);
        if (ib != null)
            return ib;
    }
    try (TimeRecordAuto tra = TimeRecord.openAuto("ShardUtxoImport.getImportBlock")) {
        ImportedBlock ib = null;
        if (node.getDB().getBlockTrust(hash)) {
            ib = node.getDB().getImportedBlockMap().get(hash.getBytes());
        }
        if (ib == null) {
            ImportedBlock.Builder ibb = ImportedBlock.newBuilder();
            Block blk = node.getDB().getBlockMap().get(hash.getBytes());
            if (blk == null)
                return null;
            ibb.setHeader(blk.getHeader());
            Set<Integer> cover_set = ShardUtil.getCoverSet(blk.getHeader().getShardId(), node.getParams());
            Map<Integer, ImportedOutputList.Builder> output_list_map = new TreeMap<>();
            for (Transaction tx : blk.getTransactionsList()) {
                TransactionInner tx_inner = TransactionUtil.getInner(tx);
                ArrayList<ByteString> tx_out_wire_lst;
                try {
                    tx_out_wire_lst = TransactionUtil.extractWireFormatTxOut(tx);
                } catch (ValidationException e) {
                    throw new RuntimeException(e);
                }
                int out_idx = 0;
                for (TransactionOutput tx_out : tx_inner.getOutputsList()) {
                    if (!cover_set.contains(tx_out.getTargetShard())) {
                        int ts = tx_out.getTargetShard();
                        if (!output_list_map.containsKey(ts)) {
                            output_list_map.put(ts, ImportedOutputList.newBuilder());
                        }
                        ImportedOutput io = ImportedOutput.newBuilder().setRawOutput(tx_out_wire_lst.get(out_idx)).setTxId(tx.getTxHash()).setOutIdx(out_idx).build();
                        output_list_map.get(ts).addTxOuts(io);
                    }
                    out_idx++;
                }
            }
            for (Map.Entry<Integer, ImportedOutputList.Builder> me : output_list_map.entrySet()) {
                ibb.putImportOutputs(me.getKey(), me.getValue().build());
            }
            ib = ibb.build();
        }
        try (TimeRecordAuto tra_cache = TimeRecord.openAuto("ShardUtxoImport.getImportBlock_cachesave")) {
            synchronized (cache) {
                cache.put(hash, ib);
            }
        }
        return ib;
    }
}
Also used : ValidationException(snowblossom.lib.ValidationException) ByteString(com.google.protobuf.ByteString) TreeMap(java.util.TreeMap) TimeRecordAuto(duckutil.TimeRecordAuto) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

ByteString (com.google.protobuf.ByteString)5 ValidationException (snowblossom.lib.ValidationException)5 MessageDigest (java.security.MessageDigest)2 Random (java.util.Random)2 AddressSpecHash (snowblossom.lib.AddressSpecHash)2 SignedMessagePayload (snowblossom.proto.SignedMessagePayload)2 TimeRecordAuto (duckutil.TimeRecordAuto)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Test (org.junit.Test)1 ChainHash (snowblossom.lib.ChainHash)1 AddressSpec (snowblossom.proto.AddressSpec)1 SigSpec (snowblossom.proto.SigSpec)1 SignedMessage (snowblossom.proto.SignedMessage)1