This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Publishing client to sparkswap-desktop: v0.3.6
- Loading branch information
Sparkswap
committed
Jan 3, 2020
1 parent
0c0151d
commit 4965494
Showing
51 changed files
with
1,048 additions
and
83 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const SECONDS_PER_BLOCK = 600 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { LndActionOptions, GrpcError } from '../types/lnd-engine/client' | ||
import { deadline } from './deadline' | ||
import { promisify } from 'util' | ||
|
||
function alreadyConnected (err: GrpcError): boolean { | ||
return (err && err.code === 2 && err.details != null && err.details.includes('already connected to peer')) | ||
} | ||
|
||
export async function connectPeer (publicKey: string, host: string, { client, logger }: LndActionOptions): Promise<void> { | ||
const connect = promisify(client.connectPeer).bind(client) | ||
const addr = { | ||
pubkey: publicKey, | ||
host | ||
} | ||
|
||
try { | ||
await connect({ addr }, { deadline: deadline() }) | ||
} catch (e) { | ||
if (alreadyConnected(e)) { | ||
logger.debug(`Peer already connected: ${publicKey}`) | ||
return | ||
} | ||
throw e | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { | ||
LndActionOptions, | ||
OpenChannelResponse, | ||
PendingUpdateResponse, | ||
ChannelOpenResponse | ||
} from '../types/lnd-engine/client' | ||
import { SECONDS_PER_BLOCK } from './config' | ||
import { connectPeer } from './connect-peer' | ||
import { | ||
parse as parseAddress, | ||
loggablePubKey | ||
} from './utils' | ||
|
||
// Default number of seconds before our first confirmation. (30 minutes) | ||
const DEFAULT_CONFIRMATION_DELAY = 1800 | ||
|
||
interface CreateChannelOptions { | ||
targetTime?: number, | ||
privateChan?: boolean | ||
} | ||
|
||
function isPendingUpdateResponse (res: OpenChannelResponse): res is PendingUpdateResponse { | ||
return Object.keys(res).includes('chanPending') | ||
} | ||
|
||
function isChannelOpenUpdateResponse (res: OpenChannelResponse): res is ChannelOpenResponse { | ||
return Object.keys(res).includes('chanOpen') | ||
} | ||
|
||
export async function createChannel (paymentChannelNetworkAddress: string, fundingAmount: number, { | ||
targetTime = DEFAULT_CONFIRMATION_DELAY, | ||
privateChan = false | ||
}: CreateChannelOptions, | ||
{ client, logger }: LndActionOptions): Promise<void> { | ||
const targetConf = Math.max(Math.floor(targetTime / SECONDS_PER_BLOCK), 1) | ||
const { publicKey, host } = parseAddress(paymentChannelNetworkAddress) | ||
const loggablePublicKey = loggablePubKey(publicKey) | ||
|
||
logger.debug(`Attempting to create a channel with ${loggablePublicKey}`) | ||
|
||
if (host) { | ||
await connectPeer(publicKey, host, { client, logger }) | ||
} else { | ||
logger.debug(`Skipping connect peer. Host is missing for pubkey ${loggablePublicKey}`) | ||
} | ||
|
||
logger.debug(`Successfully connected to peer: ${loggablePublicKey}`) | ||
|
||
return new Promise((resolve, reject) => { | ||
try { | ||
const call = client.openChannel({ | ||
nodePubkey: Buffer.from(publicKey, 'hex'), | ||
localFundingAmount: fundingAmount, | ||
targetConf, | ||
private: privateChan | ||
}) | ||
|
||
call.on('data', data => { | ||
if (isPendingUpdateResponse(data)) { | ||
return resolve() | ||
} | ||
|
||
if (isChannelOpenUpdateResponse(data)) { | ||
return resolve() | ||
} | ||
}) | ||
|
||
call.on('error', reject) | ||
} catch (e) { | ||
reject(e) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function deadline (duration = 30): number { | ||
return new Date().setSeconds(new Date().getSeconds() + duration) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export { deadline } from './deadline' | ||
export { payInvoice } from './pay-invoice' | ||
export { SECONDS_PER_BLOCK } from './config' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { parse } from './network-address-formatter' | ||
export { loggablePubKey } from './loggable-pubkey' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Nullable } from '../../types' | ||
|
||
export function loggablePubKey (pubkey: Nullable<string>): Nullable<string> { | ||
return pubkey ? `${pubkey.slice(0, 15)}...` : null | ||
} |
17 changes: 17 additions & 0 deletions
17
src/global-shared/lnd-engine/utils/network-address-formatter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const DELIMITER = ':' | ||
const NETWORK_TYPE = 'bolt' | ||
|
||
interface ParsedAddress { | ||
publicKey: string, | ||
host?: string | ||
} | ||
|
||
export function parse (paymentChannelNetworkAddress: string): ParsedAddress { | ||
const [networkType, networkAddress] = paymentChannelNetworkAddress.split(DELIMITER, 2) | ||
if (networkType !== NETWORK_TYPE) { | ||
throw new Error(`Unable to parse address for payment channel network type of '${networkType}'`) | ||
} | ||
|
||
const [publicKey, host] = networkAddress.split('@', 2) | ||
return { publicKey, host } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function parsePhoneNumber (phone: string): string { | ||
const digits = phone.split('-').join('').split(' ').join('') | ||
return digits.startsWith('+') ? digits : '+1' + digits | ||
} | ||
|
||
export function parseSSN (ssn: string): string { | ||
const digits = ssn.split('-').join('').split(' ').join('') | ||
return [digits.slice(0, 3), digits.slice(3, 5), digits.slice(5)].join('-') | ||
} |
Oops, something went wrong.