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