Skip to content

Commit

Permalink
Merge pull request #6 from stakwork/signer
Browse files Browse the repository at this point in the history
Signer
  • Loading branch information
Evanfeenstra committed Apr 8, 2020
2 parents 119bc38 + e153c1b commit 1dfd4e8
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 38 deletions.
32 changes: 27 additions & 5 deletions api/grpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,33 @@ import * as socket from '../utils/socket'
import { sendNotification, sendInvoice } from '../hub'
import * as jsonUtils from '../utils/json'
import * as decodeUtils from '../utils/decode'
import {loadLightning, SPHINX_CUSTOM_RECORD_KEY} from '../utils/lightning'
import {loadLightning, SPHINX_CUSTOM_RECORD_KEY, verifyAscii} from '../utils/lightning'

const constants = require(__dirname + '/../../config/constants.json');

function parseKeysendInvoice(i, actions){
// VERIFY PUBKEY OF SENDER
async function parseAndVerifyPayload(data){
let payload
const li = data.lastIndexOf('}')
const msg = data.substring(0,li+1)
const sig = data.substring(li+1)
try {
payload = JSON.parse(msg)
if(payload) {
if(!sig) return payload // REMOVE THIS LINE (here for backward compat)
const v = await verifyAscii(msg, sig)
if(v && v.valid && v.pubkey) {
payload.sender = payload.sender||{}
payload.sender.pub_key=v.pubkey
return payload
}
}
} catch(e) {
return null
}
}

async function parseKeysendInvoice(i, actions){
const recs = i.htlcs && i.htlcs[0] && i.htlcs[0].custom_records
const buf = recs && recs[SPHINX_CUSTOM_RECORD_KEY]
const data = buf && buf.toString()
Expand All @@ -17,16 +39,16 @@ function parseKeysendInvoice(i, actions){
let payload
if(data[0]==='{'){
try {
payload = JSON.parse(data)
payload = await parseAndVerifyPayload(data)
} catch(e){}
} else {
const threads = weave(data)
if(threads) payload = JSON.parse(threads)
if(threads) payload = await parseAndVerifyPayload(threads)
}
if(payload){
const dat = payload.content || payload
if(value && dat && dat.message){
dat.message.amount = value
dat.message.amount = value // ADD IN TRUE VALUE
}
if(actions[payload.type]) {
actions[payload.type](payload)
Expand Down
18 changes: 16 additions & 2 deletions api/utils/lightning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,16 @@ const keysend = (opts) => {
})
}

const MAX_MSG_LENGTH = 972 // 1146 - 20
const MAX_MSG_LENGTH = 868 // 1146 - 20 ??? - 104 for sig
async function keysendMessage(opts) {
return new Promise(async function(resolve, reject) {
if(!opts.data || typeof opts.data!=='string') {
return reject('string plz')
}
// SIGN HERE and append sig
// const sig = await signAscii(opts.data)
// opts.data = opts.data + sig

if(opts.data.length<MAX_MSG_LENGTH){
try {
const res = await keysend(opts)
Expand Down Expand Up @@ -191,6 +195,15 @@ async function signAscii(ascii) {
}
}

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 listInvoices() {
return new Promise(async(resolve, reject)=> {
const lightning = await loadLightning()
Expand Down Expand Up @@ -256,7 +269,7 @@ const signBuffer = (msg) => {
})
}

const verifyMessage = (msg,sig) => {
function verifyMessage(msg,sig): Promise<{[k:string]:any}> {
return new Promise(async(resolve, reject)=> {
let lightning = await loadLightning()
try {
Expand Down Expand Up @@ -311,6 +324,7 @@ export {
keysendMessage,
signMessage,
verifyMessage,
verifyAscii,
signAscii,
signBuffer,
LND_KEYSEND_KEY,
Expand Down
77 changes: 52 additions & 25 deletions dist/api/grpc/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/api/grpc/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 19 additions & 4 deletions dist/api/utils/lightning.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1dfd4e8

Please sign in to comment.