mirror of
https://github.com/bigchaindb/js-bigchaindb-driver.git
synced 2024-11-22 09:46:58 +01:00
static methods
This commit is contained in:
parent
4dd006b8cb
commit
3725afa4a0
@ -1,4 +1,6 @@
|
|||||||
export Ed25519Keypair from './Ed25519Keypair'
|
export Ed25519Keypair from './Ed25519Keypair'
|
||||||
|
|
||||||
export Transaction from './transaction'
|
|
||||||
export Connection from './connection'
|
export Connection from './connection'
|
||||||
|
export Transaction from './transaction'
|
||||||
|
export ccJsonLoad from './utils/ccJsonLoad'
|
||||||
|
export ccJsonify from './utils/ccJsonify'
|
||||||
|
@ -13,10 +13,10 @@ export default class Transaction {
|
|||||||
* @param {object} (transaction)
|
* @param {object} (transaction)
|
||||||
* @return {string} a canonically serialized Transaction
|
* @return {string} a canonically serialized Transaction
|
||||||
*/
|
*/
|
||||||
serializeTransactionIntoCanonicalString(transaction) {
|
static 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 = this.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))
|
||||||
@ -30,12 +30,12 @@ export default class Transaction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
hashTransaction(transaction) {
|
static 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(this.serializeTransactionIntoCanonicalString(tx))
|
return sha256Hash(Transaction.serializeTransactionIntoCanonicalString(tx))
|
||||||
}
|
}
|
||||||
|
|
||||||
static makeTransactionTemplate() {
|
static makeTransactionTemplate() {
|
||||||
@ -51,8 +51,8 @@ export default class Transaction {
|
|||||||
return txTemplate
|
return txTemplate
|
||||||
}
|
}
|
||||||
|
|
||||||
makeTransaction(operation, asset, metadata = null, outputs = [], inputs = []) {
|
static makeTransaction(operation, asset, metadata = null, outputs = [], inputs = []) {
|
||||||
const tx = this.makeTransactionTemplate()
|
const tx = Transaction.makeTransactionTemplate()
|
||||||
tx.operation = operation
|
tx.operation = operation
|
||||||
tx.asset = asset
|
tx.asset = asset
|
||||||
tx.metadata = metadata
|
tx.metadata = metadata
|
||||||
@ -60,7 +60,7 @@ export default class Transaction {
|
|||||||
tx.outputs = outputs
|
tx.outputs = outputs
|
||||||
|
|
||||||
// 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 = this.hashTransaction(tx)
|
tx.id = Transaction.hashTransaction(tx)
|
||||||
return tx
|
return tx
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,13 +83,13 @@ export default class Transaction {
|
|||||||
* @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!
|
||||||
*/
|
*/
|
||||||
makeCreateTransaction(asset, metadata, outputs, ...issuers) {
|
static makeCreateTransaction(asset, metadata, outputs, ...issuers) {
|
||||||
const assetDefinition = {
|
const assetDefinition = {
|
||||||
'data': asset || null,
|
'data': asset || null,
|
||||||
}
|
}
|
||||||
const inputs = issuers.map((issuer) => this.makeInputTemplate([issuer]))
|
const inputs = issuers.map((issuer) => Transaction.makeInputTemplate([issuer]))
|
||||||
|
|
||||||
return this.makeTransaction('CREATE', assetDefinition, metadata, outputs, inputs)
|
return Transaction.makeTransaction('CREATE', assetDefinition, metadata, outputs, inputs)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -100,8 +100,8 @@ export default class 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)
|
||||||
*/
|
*/
|
||||||
makeEd25519Condition(publicKey, json = true) {
|
static makeEd25519Condition(publicKey, json = true) {
|
||||||
const publicKeyBuffer = Buffer.from(this.base58.decode(publicKey))
|
const publicKeyBuffer = Buffer.from(base58.decode(publicKey))
|
||||||
|
|
||||||
const ed25519Fulfillment = new cc.Ed25519Sha256()
|
const ed25519Fulfillment = new cc.Ed25519Sha256()
|
||||||
ed25519Fulfillment.setPublicKey(publicKeyBuffer)
|
ed25519Fulfillment.setPublicKey(publicKeyBuffer)
|
||||||
@ -122,7 +122,7 @@ export default class Transaction {
|
|||||||
* @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
|
||||||
*/
|
*/
|
||||||
makeOutput(condition, amount = '1') {
|
static 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')
|
||||||
}
|
}
|
||||||
@ -136,7 +136,7 @@ export default class Transaction {
|
|||||||
details.subconditions.map(getPublicKeys)
|
details.subconditions.map(getPublicKeys)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.getPublicKeys(condition.details)
|
getPublicKeys(condition.details)
|
||||||
return {
|
return {
|
||||||
condition,
|
condition,
|
||||||
'amount': amount,
|
'amount': amount,
|
||||||
@ -151,12 +151,12 @@ export default class 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} Preimage-Sha256 Condition (that will need to wrapped in an Output)
|
* @returns {object} Preimage-Sha256 Condition (that will need to wrapped in an Output)
|
||||||
*/
|
*/
|
||||||
makeSha256Condition(preimage, json = true) {
|
static makeSha256Condition(preimage, json = true) {
|
||||||
const sha256Fulfillment = new cc.PreimageSha256()
|
const sha256Fulfillment = new cc.PreimageSha256()
|
||||||
sha256Fulfillment.preimage = Buffer.from(preimage)
|
sha256Fulfillment.preimage = Buffer.from(preimage)
|
||||||
|
|
||||||
if (json) {
|
if (json) {
|
||||||
return this.ccJsonify(sha256Fulfillment)
|
return ccJsonify(sha256Fulfillment)
|
||||||
}
|
}
|
||||||
return sha256Fulfillment
|
return sha256Fulfillment
|
||||||
}
|
}
|
||||||
@ -169,7 +169,7 @@ export default class 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} Sha256 Threshold Condition (that will need to wrapped in an Output)
|
* @returns {object} Sha256 Threshold Condition (that will need to wrapped in an Output)
|
||||||
*/
|
*/
|
||||||
makeThresholdCondition(threshold, subconditions = [], json = true) {
|
static makeThresholdCondition(threshold, subconditions = [], json = true) {
|
||||||
const thresholdCondition = new cc.ThresholdSha256()
|
const thresholdCondition = new cc.ThresholdSha256()
|
||||||
thresholdCondition.threshold = threshold
|
thresholdCondition.threshold = threshold
|
||||||
|
|
||||||
@ -179,7 +179,7 @@ export default class Transaction {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (json) {
|
if (json) {
|
||||||
return this.ccJsonify(thresholdCondition)
|
return ccJsonify(thresholdCondition)
|
||||||
}
|
}
|
||||||
|
|
||||||
return thresholdCondition
|
return thresholdCondition
|
||||||
@ -207,7 +207,7 @@ export default class Transaction {
|
|||||||
*/
|
*/
|
||||||
// TODO:
|
// TODO:
|
||||||
// - Make `metadata` optional argument
|
// - Make `metadata` optional argument
|
||||||
makeTransferTransaction(
|
static makeTransferTransaction(
|
||||||
unspentOutputs,
|
unspentOutputs,
|
||||||
outputs,
|
outputs,
|
||||||
metadata
|
metadata
|
||||||
@ -228,7 +228,7 @@ export default class Transaction {
|
|||||||
: unspentOutputs[0].tx.asset.id
|
: unspentOutputs[0].tx.asset.id
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.makeTransaction('TRANSFER', assetLink, metadata, outputs, inputs)
|
return Transaction.makeTransaction('TRANSFER', assetLink, metadata, outputs, inputs)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -243,12 +243,13 @@ export default class Transaction {
|
|||||||
* the `transaction`.
|
* the `transaction`.
|
||||||
* @returns {object} The signed version of `transaction`.
|
* @returns {object} The signed version of `transaction`.
|
||||||
*/
|
*/
|
||||||
signTransaction(transaction, ...privateKeys) {
|
static 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]
|
||||||
const privateKeyBuffer = Buffer.from(base58.decode(privateKey))
|
const privateKeyBuffer = Buffer.from(base58.decode(privateKey))
|
||||||
const serializedTransaction = this.serializeTransactionIntoCanonicalString(transaction)
|
const serializedTransaction = Transaction
|
||||||
|
.serializeTransactionIntoCanonicalString(transaction)
|
||||||
const ed25519Fulfillment = new cc.Ed25519Sha256()
|
const ed25519Fulfillment = new cc.Ed25519Sha256()
|
||||||
ed25519Fulfillment.sign(Buffer.from(serializedTransaction), privateKeyBuffer)
|
ed25519Fulfillment.sign(Buffer.from(serializedTransaction), privateKeyBuffer)
|
||||||
const fulfillmentUri = ed25519Fulfillment.serializeUri()
|
const fulfillmentUri = ed25519Fulfillment.serializeUri()
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import test from 'ava'
|
import test from 'ava'
|
||||||
import cc from 'crypto-conditions'
|
import cc from 'crypto-conditions'
|
||||||
import { Ed25519Keypair, Transaction } from '../../src'
|
import { Ed25519Keypair, Transaction, ccJsonLoad } from '../../src'
|
||||||
|
|
||||||
test('Ed25519 condition encoding', t => {
|
test('Ed25519 condition encoding', t => {
|
||||||
const publicKey = '4zvwRjXUKGfvwnParsHAS3HuSVzV5cA4McphgmoCtajS'
|
const publicKey = '4zvwRjXUKGfvwnParsHAS3HuSVzV5cA4McphgmoCtajS'
|
||||||
@ -68,7 +68,7 @@ test('Fulfillment correctly formed', t => {
|
|||||||
|
|
||||||
|
|
||||||
test('CryptoConditions JSON load', t => {
|
test('CryptoConditions JSON load', t => {
|
||||||
const cond = Transaction.ccJsonLoad({
|
const cond = ccJsonLoad({
|
||||||
type: 'threshold-sha-256',
|
type: 'threshold-sha-256',
|
||||||
threshold: 1,
|
threshold: 1,
|
||||||
subconditions: [{
|
subconditions: [{
|
||||||
@ -84,7 +84,7 @@ test('CryptoConditions JSON load', t => {
|
|||||||
|
|
||||||
|
|
||||||
test('CryptoConditions JSON load', t => {
|
test('CryptoConditions JSON load', t => {
|
||||||
const cond = Transaction.ccJsonLoad({
|
const cond = ccJsonLoad({
|
||||||
type: 'threshold-sha-256',
|
type: 'threshold-sha-256',
|
||||||
threshold: 1,
|
threshold: 1,
|
||||||
subconditions: [{
|
subconditions: [{
|
||||||
|
Loading…
Reference in New Issue
Block a user