Skip to content

Commit

Permalink
fix: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kincaidoneil committed Mar 8, 2019
1 parent ff3586f commit 537691d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/__tests__/disconnect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ test.beforeEach(async t => {
t.context = await connect(process.env.LEDGER_ENV! as LedgerEnv)
})

test('after connect', async t => {
await t.notThrowsAsync(t.context.disconnect())
})

test('after add eth', async t => {
const uplink = await addEth()(t.context)
await addEth()(t.context)
await t.notThrowsAsync(t.context.disconnect())
})

Expand Down
15 changes: 9 additions & 6 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export const serializeConfig = (state: State) =>
credentials: state.credentials.map(credentialToConfig)
})

export const persistConfig = async (fd: number, state: State) => {
await promisify(ftruncate)(fd) // r+ mode doesn't overwrite, so first delete file contents
await promisify(writeFile)(fd, serializeConfig(state))
export const persistConfig = async (fileDescriptor: number, state: State) => {
await promisify(ftruncate)(fileDescriptor) // r+ mode doesn't overwrite, so first delete file contents
await promisify(writeFile)(fileDescriptor, serializeConfig(state))
}

/**
Expand All @@ -46,20 +46,23 @@ export const loadConfig = async (): Promise<
else throw err
})

const fd = await promisify(open)(CONFIG_PATH, 'r+').catch(err => {
const fileDescriptor = await promisify(open)(CONFIG_PATH, 'r+').catch(err => {
if (err.code === 'ENOENT') {
return promisify(open)(CONFIG_PATH, 'w+')
} else {
throw err
}
})

const content = await promisify(readFile)(fd, {
const content = await promisify(readFile)(fileDescriptor, {
encoding: 'utf8'
})

// TODO Add *robust* schema validation
return [fd, content.length === 0 ? undefined : JSON.parse(content)]
return [
fileDescriptor,
content.length === 0 ? undefined : JSON.parse(content)
]
}

/**
Expand Down
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface State {
}

export const connect = async (ledgerEnv: LedgerEnv = LedgerEnv.Testnet) => {
const [fd, config] = await loadConfig()
const [fileDescriptor, config] = await loadConfig()

// TODO Make sure the config has the right ledgerEnv to support multiple? Idk

Expand Down Expand Up @@ -101,7 +101,10 @@ export const connect = async (ledgerEnv: LedgerEnv = LedgerEnv.Testnet) => {
)
: []

const saveInterval = setInterval(() => persistConfig(fd, state), 10000)
const saveInterval = setInterval(
() => persistConfig(fileDescriptor, state),
10000
)

const add = async (
credentialConfig: CredentialConfigs
Expand Down Expand Up @@ -168,11 +171,11 @@ export const connect = async (ledgerEnv: LedgerEnv = LedgerEnv.Testnet) => {

const disconnect = async () => {
clearInterval(saveInterval)
await persistConfig(fd, state)
await persistConfig(fileDescriptor, state)
await Promise.all(state.uplinks.map(closeUplink))
await Promise.all(state.credentials.map(closeCredential))
await Promise.all(Object.values(state.settlers).map(closeEngine))
await promisify(close)(fd)
await promisify(close)(fileDescriptor)
}

// TODO Should disconnecting the API prevent other operations from occuring? (they may not work anyways)
Expand Down
11 changes: 7 additions & 4 deletions src/settlement/machinomy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ export type ReadyEthereumCredential = {
}

/**
* Ensure that the given string is begins with given prefix
* Ensure that the given string begins with given prefix
* - Prefix the string if it doesn't already
*/
const prefixWith = (prefix: string, str: string) =>
const ensurePrefixedWith = (prefix: string, str: string) =>
str.startsWith(prefix) ? str : prefix + str

const addressFromPrivate = (privateKey: string) =>
Expand All @@ -99,8 +99,11 @@ export const setupCredential = ({
ReadyEthereumCredential
> => ({
settlerType,
privateKey: prefixWith('0x', privateKey),
address: prefixWith('0x', addressFromPrivate(prefixWith('0x', privateKey)))
privateKey: ensurePrefixedWith('0x', privateKey),
address: ensurePrefixedWith(
'0x',
addressFromPrivate(ensurePrefixedWith('0x', privateKey))
)
})

export const uniqueId = (cred: ReadyEthereumCredential) => cred.address
Expand Down

0 comments on commit 537691d

Please sign in to comment.