mirror of
https://github.com/bigchaindb/js-bigchaindb-driver.git
synced 2024-11-22 09:46:58 +01:00
Made class for Transaction file
This commit is contained in:
parent
aba3c7d4bc
commit
61179c167b
@ -6,13 +6,16 @@ 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)
|
||||
@ -21,7 +24,7 @@ export default function serializeTransactionIntoCanonicalString(transaction) {
|
||||
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,
|
||||
@ -29,7 +32,7 @@ export default function makeInputTemplate(publicKeys = [], fulfills = null, fulf
|
||||
}
|
||||
}
|
||||
|
||||
export default function hashTransaction(transaction) {
|
||||
hashTransaction(transaction) {
|
||||
// Safely remove any tx id from the given transaction for hashing
|
||||
const tx = { ...transaction }
|
||||
delete tx.id
|
||||
@ -37,7 +40,7 @@ export default function hashTransaction(transaction) {
|
||||
return sha256Hash(serializeTransactionIntoCanonicalString(tx))
|
||||
}
|
||||
|
||||
function makeTransactionTemplate() {
|
||||
makeTransactionTemplate() {
|
||||
return {
|
||||
'id': null,
|
||||
'operation': null,
|
||||
@ -49,7 +52,7 @@ function makeTransactionTemplate() {
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
@ -81,7 +84,7 @@ 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,
|
||||
}
|
||||
@ -97,7 +100,7 @@ export default function makeCreateTransaction(asset, metadata, outputs, ...issue
|
||||
* @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()
|
||||
@ -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')
|
||||
}
|
||||
@ -147,7 +150,7 @@ export default function makeOutput(condition, amount = '1') {
|
||||
* @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)
|
||||
|
||||
@ -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
|
||||
|
||||
@ -203,7 +206,7 @@ export default function makeThresholdCondition(threshold, subconditions = [], js
|
||||
*/
|
||||
// TODO:
|
||||
// - Make `metadata` optional argument
|
||||
export default function makeTransferTransaction(
|
||||
makeTransferTransaction(
|
||||
unspentTransaction,
|
||||
metadata,
|
||||
outputs,
|
||||
@ -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]
|
||||
@ -254,3 +257,4 @@ export default function signTransaction(transaction, ...privateKeys) {
|
||||
|
||||
return signedTx
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user