sensible-sdk
Version:
Sensible-SDK
117 lines (116 loc) • 4.24 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SatotxSigner = void 0;
const BN = require("../bn.js");
const net_1 = require("../net");
function fixApiPrefix(api) {
api = api.split(",")[0];
if (api[api.length - 1] == "/") {
api = api.slice(0, api.length - 1);
}
return api;
}
/**
* 签名器API
* https://github.com/sensible-contract/satotx
*/
class SatotxSigner {
constructor(satotxApiPrefix, satotxPubKey) {
if (satotxApiPrefix) {
satotxApiPrefix = fixApiPrefix(satotxApiPrefix);
}
this.satotxApiPrefix = satotxApiPrefix;
if (satotxPubKey) {
this.satotxPubKey = BN.fromString(satotxPubKey, 16);
}
}
getInfo() {
return __awaiter(this, void 0, void 0, function* () {
let _res = yield net_1.Net.httpGet(`${this.satotxApiPrefix}`, {}, {
headers: {
"Accept-Encoding": "gzip",
},
});
const { code, msg, data } = _res;
if (code != 0) {
throw msg;
}
return data;
});
}
/**
* @param {Object} satotxData
* @param {number} satotxData.index utxo的vout
* @param {Sha256} satotxData.txId 产生utxo的txid
* @param {String} satotxData.txHex 产生utxo的rawtx
* @param {Sha256} satotxData.byTxId 花费此utxo的txid
* @param {String} satotxData.byTxHex 花费此utxo的rawtx
*/
satoTxSigUTXOSpendBy({ index, txId, txHex, byTxId, byTxHex, }) {
return __awaiter(this, void 0, void 0, function* () {
let _res = yield net_1.Net.httpPost(`${this.satotxApiPrefix}/utxo-spend-by/${txId}/${index}/${byTxId}`, {
txHex: txHex,
byTxHex: byTxHex,
}, {
headers: {
"Accept-Encoding": "gzip",
},
});
const { code, msg, data } = _res;
if (code != 0) {
throw msg;
}
return data;
});
}
/**
* @param {Object} satotxData
* @param {number} satotxData.index utxo的vout
* @param {Sha256} satotxData.txId 产生utxo的txid
* @param {String} satotxData.txHex 产生utxo的rawtx
*/
satoTxSigUTXO({ index, txId, txHex, }) {
return __awaiter(this, void 0, void 0, function* () {
let _res = yield net_1.Net.httpPost(`${this.satotxApiPrefix}/utxo/${txId}/${index}`, {
txHex: txHex,
}, {
headers: {
"Accept-Encoding": "gzip",
},
});
const { code, msg, data } = _res;
if (code != 0) {
throw msg;
}
return data;
});
}
satoTxSigUTXOSpendByUTXO({ index, txId, txHex, byTxIndex, byTxId, byTxHex, }) {
return __awaiter(this, void 0, void 0, function* () {
let _res = yield net_1.Net.httpPost(`${this.satotxApiPrefix}/utxo-spend-by-utxo/${txId}/${index}/${byTxId}/${byTxIndex}`, {
txHex: txHex,
byTxHex: byTxHex,
}, {
headers: {
"Accept-Encoding": "gzip",
"Content-Encoding": "gzip",
},
});
const { code, msg, data } = _res;
if (code != 0) {
throw msg;
}
return data;
});
}
}
exports.SatotxSigner = SatotxSigner;
;