From 7aac8f6a1872e90f75e67d5a6cbf2305ea71b8b0 Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Wed, 20 May 2020 12:01:45 -0700 Subject: [PATCH] test signer --- api/utils/lightning.ts | 2 +- api/utils/signer.ts | 114 ++++++++++++++++++ app.ts | 12 ++ dist/api/utils/signer.js | 132 ++++++++++++++++++++ dist/api/utils/signer.js.map | 1 + dist/app.js | 14 +++ dist/app.js.map | 2 +- signer.proto | 228 +++++++++++++++++++++++++++++++++++ 8 files changed, 503 insertions(+), 2 deletions(-) create mode 100644 api/utils/signer.ts create mode 100644 dist/api/utils/signer.js create mode 100644 dist/api/utils/signer.js.map create mode 100644 signer.proto diff --git a/api/utils/lightning.ts b/api/utils/lightning.ts index 731d0cd..accbf5e 100644 --- a/api/utils/lightning.ts +++ b/api/utils/lightning.ts @@ -361,7 +361,7 @@ async function checkConnection(){ return new Promise((resolve,reject)=>{ const lightning = loadLightning() lightning.getInfo({}, function(err, response) { - if (err == null) { + if (err == null) { resolve(response) } else { reject(err) diff --git a/api/utils/signer.ts b/api/utils/signer.ts new file mode 100644 index 0000000..273dcfd --- /dev/null +++ b/api/utils/signer.ts @@ -0,0 +1,114 @@ + +import * as grpc from 'grpc' +import {loadCredentials} from './lightning' +import * as path from 'path' +import * as ByteBuffer from '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; + +export const loadSigner = () => { + console.log("LOAD SIGNER RRRRRR",signerClient?true:false) + if (signerClient) { + return signerClient + } else { + console.log("LOAD SIGNER AGAIN!!!!") + try{ + var credentials = loadCredentials() + var lnrpcDescriptor = grpc.load("signer.proto"); + var signer: any = lnrpcDescriptor.signrpc + signerClient = new signer.Signer(config.node_ip + ':' + config.lnd_port, credentials); + console.log("SIGNER CLIENT",signerClient) + return signerClient + } catch(e) { + throw e + } + } +} + +export const signMessage = (msg) => { + return new Promise(async(resolve, reject)=> { + let signer = await loadSigner() + try { + const options = {msg:ByteBuffer.fromHex(msg)} + signer.signMessage(options, function(err,sig){ + if(err || !sig.signature) { + reject(err) + } else { + resolve(sig.signature) + } + }) + } catch(e) { + reject(e) + } + }) +} + +export const signBuffer = (msg) => { + return new Promise(async (resolve, reject)=> { + let signer = await loadSigner() + try { + const options = {msg} + signer.signMessage(options, function(err,sig){ + if(err || !sig.signature) { + reject(err) + } else { + resolve(sig.signature) + } + }) + } catch(e) { + reject(e) + } + }) +} + +function verifyMessage(msg,sig): Promise<{[k:string]:any}> { + return new Promise(async(resolve, reject)=> { + let signer = await loadSigner() + try { + const options = { + msg:ByteBuffer.fromHex(msg), + signature:sig, + } + signer.verifyMessage(options, function(err,res){ + if(err || !res.pubkey) { + reject(err) + } else { + resolve(res) + } + }) + } catch(e) { + reject(e) + } + }) +} + +export async function signAscii(ascii) { + try { + const sig = await signMessage(ascii_to_hexa(ascii)) + return sig + } catch(e) { + throw e + } +} + +export async function verifyAscii(ascii,sig): Promise<{[k:string]:any}>{ + try { + const r = await verifyMessage(ascii_to_hexa(ascii),sig) + return r + } catch(e) { + throw e + } +} + +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(''); +} diff --git a/app.ts b/app.ts index 804118c..3a422ba 100644 --- a/app.ts +++ b/app.ts @@ -10,6 +10,17 @@ import {pingHubInterval, checkInvitesHubInterval} from './api/hub' import {setupDatabase, setupDone} from './api/utils/setup' import * as controllers from './api/controllers' import * as socket from './api/utils/socket' +import * as signer from './api/utils/signer' + +async function testSigner(){ + try{ + await signer.loadSigner() + const sig = await signer.signAscii('asdf') + console.log("SIG", sig) + }catch(e){ + console.log(e) + } +} let server: any = null const port = process.env.PORT || 3001; @@ -44,6 +55,7 @@ async function mainSetup(){ } await setupApp() setupDone() + testSigner() } async function setupApp(){ diff --git a/dist/api/utils/signer.js b/dist/api/utils/signer.js new file mode 100644 index 0000000..2cf4124 --- /dev/null +++ b/dist/api/utils/signer.js @@ -0,0 +1,132 @@ +"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 = () => { + console.log("LOAD SIGNER RRRRRR", signerClient ? true : false); + if (signerClient) { + return signerClient; + } + else { + console.log("LOAD SIGNER AGAIN!!!!"); + 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); + console.log("SIGNER CLIENT", signerClient); + 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 { + const options = { msg: ByteBuffer.fromHex(msg) }; + signer.signMessage(options, function (err, sig) { + if (err || !sig.signature) { + reject(err); + } + else { + resolve(sig.signature); + } + }); + } + 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 { + resolve(sig.signature); + } + }); + } + catch (e) { + reject(e); + } + })); +}; +function verifyMessage(msg, sig) { + return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { + let signer = yield exports.loadSigner(); + try { + const options = { + msg: ByteBuffer.fromHex(msg), + signature: sig, + }; + signer.verifyMessage(options, function (err, res) { + if (err || !res.pubkey) { + 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; +function verifyAscii(ascii, sig) { + return __awaiter(this, void 0, void 0, function* () { + try { + const r = yield verifyMessage(ascii_to_hexa(ascii), sig); + 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 \ No newline at end of file diff --git a/dist/api/utils/signer.js.map b/dist/api/utils/signer.js.map new file mode 100644 index 0000000..ac77840 --- /dev/null +++ b/dist/api/utils/signer.js.map @@ -0,0 +1 @@ +{"version":3,"file":"signer.js","sourceRoot":"","sources":["../../../api/utils/signer.ts"],"names":[],"mappings":";;;;;;;;;;;AACA,6BAA4B;AAC5B,2CAA2C;AAC3C,6BAA4B;AAC5B,yCAAwC;AAExC,kDAAkD;AAClD,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,aAAa,CAAC;AAClD,MAAM,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAC,uBAAuB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AAEzE,IAAI,YAAY,GAAS,IAAI,CAAC;AAEjB,QAAA,UAAU,GAAG,GAAG,EAAE;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ACT,MAAM,CAAC,CAAC,CAAC,CAAA;SACV;IACH,CAAC,CAAA,CAAC,CAAA;AACJ,CAAC;AAED,SAAsB,SAAS,CAAC,KAAK;;QACnC,IAAI;YACF,MAAM,GAAG,GAAG,MAAM,mBAAW,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAA;YACnD,OAAO,GAAG,CAAA;SACX;QAAC,OAAM,CAAC,EAAE;YACT,MAAM,CAAC,CAAA;SACR;IACH,CAAC;CAAA;AAPD,8BAOC;AAED,SAAsB,WAAW,CAAC,KAAK,EAAC,GAAG;;QACzC,IAAI;YACF,MAAM,CAAC,GAAG,MAAM,aAAa,CAAC,aAAa,CAAC,KAAK,CAAC,EAAC,GAAG,CAAC,CAAA;YACvD,OAAO,CAAC,CAAA;SACT;QAAC,OAAM,CAAC,EAAE;YACT,MAAM,CAAC,CAAA;SACR;IACH,CAAC;CAAA;AAPD,kCAOC;AAED,SAAS,aAAa,CAAC,GAAG;IACzB,IAAI,IAAI,GAAc,EAAE,CAAC;IACzB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAG,EAAE;QAC5C,IAAI,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;QACjD,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;KACd;IACF,OAAO,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACtB,CAAC"} \ No newline at end of file diff --git a/dist/app.js b/dist/app.js index e52b881..61a8752 100644 --- a/dist/app.js +++ b/dist/app.js @@ -21,6 +21,19 @@ const hub_1 = require("./api/hub"); const setup_1 = require("./api/utils/setup"); const controllers = require("./api/controllers"); const socket = require("./api/utils/socket"); +const signer = require("./api/utils/signer"); +function testSigner() { + return __awaiter(this, void 0, void 0, function* () { + try { + yield signer.loadSigner(); + const sig = yield signer.signAscii('asdf'); + console.log("SIG", sig); + } + catch (e) { + console.log(e); + } + }); +} let server = null; const port = process.env.PORT || 3001; const env = process.env.NODE_ENV || 'development'; @@ -53,6 +66,7 @@ function mainSetup() { } yield setupApp(); setup_1.setupDone(); + testSigner(); }); } function setupApp() { diff --git a/dist/app.js.map b/dist/app.js.map index 57cce78..3ef6185 100644 --- a/dist/app.js.map +++ b/dist/app.js.map @@ -1 +1 @@ -{"version":3,"file":"app.js","sourceRoot":"","sources":["../app.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,mCAAkC;AAClC,0CAAyC;AACzC,iCAAgC;AAChC,8CAA6C;AAC7C,iCAAgC;AAChC,6BAA4B;AAC5B,yCAAmC;AACnC,+CAAuC;AACvC,mCAAkE;AAClE,6CAA0D;AAC1D,iDAAgD;AAChD,6CAA4C;AAE5C,IAAI,MAAM,GAAQ,IAAI,CAAA;AACtB,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,IAAI,CAAC;AACtC,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,aAAa,CAAC;AAClD,MAAM,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAErE,OAAO,CAAC,GAAG,CAAC,sBAAsB,GAAG,YAAY,CAAA;AAEjD,IAAI,CAAC,GAAG,CAAC,CAAA;AAET,eAAe;AACf,YAAY,EAAE,CAAA;AAEd,SAAe,YAAY;;QAC1B,CAAC,EAAE,CAAA;QACH,OAAO,CAAC,GAAG,CAAC,mCAAmC,CAAC,EAAE,CAAC,CAAA;QACnD,IAAI;YACH,MAAM,WAAW,CAAC,oBAAoB,EAAE,CAAA;YACxC,SAAS,EAAE,CAAA;SACX;QAAC,OAAM,CAAC,EAAE;YACV,UAAU,CAAC,GAAO,EAAE;gBACnB,MAAM,YAAY,EAAE,CAA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AAS,CAAC,GAAG,EAAE,yBAAyB,EAAE,EAAC,cAAc,EAAG,YAAY,EAAC,CAAC,CAAC;gBAC/E,GAAG,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAC;aAC/B;iBAAM;gBACN,IAAI,EAAE,CAAC;aACP;SACD;IACF,CAAC;CAAA;AAED,kBAAe,MAAM,CAAA"} \ No newline at end of file diff --git a/signer.proto b/signer.proto new file mode 100644 index 0000000..9adcdfc --- /dev/null +++ b/signer.proto @@ -0,0 +1,228 @@ +syntax = "proto3"; + +package signrpc; + +option go_package = "github.com/lightningnetwork/lnd/lnrpc/signrpc"; + +// Signer is a service that gives access to the signing functionality of the +// daemon's wallet. +service Signer { + /* + SignOutputRaw is a method that can be used to generated a signature for a + set of inputs/outputs to a transaction. Each request specifies details + concerning how the outputs should be signed, which keys they should be + signed with, and also any optional tweaks. The return value is a fixed + 64-byte signature (the same format as we use on the wire in Lightning). + + If we are unable to sign using the specified keys, then an error will be + returned. + */ + rpc SignOutputRaw (SignReq) returns (SignResp); + + /* + ComputeInputScript generates a complete InputIndex for the passed + transaction with the signature as defined within the passed SignDescriptor. + This method should be capable of generating the proper input script for + both regular p2wkh output and p2wkh outputs nested within a regular p2sh + output. + + Note that when using this method to sign inputs belonging to the wallet, + the only items of the SignDescriptor that need to be populated are pkScript + in the TxOut field, the value in that same field, and finally the input + index. + */ + rpc ComputeInputScript (SignReq) returns (InputScriptResp); + + /* + SignMessage signs a message with the key specified in the key locator. The + returned signature is fixed-size LN wire format encoded. + + The main difference to SignMessage in the main RPC is that a specific key is + used to sign the message instead of the node identity private key. + */ + rpc SignMessage (SignMessageReq) returns (SignMessageResp); + + /* + VerifyMessage verifies a signature over a message using the public key + provided. The signature must be fixed-size LN wire format encoded. + + The main difference to VerifyMessage in the main RPC is that the public key + used to sign the message does not have to be a node known to the network. + */ + rpc VerifyMessage (VerifyMessageReq) returns (VerifyMessageResp); + + /* + DeriveSharedKey returns a shared secret key by performing Diffie-Hellman key + derivation between the ephemeral public key in the request and the node's + key specified in the key_loc parameter (or the node's identity private key + if no key locator is specified): + P_shared = privKeyNode * ephemeralPubkey + The resulting shared public key is serialized in the compressed format and + hashed with sha256, resulting in the final key length of 256bit. + */ + rpc DeriveSharedKey (SharedKeyRequest) returns (SharedKeyResponse); +} + +message KeyLocator { + // The family of key being identified. + int32 key_family = 1; + + // The precise index of the key being identified. + int32 key_index = 2; +} + +message KeyDescriptor { + /* + The raw bytes of the key being identified. Either this or the KeyLocator + must be specified. + */ + bytes raw_key_bytes = 1; + + /* + The key locator that identifies which key to use for signing. Either this + or the raw bytes of the target key must be specified. + */ + KeyLocator key_loc = 2; +} + +message TxOut { + // The value of the output being spent. + int64 value = 1; + + // The script of the output being spent. + bytes pk_script = 2; +} + +message SignDescriptor { + /* + A descriptor that precisely describes *which* key to use for signing. This + may provide the raw public key directly, or require the Signer to re-derive + the key according to the populated derivation path. + */ + KeyDescriptor key_desc = 1; + + /* + A scalar value that will be added to the private key corresponding to the + above public key to obtain the private key to be used to sign this input. + This value is typically derived via the following computation: + + * derivedKey = privkey + sha256(perCommitmentPoint || pubKey) mod N + */ + bytes single_tweak = 2; + + /* + A private key that will be used in combination with its corresponding + private key to derive the private key that is to be used to sign the target + input. Within the Lightning protocol, this value is typically the + commitment secret from a previously revoked commitment transaction. This + value is in combination with two hash values, and the original private key + to derive the private key to be used when signing. + + * k = (privKey*sha256(pubKey || tweakPub) + + tweakPriv*sha256(tweakPub || pubKey)) mod N + */ + bytes double_tweak = 3; + + /* + The full script required to properly redeem the output. This field will + only be populated if a p2wsh or a p2sh output is being signed. + */ + bytes witness_script = 4; + + /* + A description of the output being spent. The value and script MUST be + provided. + */ + TxOut output = 5; + + /* + The target sighash type that should be used when generating the final + sighash, and signature. + */ + uint32 sighash = 7; + + /* + The target input within the transaction that should be signed. + */ + int32 input_index = 8; +} + +message SignReq { + // The raw bytes of the transaction to be signed. + bytes raw_tx_bytes = 1; + + // A set of sign descriptors, for each input to be signed. + repeated SignDescriptor sign_descs = 2; +} + +message SignResp { + /* + A set of signatures realized in a fixed 64-byte format ordered in ascending + input order. + */ + repeated bytes raw_sigs = 1; +} + +message InputScript { + // The serializes witness stack for the specified input. + repeated bytes witness = 1; + + /* + The optional sig script for the specified witness that will only be set if + the input specified is a nested p2sh witness program. + */ + bytes sig_script = 2; +} + +message InputScriptResp { + // The set of fully valid input scripts requested. + repeated InputScript input_scripts = 1; +} + +message SignMessageReq { + // The message to be signed. + bytes msg = 1; + + // The key locator that identifies which key to use for signing. + KeyLocator key_loc = 2; +} +message SignMessageResp { + /* + The signature for the given message in the fixed-size LN wire format. + */ + bytes signature = 1; +} + +message VerifyMessageReq { + // The message over which the signature is to be verified. + bytes msg = 1; + + /* + The fixed-size LN wire encoded signature to be verified over the given + message. + */ + bytes signature = 2; + + // The public key the signature has to be valid for. + bytes pubkey = 3; +} +message VerifyMessageResp { + // Whether the signature was valid over the given message. + bool valid = 1; +} + +message SharedKeyRequest { + // The ephemeral public key to use for the DH key derivation. + bytes ephemeral_pubkey = 1; + + /* + The optional key locator of the local key that should be used. If this + parameter is not set then the node's identity private key will be used. + */ + KeyLocator key_loc = 2; +} + +message SharedKeyResponse { + // The shared public key, hashed with sha256. + bytes shared_key = 1; +} \ No newline at end of file