---
# System prepended metadata

title: Untitled

---

***INTRODUCTION***
Blockchain is a very powerful security tool which ensures that data or money is transferred securely between computers (miners).These miners accepts, store and process the data based on crytographic standards and finally convert it into blocks. The data associated to this block during the conversion process is converted into a hash function. The SHA-256 standard is an example of a cipher system that generates these hash functions. The SHA-256 standard always provide 64-character output in hexadecimal even if the input is just 1 character. 


***MOTIVATION***
Currently, no message signing protocol can prove control of funds neither is there any sign message protocol that can fix limitation issues of signatures. Hence the need to use Jupyter Noteboks for BIP 0322 Signature Implementation.


***BIP 0322 SIGNATURE IMPLEMENTATION USING JUPYTER NOTEBOOKS***
BIP 0322 is a standard for signing interoperable messages based on the Bitcoin Script format

The Jupyter notebook verifies a BIP 0322 simple signature on the message "Hello World" using address bc1q9vza2e8x573nczrlzms0wvx3gsqjx7vavgkx0l which is listed as a test vector with a coresponding signature in the BIP. The notebook attempts to verify both the signature produced in the BIP0322_signing notebook and the signature test vector provided in BIP 0322. 

For easy interoperability with existing signing hardware, signature message format needs to be defined. For instance, a FULL signature message format is defined below where there are two virtual transactions (can't be spent on any network except it's valid): to_spend and to_sign.

**The to_spend transaction is:**

    nVersion = 0
    nLockTime = 0
    vin[0].prevout.hash = 0000...000
    vin[0].prevout.n = 0xFFFFFFFF
    vin[0].nSequence = 0
    vin[0].scriptSig = OP_0 PUSH32[ message_hash ]
    vin[0].scriptWitness = []
    vout[0].nValue = 0
    vout[0].scriptPubKey = message_challenge

NB:
message_hash is a BIP340-tagged hash of the message, i.e. sha256_tag(m)
tag = BIP0322-signed-message 
m = message as is without length prefix or null terminator, message_challenge = to be proven (public) key script.

**The to_sign transaction is:**

    nVersion = 0 or (FULL format only) as appropriate (e.g. 2, for time locks)
    nLockTime = 0 or (FULL format only) as appropriate (for time locks)
    vin[0].prevout.hash = to_spend.txid
    vin[0].prevout.n = 0
    vin[0].nSequence = 0 or (FULL format only) as appropriate (for time locks)
    vin[0].scriptWitness = message_signature
    vout[0].nValue = 0
    vout[0].scriptPubKey = OP_RETURN


*Properties of the full signature type*  

1,) Consists of the base64-encoding of the to_sign transaction in standard network serialisation once it has been signed, 
2,) The to_spend and to_sign transactions must be valid transactions which pass all consensus checks, except of course that the output with prevout 000...000:FFFFFFFF does not exist.

***HOW IT WORKS***
For this project, we'll utilize **SIMPLE** signature message format. 
A simple signature consists of a witness stack, consensus encoded as a vector of vectors of bytes, and base64-encoded. Validators should construct to_spend and to_sign are constructed with validators as defined aboved using default values for all fields except that

message_hash is a BIP340-tagged hash of the message, as specified above
message_challenge in to_spend is set to the scriptPubKey being signed with
message_signature in to_sign is set to the provided simple signature.
and then proceed as they would for a full signature.

The first transaction, to_spend, encapsulates the message to be signed and the scriptPubKey which must be unlocked to produce a valid signature on this message. 

The second transaction, to_sign, is a transaction that takes the single tx_output of to_spend as an input and "spends" this output with a valid signature that unlocks the output and can be interpretted by any Bitcoin script engine. 

The resulting signature is stored in the witness of the to_sign transaction  and the witness is encoded which forms the signature that would be provided to a verifier.

**Link to Install Jupyter Notebooks**
https://github.com/LegReq/bip0322-signatures

***NOTEBOOK STEPS***
1.) Generate a tagged hash of the message to sign (verify against test vector)
2.) Import the provided private key and generate its associated address
3.) Create the virtual to_spend transaction
4.) Create unsigned virtual to_sign transaction
5.) Sign the to_sign transaction using the private key (from 2.)


