mazzaroth-js
Version:
Library that facilitates interaction with Mazzaroth nodes from both the browser and node-js
111 lines (86 loc) • 3.56 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _mazzarothXdr = require("mazzaroth-xdr");
var types = _interopRequireWildcard(_mazzarothXdr);
var _jsSha = require("js-sha3");
var sha3 = _interopRequireWildcard(_jsSha);
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
class ContractBuilder {
Contract(crypto, sender, channel, nonce, blockExpirationNumber) {
this._crypto = crypto;
this._categoryType = 0; // Default category unknown
this._sender = sender;
this._channel = channel;
this._nonce = nonce;
this._blockExpirationNumber = blockExpirationNumber;
this._owner = ''; // Default owner
this._pause = false; // Default Pause
return this;
}
Delete() {
this._categoryType = 4;
return this;
}
Deploy(owner, version, abi, contractBytes) {
this._categoryType = 2;
this._owner = owner;
this._contractBytes = contractBytes;
this._version = version;
this._abi = abi;
return this;
}
Pause(pause) {
this._categoryType = 3;
this._pause = pause;
return this;
}
Sign(privateKey) {
let action = '';
switch (this._categoryType) {
case 2:
if (this._contractBytes === undefined || this._version === undefined || this._abi === undefined) {
throw new Error('missing required fields for deploy transaction');
}
action = {
owner: this._owner,
contractBytes: this._contractBytes.toString('base64'),
contractHash: sha3.sha3_256(this._contractBytes),
version: this._version,
abi: this._abi
};
break;
case 3:
action = this._pause;
break;
case 4:
// Only care about the type, no data fields
break;
default:
// Invalid category type
throw new Error(`unknown contract category type: ${this._categoryType}`);
}
let data = {
channelID: this._channel,
nonce: this._nonce,
blockExpirationNumber: this._blockExpirationNumber,
category: {
type: this._categoryType,
data: action
}
};
let xdrData = types.Data();
xdrData.fromJSON(data);
const signer = this._crypto.NewEd25519Signer(privateKey);
const signature = signer.Sign(xdrData.toXDR('hex'));
let transaction = {
sender: this._sender,
signature: signature,
data: data
};
return transaction;
}
}
exports.default = ContractBuilder;