You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.6 KiB

5 years ago
"use strict";
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 });
const grpc = require("grpc");
const lightning_1 = require("./lightning");
const path = require("path");
const ByteBuffer = require("bytebuffer");
// var protoLoader = require('@grpc/proto-loader')
const env = process.env.NODE_ENV || 'development';
const config = require(path.join(__dirname, '../../config/app.json'))[env];
var signerClient = null;
exports.loadSigner = () => {
if (signerClient) {
return signerClient;
}
else {
try {
var credentials = lightning_1.loadCredentials();
var lnrpcDescriptor = grpc.load("signer.proto");
var signer = lnrpcDescriptor.signrpc;
signerClient = new signer.Signer(config.node_ip + ':' + config.lnd_port, credentials);
return signerClient;
}
catch (e) {
throw e;
}
}
};
exports.signMessage = (msg) => {
return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
let signer = yield exports.loadSigner();
try {
5 years ago
const options = {
msg: ByteBuffer.fromHex(msg),
key_loc: { key_family: 6, key_index: 0 },
};
5 years ago
signer.signMessage(options, function (err, sig) {
if (err || !sig.signature) {
reject(err);
}
else {
const buf = ByteBuffer.wrap(sig.signature);
resolve(buf.toBase64());
5 years ago
}
});
}
catch (e) {
reject(e);
}
}));
};
exports.signBuffer = (msg) => {
return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
let signer = yield exports.loadSigner();
try {
const options = { msg };
signer.signMessage(options, function (err, sig) {
if (err || !sig.signature) {
reject(err);
}
else {
const buf = ByteBuffer.wrap(sig.signature);
resolve(buf.toBase64());
5 years ago
}
});
}
catch (e) {
reject(e);
}
}));
};
5 years ago
function verifyMessage(msg, sig, pubkey) {
5 years ago
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
let signer = yield exports.loadSigner();
5 years ago
if (sig.length !== 96) {
return reject('invalid sig');
}
5 years ago
if (pubkey.length !== 66) {
return reject('invalid pubkey');
}
5 years ago
try {
const options = {
msg: ByteBuffer.fromHex(msg),
signature: ByteBuffer.fromBase64(sig),
5 years ago
pubkey: ByteBuffer.fromHex(pubkey),
5 years ago
};
signer.verifyMessage(options, function (err, res) {
5 years ago
if (err) {
5 years ago
reject(err);
}
else {
resolve(res);
}
});
}
catch (e) {
reject(e);
}
}));
}
function signAscii(ascii) {
return __awaiter(this, void 0, void 0, function* () {
try {
const sig = yield exports.signMessage(ascii_to_hexa(ascii));
return sig;
}
catch (e) {
throw e;
}
});
}
exports.signAscii = signAscii;
5 years ago
function verifyAscii(ascii, sig, pubkey) {
5 years ago
return __awaiter(this, void 0, void 0, function* () {
try {
5 years ago
const r = yield verifyMessage(ascii_to_hexa(ascii), sig, pubkey);
5 years ago
return r;
}
catch (e) {
throw e;
}
});
}
exports.verifyAscii = verifyAscii;
function ascii_to_hexa(str) {
var arr1 = [];
for (var n = 0, l = str.length; n < l; n++) {
var hex = Number(str.charCodeAt(n)).toString(16);
arr1.push(hex);
}
return arr1.join('');
}
//# sourceMappingURL=signer.js.map