All files / src/transaction signTransaction.js

100% Statements 17/17
100% Branches 0/0
100% Functions 2/2
100% Lines 16/16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 364x 4x 4x 4x   4x                             18x 14x 14x 15x 15x 15x 15x 15x 15x   15x     14x    
import { Buffer } from 'buffer'
import base58 from 'bs58'
import cc from 'five-bells-condition'
import clone from 'clone'
 
import serializeTransactionIntoCanonicalString from './serializeTransactionIntoCanonicalString'
 
 
/**
 * @public
 * Sign the given `transaction` with the given `privateKey`s, returning a new copy of `transaction`
 * that's been signed.
 * Note: Only generates Ed25519 Fulfillments. Thresholds and other types of Fulfillments are left as
 * an exercise for the user.
 * @param {object} transaction Transaction to sign. `transaction` is not modified.
 * @param {...string} privateKeys Private keys associated with the issuers of the `transaction`.
 *                                Looped through to iteratively sign any Input Fulfillments found in
 *                                the `transaction`.
 * @returns {object} The signed version of `transaction`.
 */
export default function signTransaction(transaction, ...privateKeys) {
    const signedTx = clone(transaction)
    signedTx.inputs.forEach((input, index) => {
        const privateKey = privateKeys[index]
        const privateKeyBuffer = new Buffer(base58.decode(privateKey))
        const serializedTransaction = serializeTransactionIntoCanonicalString(transaction)
        const ed25519Fulfillment = new cc.Ed25519()
        ed25519Fulfillment.sign(new Buffer(serializedTransaction), privateKeyBuffer)
        const fulfillmentUri = ed25519Fulfillment.serializeUri()
 
        input.fulfillment = fulfillmentUri
    })
 
    return signedTx
}