navio-blsct
Version:
TypeScript bindings for the `libblsct` library used by the [Navio](https://nav.io/) blockchain to construct confidential transactions based on the BLS12-381 curve.
58 lines (57 loc) • 2.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Script = void 0;
const blsct_1 = require("./blsct");
const managedObj_1 = require("./managedObj");
/** Represents a script, which may be a scriptPubKey, scriptSig, or scriptWitness. Also known as `CScript` on the C++ side.
*
* A `Script` appears as an attribute of `CTxOut` (scriptPubKey) or `CTxIn` (scriptSig and scriptWitness), and is not meant to be instantiated directly.
*
* Examples:
* ```ts
* const { CTx, CTxId, TxIn, TxOut, OutPoint, Scalar, SubAddr, DoublePublicKey, TokenId, Script, CTX_ID_SIZE } = require('navio-blsct')
* const { randomBytes } = require('crypto')
* const numTxIn = 1
* const numTxOut = 1
* const defaultFee = 200000
* const fee = (numTxIn + numTxOut) * defaultFee
* const outAmount = 10000
* const inAmount = fee + outAmount
* const cTxIdHex = randomBytes(CTX_ID_SIZE).toString('hex')
* const cTxId = CTxId.deserialize(cTxIdHex)
* const outIndex = 0
* const outPoint = OutPoint.generate(cTxId, outIndex)
* const gamma = 100
* const spendingKey = Scalar.random()
* const tokenId = TokenId.default()
* const txIn = TxIn.generate(inAmount, gamma, spendingKey, tokenId, outPoint)
* const subAddr = SubAddr.fromDoublePublicKey(DoublePublicKey.random())
* const txOut = TxOut.generate(subAddr, outAmount, 'navio')
* const cTx = CTx.generate([txIn], [txOut])
* const cTxOuts = cTx.getCTxOuts()
* const scriptPubKey = cTxOuts[0].getScriptPubKey()
* const ser = scriptPubKey.serialize()
* const deser = Script.deserialize(ser)
* deser.serialize() === ser // true
* ```
*/
class Script extends managedObj_1.ManagedObj {
constructor(obj) {
super(obj);
}
value() {
return (0, blsct_1.castToScript)(this.obj);
}
serialize() {
return (0, blsct_1.serializeScript)(this.value());
}
/** Deserializes a `Script` from its hexadecimal representation.
*
* @param hex - The hexadecimal string to deserialize.
* @returns A new `Script` instance.
*/
static deserialize(hex) {
return Script._deserialize(hex, blsct_1.deserializeScript);
}
}
exports.Script = Script;