-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a0a509
commit b5137ac
Showing
15 changed files
with
362 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ package-lock.json | |
|
||
# production | ||
/build | ||
dist | ||
|
||
# misc | ||
.DS_Store | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
|
||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
|
||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
|
||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
|
||
var _bigchaindbDriver = require('bigchaindb-driver'); | ||
|
||
var driver = _interopRequireWildcard(_bigchaindbDriver); | ||
|
||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
|
||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
|
||
// eslint-disable-line import/no-namespace | ||
|
||
var Connection = function () { | ||
function Connection(path) { | ||
var headers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
|
||
_classCallCheck(this, Connection); | ||
|
||
this.path = path; | ||
this.headers = _extends({}, headers); | ||
this.conn = new driver.Connection(path, headers); | ||
} | ||
|
||
_createClass(Connection, [{ | ||
key: 'getAssetId', | ||
value: function getAssetId(tx) { | ||
// eslint-disable-line class-methods-use-this | ||
return tx.operation === 'CREATE' ? tx.id : tx.asset.id; | ||
} | ||
}, { | ||
key: 'getTransaction', | ||
value: function getTransaction(transactionId) { | ||
return this.conn.getTransaction(transactionId); | ||
} | ||
}, { | ||
key: 'listTransactions', | ||
value: function listTransactions(assetId, operation) { | ||
return this.conn.listTransactions(assetId, operation); | ||
} | ||
}, { | ||
key: 'listOutputs', | ||
value: function listOutputs(publicKey, spent) { | ||
return this.conn.listOutputs(publicKey, spent); | ||
} | ||
}, { | ||
key: 'getBlock', | ||
value: function getBlock(blockId) { | ||
return this.conn.getBlock(blockId); | ||
} | ||
}, { | ||
key: 'listBlocks', | ||
value: function listBlocks(transactionId) { | ||
var _this = this; | ||
|
||
return this.conn.listBlocks(transactionId).then(function (blockIds) { | ||
return Promise.all(blockIds.map(function (blockId) { | ||
return _this.conn.getBlock(blockId); | ||
})); | ||
}); | ||
} | ||
}, { | ||
key: 'listVotes', | ||
value: function listVotes(blockId) { | ||
return this.conn.listVotes(blockId); | ||
} | ||
}, { | ||
key: 'searchAssets', | ||
value: function searchAssets(text) { | ||
return this.conn.searchAssets(text); | ||
} | ||
}, { | ||
key: 'createTransaction', | ||
value: function createTransaction(publicKey, privateKey, payload, metadata) { | ||
var _this2 = this; | ||
|
||
try { | ||
// Create a transation | ||
var tx = driver.Transaction.makeCreateTransaction(payload, metadata, [driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(publicKey))], publicKey); | ||
|
||
// sign/fulfill the transaction | ||
var txSigned = driver.Transaction.signTransaction(tx, privateKey); | ||
|
||
// send it off to BigchainDB | ||
return this.conn.postTransaction(txSigned).then(function () { | ||
return _this2.conn.pollStatusAndFetchTransaction(txSigned.id); | ||
}).then(function () { | ||
return txSigned; | ||
}); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
} | ||
}, { | ||
key: 'transferTransaction', | ||
value: function transferTransaction(tx, fromPublicKey, fromPrivateKey, toPublicKey, metadata) { | ||
var _this3 = this; | ||
|
||
try { | ||
var txTransfer = driver.Transaction.makeTransferTransaction(tx, metadata, [driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(toPublicKey))], 0); | ||
|
||
var txTransferSigned = driver.Transaction.signTransaction(txTransfer, fromPrivateKey); | ||
// send it off to BigchainDB | ||
return this.conn.postTransaction(txTransferSigned).then(function () { | ||
return _this3.conn.pollStatusAndFetchTransaction(txTransferSigned.id); | ||
}).then(function () { | ||
return txTransferSigned; | ||
}); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
} | ||
}, { | ||
key: 'getSortedTransactions', | ||
value: function getSortedTransactions(assetId) { | ||
return this.conn.listTransactions(assetId).then(function (txList) { | ||
if (txList.length <= 1) { | ||
return txList; | ||
} | ||
var inputTransactions = []; | ||
txList.forEach(function (tx) { | ||
return tx.inputs.forEach(function (input) { | ||
if (input.fulfills) { | ||
inputTransactions.push(input.fulfills.transaction_id); | ||
} | ||
}); | ||
}); | ||
var unspents = txList.filter(function (tx) { | ||
return inputTransactions.indexOf(tx.id) === -1; | ||
}); | ||
if (unspents.length) { | ||
var _ret = function () { | ||
var tipTransaction = unspents[0]; | ||
var tipTransactionId = tipTransaction.inputs[0].fulfills.transaction_id; | ||
var sortedTxList = []; | ||
while (true) { | ||
// eslint-disable-line no-constant-condition | ||
sortedTxList.push(tipTransaction); | ||
try { | ||
tipTransactionId = tipTransaction.inputs[0].fulfills.transaction_id; | ||
} catch (e) { | ||
break; | ||
} | ||
if (!tipTransactionId) { | ||
break; | ||
} | ||
tipTransaction = txList.filter(function (tx) { | ||
return (// eslint-disable-line no-loop-func | ||
tx.id === tipTransactionId | ||
); | ||
})[0]; | ||
} | ||
return { | ||
v: sortedTxList.reverse() | ||
}; | ||
}(); | ||
|
||
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v; | ||
} else { | ||
console.error('something went wrong while sorting transactions', txList, inputTransactions); | ||
} | ||
return txList; | ||
}); | ||
} | ||
}]); | ||
|
||
return Connection; | ||
}(); | ||
|
||
exports.default = Connection; | ||
module.exports = exports['default']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
|
||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
|
||
var _connection = require('./connection'); | ||
|
||
var _connection2 = _interopRequireDefault(_connection); | ||
|
||
var _ormobject = require('./ormobject'); | ||
|
||
var _ormobject2 = _interopRequireDefault(_ormobject); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
|
||
var Orm = function () { | ||
function Orm(connectionUrl, headers) { | ||
_classCallCheck(this, Orm); | ||
|
||
this.connection = new _connection2.default(connectionUrl, headers); | ||
this.appId = ''; | ||
if (headers && headers.app_id) { | ||
this.appId = headers.app_id; | ||
} | ||
} | ||
|
||
_createClass(Orm, [{ | ||
key: 'define', | ||
value: function define(modelName, modelSchema) { | ||
this[modelName] = new _ormobject2.default(modelName, modelSchema, this.connection, this.appId); | ||
} | ||
}]); | ||
|
||
return Orm; | ||
}(); | ||
|
||
exports.default = Orm; | ||
module.exports = exports['default']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
|
||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
|
||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
|
||
var _v = require('uuid/v4'); | ||
|
||
var _v2 = _interopRequireDefault(_v); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
|
||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
|
||
// The likelihood to generate a vanity address that is 11 times "Burn" is extremely low: | ||
// - https://en.bitcoin.it/wiki/Vanitygen#Use_of_vanitygen_to_try_to_attack_addresses | ||
var BURN_ADDRESS = 'BurnBurnBurnBurnBurnBurnBurnBurnBurnBurnBurn'; | ||
|
||
var OrmObject = function () { | ||
function OrmObject(modelName, modelSchema, connection) { | ||
var appId = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ''; | ||
var transactionList = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : []; | ||
|
||
_classCallCheck(this, OrmObject); | ||
|
||
this._name = modelName; | ||
this._schema = modelSchema; | ||
this._connection = connection; | ||
this._appId = appId; | ||
if (transactionList.length) { | ||
this.transactionHistory = transactionList; | ||
this.id = transactionList[0].asset.data[this._appId + '-' + this._name].id; | ||
this.data = _extends.apply(undefined, [{}].concat(_toConsumableArray(transactionList.map(function (tx) { | ||
return tx.metadata; | ||
})))); | ||
} | ||
} | ||
|
||
_createClass(OrmObject, [{ | ||
key: 'retrieve', | ||
value: function retrieve(input) { | ||
var _this = this; | ||
|
||
var query = input || '"' + this._appId + '-' + this._name + '"'; | ||
return this._connection.searchAssets('"' + query + '"').then(function (assets) { | ||
return Promise.all(assets.map(function (asset) { | ||
return _this._connection.getSortedTransactions(asset.id).then(function (txList) { | ||
return new OrmObject(_this._name, _this._schema, _this._connection, _this._appId, txList); | ||
}); | ||
})); | ||
}); | ||
} | ||
}, { | ||
key: 'create', | ||
value: function create(inputs) { | ||
var _this2 = this; | ||
|
||
if (inputs === undefined) { | ||
console.error('inputs missing'); | ||
} | ||
var assetPayload = {}; | ||
assetPayload[this._appId + '-' + this._name] = { | ||
'schema': this._schema, | ||
'id': 'id:' + this._appId + ':' + (0, _v2.default)() | ||
}; | ||
return this._connection.createTransaction(inputs.keypair.publicKey, inputs.keypair.privateKey, assetPayload, inputs.data).then(function (tx) { | ||
return Promise.resolve(_this2._connection.getSortedTransactions(tx.id).then(function (txList) { | ||
return new OrmObject(_this2._name, _this2._schema, _this2._connection, _this2._appId, txList); | ||
})); | ||
}); | ||
} | ||
}, { | ||
key: 'append', | ||
value: function append(inputs) { | ||
var _this3 = this; | ||
|
||
if (inputs === undefined) { | ||
console.error('inputs missing'); | ||
} | ||
return this._connection.transferTransaction(this.transactionHistory[this.transactionHistory.length - 1], inputs.keypair.publicKey, inputs.keypair.privateKey, inputs.toPublicKey, inputs.data).then(function () { | ||
return Promise.resolve(_this3._connection.getSortedTransactions(_this3.transactionHistory[0].id).then(function (txList) { | ||
return new OrmObject(_this3._name, _this3._schema, _this3._connection, _this3._appId, txList); | ||
})); | ||
}); | ||
} | ||
}, { | ||
key: 'burn', | ||
value: function burn(inputs) { | ||
var _this4 = this; | ||
|
||
if (inputs === undefined) { | ||
console.error('inputs missing'); | ||
} | ||
|
||
return this._connection.transferTransaction(this.transactionHistory[this.transactionHistory.length - 1], inputs.keypair.publicKey, inputs.keypair.privateKey, BURN_ADDRESS, { status: 'BURNED' }).then(function () { | ||
return Promise.resolve(_this4._connection.getSortedTransactions(_this4.transactionHistory[0].id).then(function (txList) { | ||
return new OrmObject(_this4._name, _this4._schema, _this4._connection, _this4._appId, txList); | ||
})); | ||
}); | ||
} | ||
}]); | ||
|
||
return OrmObject; | ||
}(); | ||
|
||
exports.default = OrmObject; | ||
module.exports = exports['default']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters