-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add verifiedOnBlockchain property to transaction meta (#20890)
- Loading branch information
1 parent
db37487
commit 5a90125
Showing
9 changed files
with
183 additions
and
1 deletion.
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
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,117 @@ | ||
import { migrate, version } from './098'; | ||
|
||
const oldVersion = 97; | ||
describe('migration #98', () => { | ||
it('updates the version metadata', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: {}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
|
||
expect(newStorage.meta).toStrictEqual({ version }); | ||
}); | ||
|
||
it('handles missing TransactionController', async () => { | ||
const oldState = { | ||
OtherController: {}, | ||
}; | ||
|
||
const transformedState = await migrate({ | ||
meta: { version: oldVersion }, | ||
data: oldState, | ||
}); | ||
|
||
expect(transformedState.data).toEqual(oldState); | ||
}); | ||
|
||
it('handles empty transactions', async () => { | ||
const oldState = { | ||
TransactionController: { | ||
transactions: {}, | ||
}, | ||
}; | ||
|
||
const transformedState = await migrate({ | ||
meta: { version: oldVersion }, | ||
data: oldState, | ||
}); | ||
|
||
expect(transformedState.data).toEqual(oldState); | ||
}); | ||
|
||
it('handles missing state', async () => { | ||
const transformedState = await migrate({ | ||
meta: { version: oldVersion }, | ||
data: {}, | ||
}); | ||
|
||
expect(transformedState.data).toEqual({}); | ||
}); | ||
|
||
it('adds verifiedOnBlockchain in transaction based on the presence of txReceipt', async () => { | ||
const oldState = { | ||
TransactionController: { | ||
transactions: { | ||
tx1: { | ||
to: '0x9ef57335bc7d5b6cbc06dca6064a604b75e09ace', | ||
txReceipt: { | ||
blockHash: | ||
'0xafa4e1fd95e429d9c6e6c7c1d282b2bd0bbeb50d0a68743e9392b9c95a06e2eb', | ||
}, | ||
otherProp: 'value', | ||
}, | ||
tx2: { | ||
to: '0x9ef57335bc7d5b6cbc06dca6064a604b75e09ace', | ||
otherProp: 'value', | ||
}, | ||
tx3: { | ||
to: '0x9ef57335bc7d5b6cbc06dca6064a604b75e09ace', | ||
txReceipt: { | ||
blockHash: | ||
'0xafa4e1fd95e429d9c6e6c7c1d282b2bd0bbeb50d0a68743e9392b9c95a06e2eb', | ||
}, | ||
otherProp: 'value', | ||
}, | ||
}, | ||
}, | ||
}; | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: oldState, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
|
||
expect(newStorage.data).toEqual({ | ||
TransactionController: { | ||
transactions: { | ||
tx1: { | ||
to: '0x9ef57335bc7d5b6cbc06dca6064a604b75e09ace', | ||
txReceipt: { | ||
blockHash: | ||
'0xafa4e1fd95e429d9c6e6c7c1d282b2bd0bbeb50d0a68743e9392b9c95a06e2eb', | ||
}, | ||
otherProp: 'value', | ||
verifiedOnBlockchain: true, | ||
}, | ||
tx2: { | ||
to: '0x9ef57335bc7d5b6cbc06dca6064a604b75e09ace', | ||
otherProp: 'value', | ||
verifiedOnBlockchain: false, | ||
}, | ||
tx3: { | ||
to: '0x9ef57335bc7d5b6cbc06dca6064a604b75e09ace', | ||
txReceipt: { | ||
blockHash: | ||
'0xafa4e1fd95e429d9c6e6c7c1d282b2bd0bbeb50d0a68743e9392b9c95a06e2eb', | ||
}, | ||
otherProp: 'value', | ||
verifiedOnBlockchain: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |
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,52 @@ | ||
import { cloneDeep, isEmpty } from 'lodash'; | ||
|
||
type VersionedData = { | ||
meta: { version: number }; | ||
data: Record<string, unknown>; | ||
}; | ||
|
||
export const version = 98; // Increment the version number | ||
|
||
/** | ||
* Add `verifiedOnBlockchain` property to transactions based on the presence of `txReceipt` | ||
* | ||
* @param originalVersionedData - Versioned MetaMask extension state, exactly what we persist to dist. | ||
* @param originalVersionedData.meta - State metadata. | ||
* @param originalVersionedData.meta.version - The current state version. | ||
* @param originalVersionedData.data - The persisted MetaMask state, keyed by controller. | ||
* @returns Updated versioned MetaMask extension state. | ||
*/ | ||
export async function migrate( | ||
originalVersionedData: VersionedData, | ||
): Promise<VersionedData> { | ||
const versionedData = cloneDeep(originalVersionedData); | ||
versionedData.meta.version = version; | ||
transformState(versionedData.data); | ||
return versionedData; | ||
} | ||
|
||
function transformState(state: Record<string, any>) { | ||
const transactionControllerState = state?.TransactionController || {}; | ||
const transactions = transactionControllerState?.transactions || {}; | ||
|
||
if (isEmpty(transactions)) { | ||
return; | ||
} | ||
|
||
const newTxs = Object.keys(transactions).reduce((txs, txId) => { | ||
const transaction = transactions[txId]; | ||
|
||
// Add the `verifiedOnBlockchain` property based on the presence of `txReceipt` | ||
transaction.verifiedOnBlockchain = Boolean(transaction.txReceipt); | ||
|
||
return { | ||
...txs, | ||
[txId]: transaction, | ||
}; | ||
}, {}); | ||
|
||
state.TransactionController = { | ||
...transactionControllerState, | ||
transactions: newTxs, | ||
}; | ||
} |
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