From 703f92fb34e010e28403c5d02dbfe93a9d3aa26d Mon Sep 17 00:00:00 2001 From: Esteban Ordano Date: Wed, 11 Feb 2015 13:01:24 -0300 Subject: [PATCH] Update documentation with indications on signatures --- docs/transaction.md | 65 ++++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/docs/transaction.md b/docs/transaction.md index 0c4f04e..0f17c8e 100644 --- a/docs/transaction.md +++ b/docs/transaction.md @@ -36,6 +36,49 @@ var transaction = new Transaction().fee(5430); // Minimum non-dust amount var transaction = new Transaction().fee(1e8); // Generous fee of 1 BTC ``` +## Multisig Transactions + +To send a transaction to a multisig address, the API is the same as in the above example. To spend outputs that require multiple signatures, the process needs extra information: the public keys of the signers that can unlock that output. + +```javascript +var multiSigTx = new Transaction() + .from(utxo, publicKeys, threshold) + .change(address) + .sign(myKeys); + +var serialized = multiSigTx.toObject(); +``` + +This can be serialized and sent to another party, to complete with the needed signatures: + +```javascript +var multiSigTx = new Transaction(serialized) + .sign(anotherSetOfKeys); + +assert(multiSigTx.isFullySigned()); +``` + +Also, you can just send over the signature for your private key: + +```javascript +var multiSigTx = new Transaction() + .from(utxo, publicKeys, threshold) + .change(address); + +var signature = multiSigTx.getSignatures(privateKey)[0]; +console.log(signature.toJSON()); +console.log(signature.toObject()); +console.log(signature.signature.toString()); // Outputs a DER signature +console.log(signature.sigtype); +``` + +Transfer that over the wire, and on the other side, apply it to a transaction: + +```javascript +assert(transaction.isValidSignature(receivedSig)); +transaction.applySignature(receivedSig); +``` + ## Adding inputs Transaction inputs are instances of either [Input](https://github.com/bitpay/bitcore/tree/master/lib/transaction/input) or its subclasses. `Input` has some abstract methods, as there is no actual concept of a "signed input" in the bitcoin scripting system (just valid signatures for OP_CHECKSIG and similar opcodes). They are stored in the `input` property of `Transaction` instances. @@ -118,28 +161,6 @@ For this reason, some methods in the Transaction class are provided: Internally, a `_changeIndex` property stores the index of the change output (so it can get updated when a new input or output is added). -## Multisig Transactions - -To send a transaction to a multisig address, the API is the same as in the above example. To spend outputs that require multiple signatures, the process needs extra information: the public keys of the signers that can unlock that output. - -```javascript -var multiSigTx = new Transaction() - .from(utxo, publicKeys, threshold) - .change(address) - .sign(myKeys); - -var serialized = multiSigTx.toObject(); -``` - -This can be serialized and sent to another party, to complete with the needed signatures: - -```javascript -var multiSigTx = new Transaction(serialized) - .sign(anotherSetOfKeys); - -assert(multiSigTx.isFullySigned()); -``` - ## Time-Locking transaction All bitcoin transactions contain a locktime field. The locktime indicates the earliest time a transaction can be added to the blockchain.