Search in sources :

Example 1 with AddressSpec

use of snowblossom.proto.AddressSpec 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 AddressSpec

use of snowblossom.proto.AddressSpec in project snowblossom by snowblossomcoin.

the class AddressUtil method getMultiSig.

public static AddressSpec getMultiSig(int required, List<WalletKeyPair> wkp_list) {
    AddressSpec.Builder addrspec = AddressSpec.newBuilder();
    addrspec.setRequiredSigners(required);
    Assert.assertTrue(required >= wkp_list.size());
    for (WalletKeyPair wkp : wkp_list) {
        addrspec.addSigSpecs(SigSpec.newBuilder().setSignatureType(wkp.getSignatureType()).setPublicKey(wkp.getPublicKey()).build());
    }
    return addrspec.build();
}
Also used : WalletKeyPair(snowblossom.proto.WalletKeyPair) AddressSpec(snowblossom.proto.AddressSpec)

Example 3 with AddressSpec

use of snowblossom.proto.AddressSpec in project snowblossom by snowblossomcoin.

the class CertGen method getServerSSLContext.

public static SslContext getServerSSLContext(WalletDatabase db) throws Exception {
    if (db.getKeysCount() != 1)
        throw new RuntimeException("Unexpected number of keys in wallet db");
    if (db.getAddressesCount() != 1)
        throw new RuntimeException("Unexpected number of addresses in wallet db");
    WalletKeyPair wkp = db.getKeys(0);
    AddressSpec address_spec = db.getAddresses(0);
    WalletKeyPair tls_wkp = KeyUtil.generateWalletRSAKey(2048);
    KeyPair tls_pair = KeyUtil.decodeKeypair(tls_wkp);
    X509Certificate cert = generateSelfSignedCert(wkp, tls_wkp, address_spec);
    // System.out.println(cert);
    ByteString pem_cert = pemCodeCert(cert);
    ByteString pem_prv = pemCodeECPrivateKey(tls_pair.getPrivate());
    return GrpcSslContexts.forServer(pem_cert.newInput(), pem_prv.newInput()).build();
}
Also used : WalletKeyPair(snowblossom.proto.WalletKeyPair) KeyPair(java.security.KeyPair) WalletKeyPair(snowblossom.proto.WalletKeyPair) ByteString(com.google.protobuf.ByteString) AddressSpec(snowblossom.proto.AddressSpec) X509Certificate(java.security.cert.X509Certificate)

Example 4 with AddressSpec

use of snowblossom.proto.AddressSpec in project snowblossom by snowblossomcoin.

the class PeerUtil method isSane.

public static boolean isSane(PeerInfo a, NetworkParams params) {
    if (a.toByteString().size() > 16000)
        return false;
    if (a.getHost().length() < 1)
        return false;
    if (a.getHost().length() > 255)
        return false;
    if (a.getPort() <= 0)
        return false;
    if (a.getPort() > 65535)
        return false;
    if (!HexUtil.getSafeString(a.getHost()).equals(a.getHost()))
        return false;
    if (!HexUtil.getSafeString(a.getVersion()).equals(a.getVersion()))
        return false;
    if (a.getNodeId().size() > Globals.MAX_NODE_ID_SIZE)
        return false;
    if (a.getLastChecked() > System.currentTimeMillis())
        return false;
    if (a.getLastPassed() > System.currentTimeMillis())
        return false;
    if (a.getLearned() > System.currentTimeMillis())
        return false;
    if (a.getNodeSnowAddress().size() > Globals.ADDRESS_SPEC_HASH_LEN)
        return false;
    if (a.getTrustnetAddress().size() > Globals.ADDRESS_SPEC_HASH_LEN)
        return false;
    for (int shard_id : a.getShardIdSetList()) {
        if (shard_id < 0)
            return false;
        if (shard_id > params.getMaxShardId())
            return false;
    }
    // If there is a claim of a trustnet, it must be signed
    if (a.getTrustnetAddress().size() > 0) {
        try {
            SignedMessagePayload payload = MsgSigUtil.validateSignedMessage(a.getTrustnetSignedPeerInfo(), params);
            PeerInfo b = payload.getPeerInfo();
            AddressSpec claim = payload.getClaim();
            AddressSpecHash signed_by = AddressUtil.getHashForSpec(claim);
            if (!signed_by.equals(a.getTrustnetAddress()))
                return false;
            // At this point, the peer info has a signed version and it is signed by the claimed trustnet address
            if (!a.getHost().equals(b.getHost()))
                return false;
            if (a.getPort() != b.getPort())
                return false;
            if (ByteStringComparator.compareStatic(a.getNodeId(), b.getNodeId()) != 0)
                return false;
            if (ByteStringComparator.compareStatic(a.getNodeSnowAddress(), b.getNodeSnowAddress()) != 0)
                return false;
            if (ByteStringComparator.compareStatic(a.getTrustnetAddress(), b.getTrustnetAddress()) != 0)
                return false;
            TreeSet<Integer> a_set = new TreeSet<Integer>();
            a_set.addAll(a.getShardIdSetList());
            TreeSet<Integer> b_set = new TreeSet<Integer>();
            b_set.addAll(b.getShardIdSetList());
            if (!a_set.equals(b_set))
                return false;
        } catch (ValidationException e) {
            return false;
        }
    }
    return true;
}
Also used : PeerInfo(snowblossom.proto.PeerInfo) TreeSet(java.util.TreeSet) SignedMessagePayload(snowblossom.proto.SignedMessagePayload) AddressSpec(snowblossom.proto.AddressSpec)

Example 5 with AddressSpec

use of snowblossom.proto.AddressSpec in project snowblossom by snowblossomcoin.

the class MemPoolTest method addUtxoToUseAtInput.

public static TransactionInput addUtxoToUseAtInput(UtxoUpdateBuffer utxo_buffer, KeyPair keys, long value) throws Exception {
    Random rnd = new Random();
    byte[] tx_id_buff = new byte[Globals.BLOCKCHAIN_HASH_LEN];
    rnd.nextBytes(tx_id_buff);
    ChainHash tx_id = new ChainHash(tx_id_buff);
    AddressSpec claim = AddressUtil.getSimpleSpecForKey(keys.getPublic(), SignatureUtil.SIG_TYPE_ECDSA_COMPRESSED);
    AddressSpecHash addr = AddressUtil.getHashForSpec(claim);
    TransactionInput tx_in = TransactionInput.newBuilder().setSpecHash(addr.getBytes()).setSrcTxId(tx_id.getBytes()).setSrcTxOutIdx(0).build();
    TransactionOutput tx_out = TransactionOutput.newBuilder().setRecipientSpecHash(addr.getBytes()).setValue(value).build();
    utxo_buffer.addOutput(ImmutableList.of(tx_out.toByteString()), tx_out, tx_id, 0);
    return tx_in;
}
Also used : TransactionOutput(snowblossom.proto.TransactionOutput) Random(java.util.Random) AddressSpec(snowblossom.proto.AddressSpec) TransactionInput(snowblossom.proto.TransactionInput)

Aggregations

AddressSpec (snowblossom.proto.AddressSpec)5 ByteString (com.google.protobuf.ByteString)2 SignedMessagePayload (snowblossom.proto.SignedMessagePayload)2 WalletKeyPair (snowblossom.proto.WalletKeyPair)2 KeyPair (java.security.KeyPair)1 MessageDigest (java.security.MessageDigest)1 X509Certificate (java.security.cert.X509Certificate)1 Random (java.util.Random)1 TreeSet (java.util.TreeSet)1 ValidationException (snowblossom.lib.ValidationException)1 PeerInfo (snowblossom.proto.PeerInfo)1 SigSpec (snowblossom.proto.SigSpec)1 TransactionInput (snowblossom.proto.TransactionInput)1 TransactionOutput (snowblossom.proto.TransactionOutput)1