-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* write tests for migrating config encoding * complete migration 02 test * add better config encode/decode * fix migration
- Loading branch information
Showing
7 changed files
with
198 additions
and
9 deletions.
There are no files selected for viewing
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,43 @@ | ||
export const version = 2 | ||
|
||
const targetKeys = new Set(['secretKey', 'publicKey', 'addressRaw']) | ||
|
||
export function migrate (data = {}) { | ||
if (!isObject(data)) return data | ||
if (isUI8A(data)) return data | ||
|
||
const initial = isArray(data) ? [] : {} | ||
|
||
return Object.entries(data).reduce((acc, [key, value]) => { | ||
if (targetKeys.has(key) && !isUI8A(value)) { | ||
acc[key] = objToUI8A(value) | ||
} | ||
else { | ||
acc[key] = migrate(value) | ||
} | ||
|
||
return acc | ||
}, initial) | ||
} | ||
|
||
|
||
function isObject (thing) { | ||
return typeof thing === 'object' | ||
} | ||
|
||
function isArray (thing) { | ||
return Array.isArray(thing) | ||
} | ||
|
||
function isUI8A (thing) { | ||
return thing instanceof Uint8Array | ||
} | ||
|
||
|
||
function objToUI8A (obj) { | ||
const bytes = Object.keys(obj) | ||
.sort((a, b) => Number(a) > Number(b) ? 1 : -1) | ||
.map(arrayIndex => obj[arrayIndex]) | ||
|
||
return new Uint8Array(bytes) | ||
} |
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,9 +1,11 @@ | ||
import * as migration00 from './00' | ||
import * as migration01 from './01' | ||
import * as migration02 from './02' | ||
|
||
const migrations = [ | ||
migration00, | ||
migration01, | ||
migration02, | ||
] | ||
|
||
export default migrations |
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