mirror of
https://github.com/bigchaindb/js-bigchaindb-driver.git
synced 2024-11-22 17:50:09 +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 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)
|
||||||
@ -21,7 +24,7 @@ export default function serializeTransactionIntoCanonicalString(transaction) {
|
|||||||
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,
|
||||||
@ -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
|
// Safely remove any tx id from the given transaction for hashing
|
||||||
const tx = { ...transaction }
|
const tx = { ...transaction }
|
||||||
delete tx.id
|
delete tx.id
|
||||||
@ -37,7 +40,7 @@ export default function hashTransaction(transaction) {
|
|||||||
return sha256Hash(serializeTransactionIntoCanonicalString(tx))
|
return sha256Hash(serializeTransactionIntoCanonicalString(tx))
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeTransactionTemplate() {
|
makeTransactionTemplate() {
|
||||||
return {
|
return {
|
||||||
'id': null,
|
'id': null,
|
||||||
'operation': 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()
|
const tx = makeTransactionTemplate()
|
||||||
tx.operation = operation
|
tx.operation = operation
|
||||||
tx.asset = asset
|
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
|
* @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,
|
||||||
}
|
}
|
||||||
@ -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
|
* @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()
|
||||||
@ -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')
|
||||||
}
|
}
|
||||||
@ -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
|
* @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)
|
||||||
|
|
||||||
@ -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
|
||||||
|
|
||||||
@ -203,7 +206,7 @@ export default function makeThresholdCondition(threshold, subconditions = [], js
|
|||||||
*/
|
*/
|
||||||
// TODO:
|
// TODO:
|
||||||
// - Make `metadata` optional argument
|
// - Make `metadata` optional argument
|
||||||
export default function makeTransferTransaction(
|
makeTransferTransaction(
|
||||||
unspentTransaction,
|
unspentTransaction,
|
||||||
metadata,
|
metadata,
|
||||||
outputs,
|
outputs,
|
||||||
@ -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]
|
||||||
@ -254,3 +257,4 @@ export default function signTransaction(transaction, ...privateKeys) {
|
|||||||
|
|
||||||
return signedTx
|
return signedTx
|
||||||
}
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user