|
| 1 | +/* |
| 2 | + * Copyright 2020 NEM |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { MultisigAccountGraphInfo } from '../../model/account/MultisigAccountGraphInfo'; |
| 18 | +import { MultisigAccountInfo } from '../../model/account/MultisigAccountInfo'; |
| 19 | + |
| 20 | +/** |
| 21 | + * MultisigGraph utilities |
| 22 | + */ |
| 23 | + |
| 24 | +// Type for Multisig Tree children Object |
| 25 | +export type MultisigChildrenTreeObject = { |
| 26 | + address: string; |
| 27 | + children: []; // children array. |
| 28 | +}; |
| 29 | + |
| 30 | +export class MultisigGraphUtils { |
| 31 | + /** |
| 32 | + * creates a structred Tree object containing Current multisig account with children |
| 33 | + * @param {MultisigAccountInfo[][]} multisigEnteries |
| 34 | + * @returns {MultisigChildrenTreeObject[]} Array of multisigChildrentTree objects |
| 35 | + */ |
| 36 | + public static getMultisigChildren(multisigAccountGraphInfoMapped: MultisigAccountInfo[][]): MultisigChildrenTreeObject[] { |
| 37 | + if (multisigAccountGraphInfoMapped) { |
| 38 | + const mappedTree: MultisigChildrenTreeObject[] = []; |
| 39 | + multisigAccountGraphInfoMapped.forEach((level: MultisigAccountInfo[]) => { |
| 40 | + level.forEach((entry: MultisigAccountInfo) => { |
| 41 | + mappedTree.push({ |
| 42 | + address: entry.accountAddress.plain(), |
| 43 | + children: [], |
| 44 | + }); |
| 45 | + // find the entry matching with address matching cosignatory address and update his children |
| 46 | + const updateRecursively = (address: string, object: MultisigChildrenTreeObject) => (obj): any => { |
| 47 | + if (obj.address === address) { |
| 48 | + obj.children.push(object); |
| 49 | + } else if (obj.children) { |
| 50 | + obj.children.forEach(updateRecursively(address, object)); |
| 51 | + } |
| 52 | + }; |
| 53 | + entry.cosignatoryAddresses.forEach((addressVal) => { |
| 54 | + mappedTree.forEach( |
| 55 | + updateRecursively(addressVal['address'], { |
| 56 | + address: entry.accountAddress.plain(), |
| 57 | + children: [], |
| 58 | + }), |
| 59 | + ); |
| 60 | + }); |
| 61 | + }); |
| 62 | + }); |
| 63 | + return mappedTree; |
| 64 | + } |
| 65 | + return []; |
| 66 | + } |
| 67 | + /** |
| 68 | + * sort entries based on tree hierarchy from top to bottom |
| 69 | + * @param {Map<number, MultisigAccountInfo[]>} multisigEnteries |
| 70 | + * @returns {MultisigAccountInfo[]} sorted multisig graph |
| 71 | + */ |
| 72 | + private static getMultisigGraphArraySorted(multisigEntries: Map<number, MultisigAccountInfo[]>): MultisigAccountInfo[][] { |
| 73 | + return [...multisigEntries.keys()] |
| 74 | + .sort((a, b) => b - a) // Get addresses from top to bottom |
| 75 | + .map((key) => multisigEntries.get(key) || []) |
| 76 | + .filter((x) => x.length > 0); |
| 77 | + } |
| 78 | + /** |
| 79 | + * returns sorted tree entries |
| 80 | + * @param {MultisigAccountGraphInfo} graphInfo |
| 81 | + * @returns {MultisigAccountInfo[][]} array of sorted multisigInfo |
| 82 | + */ |
| 83 | + public static getMultisigInfoFromMultisigGraphInfo(graphInfo: MultisigAccountGraphInfo): MultisigAccountInfo[][] { |
| 84 | + const { multisigEntries } = graphInfo; |
| 85 | + return [...this.getMultisigGraphArraySorted(multisigEntries)].map((item) => item); |
| 86 | + } |
| 87 | +} |
0 commit comments