Block Chain Foundation: trading simple to achieve __ block chain

Source: Internet
Author: User
Tags hash unique id
1. Transaction input
Package Com.blockchain.model;

/**
 * Transaction Input,utxo=<txid,value>
 * A transaction, may have multiple inputs/public
class Transactioninput {

    /**
     * Previous transaction ID
     *
    /private String Txid;
    /**
     * Transaction amount * *
    private int value;
    /**
     * Transaction signature
    /private String signature;
    /**
     * Transaction sender's wallet public key
    /private String publickey;

    Public Transactioninput () {
        super ();
    }

    Public Transactioninput (string txid, int value, string signature, string publickey) {
        super ();
        This.txid = Txid;
        This.value = value;
        This.signature = signature;
        This.publickey = PublicKey;
    }

    Omit getter and Setter
}
2. Transaction Output
Package Com.blockchain.model;

/**
 * Transaction output, a transaction may have multiple outputs/public
class Transactionoutput {

    /**
     * Admission Amount
     /
    private int value;
    /**
     * Transaction recipient's wallet public key hash value *
    /private String Publickeyhash;

    Public Transactionoutput () {
        super ();
    }

    public transactionoutput (int value, String publickeyhash) {
        this.value = value;
        This.publickeyhash = Publickeyhash;
    }
    Omit getter and Setter
}
3. Trading Parameters
Package Com.blockchain.model;

/**
 * Transaction Interface parameter
 */public
class Transactionparam {

    /**
     * sender's wallet address
    * * Private String Sender;
    /**
     * Receiver's wallet address
     *
    /private String recipient;
    /**
     * Transaction amount
    /private int amount;

    Omit getter and Setter

}
3. Trading
Package Com.blockchain.model;

Import java.util.List;
Import Com.alibaba.fastjson.JSON;
Import Com.blockchain.security.CryptoUtil;

Import Com.blockchain.security.RSACoder; 
     /** * Transaction class * Easy process, the transfer party need to sign the script to prove that they are utxo legal users easy process, the transfer party need to sign the script to prove that they are utxo legitimate users/public class Transaction {/**
    * Transaction Unique ID/private String ID;

    /** * Simple Transaction input (only one input)/private transactioninput txin;

    /** * Simple Transaction output (only one output)/private transactionoutput txout;

    /** * General Transaction input/private list<transactioninput> txinlist;  

    /** * Simple Transaction output (only one output)/private list<transactionoutput> txoutlist;
    Public Transaction () {super (); /** * Simple transaction: Only one input and one output/public Transaction (String ID, transactioninput txin, Transactionoutput txout
        ) {super ();
        This.id = ID;
        This.txin = Txin;
    This.txout = Txout; /** * General transactions: multiple inputs and multiple outputs/public transactioN (String ID, list<transactioninput> txinlist, list<transactionoutput> txoutlist) {super ();
        This.id = ID;
        This.txinlist = txinlist;
    This.txoutlist = txoutlist;
        /** * Whether the system generates block reward transactions * Coinbase transaction txin is null value (previous transaction: ID is 0,value to-1)/public boolean coinbasetx () {
    return Txin.gettxid () equals ("0") && txin.getvalue () = = 1;
     /** * Payer to sign the transaction signature confirmation, with the transaction initiator's private key to the transaction of the hash data signature: * (Generally, the sender will use the hash algorithm to deal with the hash value, and then use the private key to encrypt the hash value, to obtain a signature.)
     * The sender then sends the message and the signature to the recipient. * The receiver decrypts the signature using the sender's public key, restores the hash value, and then verifies that the hash value of the information is consistent with the hash value of the decrypted signature by the hash algorithm, * so that it can authenticate whether the information is from the sender or whether the information is tampered with. * * Signature Result deposit in the transaction input (txin) * * * @param privatekey Transaction Initiator's private key * @param prevtx * * Public voi
        D sign (String Privatekey, Transaction prevtx) {if (Coinbasetx ()) {//Skip mining system reward transaction return; The previous transaction that was referenced by the transaction input does not match the previous transaction that was passed in if (!prevtx.getid (). Equals (Txin.gettxid ()) {System.ERR.PRINTLN ("Transaction signature failed: The previous transaction referenced by the current transaction input does not match the previous transaction");
        } Transaction Txclone = Clonetx ();
        Set up the transaction copy's Txin public Key Txclone.gettxin (). Setpublickey (Prevtx.gettxout (). Getpublickeyhash ());
        String sign = "";
        try {/////Use the transaction initiator's private key to sign the transaction's hash data sign = Rsacoder.sign (Txclone.hash (). GetBytes (), Privatekey); The catch (Exception e) {System.err.println ("Digital signature error.
            ");
        E.printstacktrace ();
        } System.out.println ("Digital signature length:" +sign.length ());
    Txin.setsignature (sign); /** * Generate transaction copy for transaction signature/public Transaction Clonetx () {Transactioninput transactioninput = n
        EW Transactioninput (Txin.gettxid (), txin.getvalue (), NULL, NULL);
        Transactionoutput transactionoutput = new Transactionoutput (Txout.getvalue (), Txout.getpublickeyhash ());
    return new Transaction (ID, transactioninput, transactionoutput);
  /** * Payee checks the payer's transaction signature * @param prevtx   * * Public boolean verify (Transaction prevtx) {if (Coinbasetx ()) {//Skip mining system reward transaction return true; The previous transaction that was referenced by the transaction input does not match the previous transaction that was passed in if (!prevtx.getid (). Equals (Txin.gettxid ()) {SYSTEM.ERR.P
        Rintln ("Verify transaction Signature failed: Current transaction input references previous transaction does not match incoming previous transaction");
        } Transaction Txclone = Clonetx ();
        The output of the last transaction specifies the recipient's public key, which is the public key of the current transaction initiator Txclone.gettxin (). Setpublickey (Prevtx.gettxout (). Getpublickeyhash ());
        Boolean result = false; try {//the signature of the sender's public key verifies the result = Rsacoder.verify (Txclone.hash (). GetBytes (), Txin.getpublickey (), TX
        In.getsignature ());
        catch (Exception e) {e.printstacktrace ();
    return result;
    /** * Generate TRANSACTION Hash */public String hash () {return cryptoutil.sha256 (json.tojsonstring (this));
        @Override public int hashcode () {final int prime = 31;
        int result = 1; result = Prime * result + ((id = null)? 0:id.hashcode ());
    return result;
        @Override public boolean equals (Object obj) {if (this = obj) return true;
        if (obj = null) return false;
        if (GetClass ()!= Obj.getclass ()) return false;
        Transaction other = (Transaction) obj;
        if (id = = NULL) {if (other.id!= null) return false;
        else if (!id.equals (Other.id)) return false;
    return true;
 }//Omit getter and setter}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.