Skip to content

Commit

Permalink
Clean EdgeBox more deeply
Browse files Browse the repository at this point in the history
  • Loading branch information
swansontec committed Oct 2, 2023
1 parent fe8f2d1 commit 7262003
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
32 changes: 22 additions & 10 deletions src/core/storage/repo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Disklet, mergeDisklets, navigateDisklet } from 'disklet'
import type { EdgeBox as SyncEdgeBox } from 'edge-sync-client'
import { SyncClient } from 'edge-sync-client'
import { base16, base64 } from 'rfc4648'

import { asEdgeBox, wasEdgeBox } from '../../types/server-cleaners'
import { EdgeBox } from '../../types/server-types'
import { EdgeIo } from '../../types/types'
import { sha256 } from '../../util/crypto/hashes'
import { base58 } from '../../util/encoding'
Expand All @@ -10,8 +13,12 @@ import { StorageWalletPaths, StorageWalletStatus } from './storage-reducer'

const CHANGESET_MAX_ENTRIES = 100

interface RepoChanges {
[path: string]: EdgeBox | null
}

export interface SyncResult {
changes: { [path: string]: any }
changes: RepoChanges
status: StorageWalletStatus
}

Expand Down Expand Up @@ -71,13 +78,13 @@ export function loadRepoStatus(
*/
export async function saveChanges(
disklet: Disklet,
changes: { [path: string]: any }
changes: RepoChanges
): Promise<void> {
await Promise.all(
Object.keys(changes).map(path => {
const json = changes[path]
return json != null
? disklet.setText(path, JSON.stringify(json))
const box = changes[path]
return box != null
? disklet.setText(path, JSON.stringify(wasEdgeBox(box)))
: disklet.delete(path)
})
)
Expand All @@ -95,12 +102,12 @@ export async function syncRepo(

const ourChanges: Array<{
path: string
json: any
box: EdgeBox
}> = await deepListWithLimit(changesDisklet).then(paths => {
return Promise.all(
paths.map(async path => ({
path,
json: JSON.parse(await changesDisklet.getText(path))
box: asEdgeBox(JSON.parse(await changesDisklet.getText(path)))
}))
)
})
Expand All @@ -116,15 +123,20 @@ export async function syncRepo(
}

// Write local changes to the repo.
const changes: { [name: string]: any } = {}
const changes: { [name: string]: SyncEdgeBox } = {}
for (const change of ourChanges) {
changes[change.path] = change.json
changes[change.path] = wasEdgeBox(change.box)
}
return syncClient.updateRepo(syncKeyEncoded, status.lastHash, { changes })
})()

// Make the request:
const { changes = {}, hash } = reply
const { hash } = reply
const changes: RepoChanges = {}
for (const path of Object.keys(reply.changes ?? {})) {
const json = reply.changes[path]
changes[path] = json == null ? null : asEdgeBox(json)
}

// Save the incoming changes into our `data` folder:
await saveChanges(dataDisklet, changes)
Expand Down
4 changes: 2 additions & 2 deletions src/types/server-cleaners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ const asEdgeRecoveryQuestionChoice: Cleaner<EdgeRecoveryQuestionChoice> =

export const asEdgeBox: Cleaner<EdgeBox> = asObject({
encryptionType: asNumber,
data_base64: asString,
iv_hex: asString
data_base64: asBase64,
iv_hex: asBase16
})

export const asEdgeSnrp: Cleaner<EdgeSnrp> = asObject({
Expand Down
4 changes: 2 additions & 2 deletions src/types/server-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import type { EdgePendingVoucher, EdgeRecoveryQuestionChoice } from './types'
*/
export interface EdgeBox {
encryptionType: number
data_base64: string
iv_hex: string
data_base64: Uint8Array
iv_hex: Uint8Array
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/util/crypto/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import aesjs from 'aes-js'
import { base16, base64 } from 'rfc4648'

import { EdgeBox } from '../../types/server-types'
import { EdgeIo } from '../../types/types'
Expand Down Expand Up @@ -30,8 +29,8 @@ export function decrypt(box: EdgeBox, key: Uint8Array): Uint8Array {
if (box.encryptionType !== 0) {
throw new Error('Unknown encryption type')
}
const iv = base16.parse(box.iv_hex)
const ciphertext = base64.parse(box.data_base64)
const iv = box.iv_hex
const ciphertext = box.data_base64

// Decrypt:
const cipher = new AesCbc(key, iv)
Expand Down Expand Up @@ -115,8 +114,8 @@ export function encrypt(
const ciphertext = cipher.encrypt(raw)
return {
encryptionType: 0,
iv_hex: base16.stringify(iv),
data_base64: base64.stringify(ciphertext)
iv_hex: iv,
data_base64: ciphertext
}
}

Expand Down
5 changes: 3 additions & 2 deletions test/util/crypto/crypto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, it } from 'mocha'
import { base16 } from 'rfc4648'

import { makeFakeIo } from '../../../src/index'
import { asEdgeBox } from '../../../src/types/server-cleaners'
import { decrypt, decryptText, encrypt } from '../../../src/util/crypto/crypto'
import { utf8 } from '../../../src/util/encoding'

Expand All @@ -11,12 +12,12 @@ describe('encryption', function () {
const key = base16.parse(
'002688cc350a5333a87fa622eacec626c3d1c0ebf9f3793de3885fa254d7e393'
)
const box = {
const box = asEdgeBox({
data_base64:
'X08Snnou2PrMW21ZNyJo5C8StDjTNgMtuEoAJL5bJ6LDPdZGQLhjaUMetOknaPYnmfBCHNQ3ApqmE922Hkp30vdxzXBloopfPLJKdYwQxURYNbiL4TvNakP7i0bnTlIsR7bj1q/65ZyJOW1HyOKV/tmXCf56Fhe3Hcmb/ebsBF72FZr3jX5pkSBO+angK15IlCIiem1kPi6QmzyFtMB11i0GTjSS67tLrWkGIqAmik+bGqy7WtQgfMRxQNNOxePPSHHp09431Ogrc9egY3txnBN2FKnfEM/0Wa/zLWKCVQXCGhmrTx1tmf4HouNDOnnCgkRWJYs8FJdrDP8NZy4Fkzs7FoH7RIaUiOvosNKMil1CBknKremP6ohK7SMLGoOHpv+bCgTXcAeB3P4Slx3iy+RywTSLb3yh+HDo6bwt+vhujP0RkUamI5523bwz3/7vLO8BzyF6WX0By2s4gvMdFQ==',
encryptionType: 0,
iv_hex: '96a4cd52670c13df9712fdc1b564d44b'
}
})

expect(decrypt(box, key)).deep.equals(utf8.parse('payload'))
})
Expand Down

0 comments on commit 7262003

Please sign in to comment.