1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2025-02-14 21:10:32 +01:00

Made class for Transaction file

This commit is contained in:
michielmulders 2017-11-02 21:03:15 +01:00
parent aba3c7d4bc
commit 61179c167b

View File

@ -6,38 +6,41 @@ import cc from 'five-bells-condition'
import ccJsonify from './utils/ccJsonify'
import sha256Hash from './sha256Hash'
/**
export default class Transaction {
constructor() { }
/**
* @public
* Canonically serializes a transaction into a string by sorting the keys
* @param {object} (transaction)
* @return {string} a canonically serialized Transaction
*/
export default function serializeTransactionIntoCanonicalString(transaction) {
serializeTransactionIntoCanonicalString(transaction) {
// BigchainDB signs fulfillments by serializing transactions into a
// "canonical" format where
const tx = clone(transaction)
// TODO: set fulfillments to null
// Sort the keys
return stableStringify(tx, (a, b) => (a.key > b.key ? 1 : -1))
}
}
export default function makeInputTemplate(publicKeys = [], fulfills = null, fulfillment = null) {
makeInputTemplate(publicKeys = [], fulfills = null, fulfillment = null) {
return {
fulfillment,
fulfills,
'owners_before': publicKeys,
}
}
}
export default function hashTransaction(transaction) {
hashTransaction(transaction) {
// Safely remove any tx id from the given transaction for hashing
const tx = { ...transaction }
delete tx.id
return sha256Hash(serializeTransactionIntoCanonicalString(tx))
}
}
function makeTransactionTemplate() {
makeTransactionTemplate() {
return {
'id': null,
'operation': null,
@ -47,9 +50,9 @@ function makeTransactionTemplate() {
'asset': null,
'version': '1.0',
}
}
}
export default function makeTransaction(operation, asset, metadata = null, outputs = [], inputs = []) {
makeTransaction(operation, asset, metadata = null, outputs = [], inputs = []) {
const tx = makeTransactionTemplate()
tx.operation = operation
tx.asset = asset
@ -60,9 +63,9 @@ export default function makeTransaction(operation, asset, metadata = null, outpu
// Hashing must be done after, as the hash is of the Transaction (up to now)
tx.id = hashTransaction(tx)
return tx
}
}
/**
/**
* @public
* Generate a `CREATE` transaction holding the `asset`, `metadata`, and `outputs`, to be signed by
* the `issuers`.
@ -81,23 +84,23 @@ export default function makeTransaction(operation, asset, metadata = null, outpu
* @returns {object} Unsigned transaction -- make sure to call signTransaction() on it before
* sending it off!
*/
export default function makeCreateTransaction(asset, metadata, outputs, ...issuers) {
makeCreateTransaction(asset, metadata, outputs, ...issuers) {
const assetDefinition = {
'data': asset || null,
}
const inputs = issuers.map((issuer) => makeInputTemplate([issuer]))
return makeTransaction('CREATE', assetDefinition, metadata, outputs, inputs)
}
}
/**
/**
* @public
* Create an Ed25519 Cryptocondition from an Ed25519 public key to put into an Output of a Transaction
* @param {string} publicKey base58 encoded Ed25519 public key for the recipient of the Transaction
* @param {boolean} [json=true] If true returns a json object otherwise a crypto-condition type
* @returns {object} Ed25519 Condition (that will need to wrapped in an Output)
*/
export default function makeEd25519Condition(publicKey, json = true) {
makeEd25519Condition(publicKey, json = true) {
const publicKeyBuffer = new Buffer(base58.decode(publicKey))
const ed25519Fulfillment = new cc.Ed25519Sha256()
@ -108,9 +111,9 @@ export default function makeEd25519Condition(publicKey, json = true) {
}
return ed25519Fulfillment
}
}
/**
/**
* @public
* Create an Output from a Condition.
* Note: Assumes the given Condition was generated from a single public key (e.g. a Ed25519 Condition)
@ -118,7 +121,7 @@ export default function makeEd25519Condition(publicKey, json = true) {
* @param {string} amount Amount of the output
* @returns {object} An Output usable in a Transaction
*/
export default function makeOutput(condition, amount = '1') {
makeOutput(condition, amount = '1') {
if (typeof amount !== 'string') {
throw new TypeError('`amount` must be of type string')
}
@ -138,16 +141,16 @@ export default function makeOutput(condition, amount = '1') {
'amount': amount,
'public_keys': publicKeys,
}
}
}
/**
/**
* @public
* Create a Preimage-Sha256 Cryptocondition from a secret to put into an Output of a Transaction
* @param {string} preimage Preimage to be hashed and wrapped in a crypto-condition
* @param {boolean} [json=true] If true returns a json object otherwise a crypto-condition type
* @returns {object} Preimage-Sha256 Condition (that will need to wrapped in an Output)
*/
export default function makeSha256Condition(preimage, json = true) {
makeSha256Condition(preimage, json = true) {
const sha256Fulfillment = new cc.PreimageSha256()
sha256Fulfillment.preimage = new Buffer(preimage)
@ -155,9 +158,9 @@ export default function makeSha256Condition(preimage, json = true) {
return ccJsonify(sha256Fulfillment)
}
return sha256Fulfillment
}
}
/**
/**
* @public
* Create an Sha256 Threshold Cryptocondition from threshold to put into an Output of a Transaction
* @param {number} threshold
@ -165,7 +168,7 @@ export default function makeSha256Condition(preimage, json = true) {
* @param {boolean} [json=true] If true returns a json object otherwise a crypto-condition type
* @returns {object} Sha256 Threshold Condition (that will need to wrapped in an Output)
*/
export default function makeThresholdCondition(threshold, subconditions = [], json = true) {
makeThresholdCondition(threshold, subconditions = [], json = true) {
const thresholdCondition = new cc.ThresholdSha256()
thresholdCondition.threshold = threshold
@ -179,9 +182,9 @@ export default function makeThresholdCondition(threshold, subconditions = [], js
}
return thresholdCondition
}
}
/**
/**
* @public
* Generate a `TRANSFER` transaction holding the `asset`, `metadata`, and `outputs`, that fulfills
* the `fulfilledOutputs` of `unspentTransaction`.
@ -201,14 +204,14 @@ export default function makeThresholdCondition(threshold, subconditions = [], js
* @returns {object} Unsigned transaction -- make sure to call signTransaction() on it before
* sending it off!
*/
// TODO:
// - Make `metadata` optional argument
export default function makeTransferTransaction(
// TODO:
// - Make `metadata` optional argument
makeTransferTransaction(
unspentTransaction,
metadata,
outputs,
...outputIndices
) {
) {
const inputs = outputIndices.map((outputIndex) => {
const fulfilledOutput = unspentTransaction.outputs[outputIndex]
const transactionLink = {
@ -225,9 +228,9 @@ export default function makeTransferTransaction(
}
return makeTransaction('TRANSFER', assetLink, metadata, outputs, inputs)
}
}
/**
/**
* @public
* Sign the given `transaction` with the given `privateKey`s, returning a new copy of `transaction`
* that's been signed.
@ -239,7 +242,7 @@ export default function makeTransferTransaction(
* the `transaction`.
* @returns {object} The signed version of `transaction`.
*/
export default function signTransaction(transaction, ...privateKeys) {
signTransaction(transaction, ...privateKeys) {
const signedTx = clone(transaction)
signedTx.inputs.forEach((input, index) => {
const privateKey = privateKeys[index]
@ -253,4 +256,5 @@ export default function signTransaction(transaction, ...privateKeys) {
})
return signedTx
}
}