Skip to content
Permalink
Browse files

hack to please Flow

  • Loading branch information...
SebastienGllmt committed Oct 10, 2019
1 parent 10139ae commit 371cbb94bcfffcd9088f799df8c0d22679915f5e
Showing with 107 additions and 77 deletions.
  1. +8 −8 app/api/ada/lib/storage/bridge/walletBuilder.js
  2. +99 −69 app/api/ada/lib/storage/bridge/walletHelper.js
@@ -277,17 +277,17 @@ function AsNotNull<T: {}>(
}

// types to represent requirements
export type HasConceptualWallet = {
export type HasConceptualWallet = {|
conceptualWalletRow: PromisslessReturnType<typeof ModifyConceptualWallet.add>
};
export type HasBip44Wrapper = {
|};
export type HasBip44Wrapper = {|
bip44WrapperRow: PromisslessReturnType<typeof AddBip44Wrapper.add>
};
export type HasPrivateDeriver = {
|};
export type HasPrivateDeriver = {|
privateDeriver: PromisslessReturnType<typeof AddPrivateDeriver.add>
};
|};

export type HasPublicDeriver<Row> = {
export type HasPublicDeriver<Row> = {|
// we have to re-specify the whole type since you can't use typeof on generic return types
publicDeriver: Array<{
publicDeriverResult: $ReadOnly<PublicDeriverRow>,
@@ -296,4 +296,4 @@ export type HasPublicDeriver<Row> = {
specificDerivationResult: $ReadOnly<Row>
}
}>,
};
|};
@@ -344,49 +344,7 @@ export async function migrateFromStorageV1(request: {
displayCutoff: number,
walletName: string,
hwWalletMetaInsert: void | HWFeatures,
}): Promise<HasConceptualWallet & HasBip44Wrapper & HasPublicDeriver<mixed>> {

const accountIndex = HARD_DERIVATION_START + 0;
const accountName = '';

const accountPublicKey = RustModule.Wallet.Bip44AccountPublic.new(
RustModule.Wallet.PublicKey.from_hex(request.accountPubKey),
RustModule.Wallet.DerivationScheme.v2()
);

// Note: we generate initial addresses in a separate database query
// from creation of the actual wallet
const initialDerivations = await raii(
request.db,
getAllSchemaTables(request.db, GetOrAddAddress),
async tx => {
const hashToIdFunc = async (
addressHash: Array<string>
): Promise<Array<number>> => {
const rows = await GetOrAddAddress.addByHash(
request.db, tx,
addressHash
);
return rows.map(row => row.AddressId);
};

const insert = await getAccountDefaultDerivations(
request.settings,
accountPublicKey,
hashToIdFunc,
);
// replace default display cutoff
const external = insert.find(chain => chain.index === EXTERNAL);
if (external == null) {
throw new Error('migrateFromStorageV1 cannot find external chain. Should never happen');
}
external.insert.DisplayCutoff = request.displayCutoff;

return insert;
}
);

let state;
}): Promise<void> {
{
let builder = WalletBuilder
.start(request.db)
@@ -431,22 +389,99 @@ export async function migrateFromStorageV1(request: {
}),
})
);
builder = await addPublicDeriverToMigratedWallet({
builder,
db: request.db,
accountPubKey: request.accountPubKey,
settings: request.settings,
displayCutoff: request.displayCutoff,
hwWalletMetaInsert: request.hwWalletMetaInsert,
});
return await builder.commit();
}
const pathToPublic = [{
index: BIP44_PURPOSE,
insert: {},
},
{
index: CARDANO_COINTYPE,
insert: {},
},
{
index: accountIndex,
insert: {},
}];
builder = builder
.addAdhocPublicDeriver(
finalState => ({
builder = await addPublicDeriverToMigratedWallet({
builder,
db: request.db,
accountPubKey: request.accountPubKey,
settings: request.settings,
displayCutoff: request.displayCutoff,
hwWalletMetaInsert: request.hwWalletMetaInsert,
});
await builder.commit();
}
}
async function addPublicDeriverToMigratedWallet<
T: $Shape<{||}> & HasConceptualWallet & HasBip44Wrapper
>(request: {
builder: WalletBuilder<T>,
db: lf$Database,
accountPubKey: string,
settings: RustModule.Wallet.BlockchainSettings,
displayCutoff: number,
hwWalletMetaInsert: void | HWFeatures,
}): Promise<WalletBuilder<T & HasPublicDeriver<mixed>>> {
const accountIndex = HARD_DERIVATION_START + 0;
const accountName = '';

const accountPublicKey = RustModule.Wallet.Bip44AccountPublic.new(
RustModule.Wallet.PublicKey.from_hex(request.accountPubKey),
RustModule.Wallet.DerivationScheme.v2()
);

// Note: we generate initial addresses in a separate database query
// from creation of the actual wallet
const initialDerivations = await raii(
request.db,
getAllSchemaTables(request.db, GetOrAddAddress),
async tx => {
const hashToIdFunc = async (
addressHash: Array<string>
): Promise<Array<number>> => {
const rows = await GetOrAddAddress.addByHash(
request.db, tx,
addressHash
);
return rows.map(row => row.AddressId);
};

const insert = await getAccountDefaultDerivations(
request.settings,
accountPublicKey,
hashToIdFunc,
);
// replace default display cutoff
const external = insert.find(chain => chain.index === EXTERNAL);
if (external == null) {
throw new Error('migrateFromStorageV1 cannot find external chain. Should never happen');
}
external.insert.DisplayCutoff = request.displayCutoff;

return insert;
}
);

const pathToPublic = [{
index: BIP44_PURPOSE,
insert: {},
},
{
index: CARDANO_COINTYPE,
insert: {},
},
{
index: accountIndex,
insert: {},
}];

return request.builder
.addAdhocPublicDeriver(
finalState => {
// kind of hacky way to extract the ID without angering Flow
const maybePrivateDeriver = ((finalState: any): WithNullableFields<HasPrivateDeriver>);
const privateDeriverKeyDerivationId = maybePrivateDeriver.privateDeriver == null
? null
: maybePrivateDeriver.privateDeriver.privateDeriverResult.KeyDerivationId;
return {
bip44WrapperId: finalState.bip44WrapperRow.Bip44WrapperId,
publicKey: {
Hash: accountPublicKey.key().to_hex(),
@@ -459,11 +494,9 @@ export async function migrateFromStorageV1(request: {
Name: accountName,
LastSyncInfoId: ids.lastSyncInfoId,
}),
parentDerivationId: finalState.privateDeriver == null
? null
: finalState.privateDeriver.privateDeriverResult.KeyDerivationId,
pathStartLevel: finalState.privateDeriver == null ? 0 : 1,
pathToPublic: finalState.privateDeriver != null
parentDerivationId: privateDeriverKeyDerivationId,
pathStartLevel: privateDeriverKeyDerivationId == null ? 0 : 1,
pathToPublic: privateDeriverKeyDerivationId != null
? pathToPublic
: [{
index: 0,
@@ -476,10 +509,7 @@ export async function migrateFromStorageV1(request: {
ConceptualWalletId: finalState.conceptualWalletRow.ConceptualWalletId,
...request.hwWalletMetaInsert
},
})
);
state = builder.commit();
}

return state;
};
}
);
}

0 comments on commit 371cbb9

Please sign in to comment.
You can’t perform that action at this time.