func MigrateVault(data []byte, fromVersion int) (*StoreType, error) {
	var (
		err     error
		storeV0 = make([]AccountInfoV1, 0)
		storeV1 = &StoreTypeV1{}
		storeV2 = &StoreType{}
	)

	// Read the correct initial data format
	switch fromVersion {
	case VersionV0:
		err = json.Unmarshal(data, &storeV0)
	case VersionV1:
		err = json.Unmarshal(data, &storeV1)
	case VersionV2:
		err = json.Unmarshal(data, &storeV2)
	}
	if err != nil {
		return nil, err
	}

	// Iterate migrations until we reach the latest version
	for fromVersion <= LatestVaultVersion {
		switch fromVersion {
		case VersionV0:
			/*
			 * V0 -> V1 adds organisations
			 */
			storeV1.Accounts = storeV0
			storeV1.Organisations = []OrganisationInfoV1{}

			fromVersion++
		case VersionV1:
			/*
			 * V1 -> V2 has multiple keys in both organisation and accounts
			 */
			for _, accV1 := range storeV1.Accounts {
				accV2 := &AccountInfo{}
				accV2.Address = accV1.Address
				accV2.Pow = accV1.Pow
				accV2.RoutingID = accV1.RoutingID
				accV2.Name = accV1.Name
				accV2.Settings = accV1.Settings
				accV2.Keys = []KeyPair{
					{
						KeyPair: bmcrypto.KeyPair{
							Generator:   "",
							FingerPrint: accV1.PubKey.Fingerprint(),
							PrivKey:     accV1.PrivKey,
							PubKey:      accV1.PubKey,
						},
						Active: true,
					},
				}
				storeV2.Accounts = append(storeV2.Accounts, *accV2)
			}

			for _, orgV1 := range storeV1.Organisations {
				orgV2 := &OrganisationInfo{}
				orgV2.Addr = orgV1.Addr
				orgV2.Pow = orgV1.Pow
				orgV2.FullName = orgV1.FullName
				orgV2.Validations = orgV1.Validations
				orgV2.Keys = []KeyPair{
					{
						KeyPair: bmcrypto.KeyPair{
							Generator:   "",
							FingerPrint: orgV1.PubKey.Fingerprint(),
							PrivKey:     orgV1.PrivKey,
							PubKey:      orgV1.PubKey,
						},
						Active: true,
					},
				}
				storeV2.Organisations = append(storeV2.Organisations, *orgV2)
			}

			fromVersion++

		case VersionV2:
			// Latest version, no need to migrate
			return storeV2, nil
		}
	}

	return nil, errors.New("error while running migrations on the vault")
}
// MigrateVault will migrate a vault from a specific version all the way to the latest version
MigrateVault will migrate a vault from a specific version all the way to the latest version
// MigrateVault will migrate a vault from a specific version all the way to the latest version
func MigrateVault(data []byte, fromVersion int) (*StoreType, error) {
	var (
		err     error
		storeV0 = make([]AccountInfoV1, 0)
		storeV1 = &StoreTypeV1{}
		storeV2 = &StoreType{}
	)
	switch fromVersion {
	case VersionV0:
		err = json.Unmarshal(data, &storeV0)
	case VersionV1:
		err = json.Unmarshal(data, &storeV1)
	case VersionV2:
		err = json.Unmarshal(data, &storeV2)
	}
	if err != nil {
		return nil, err
	}
	for fromVersion <= LatestVaultVersion {
		switch fromVersion {
		case VersionV0:
			storeV1.Accounts = storeV0
			storeV1.Organisations = []OrganisationInfoV1{}
			fromVersion++
		case VersionV1:
			for _, accV1 := range storeV1.Accounts {
				accV2 := &AccountInfo{}
				accV2.Address = accV1.Address
				accV2.Pow = accV1.Pow
				accV2.RoutingID = accV1.RoutingID
				accV2.Name = accV1.Name
				accV2.Settings = accV1.Settings
				accV2.Keys = []KeyPair{
					{
						KeyPair: bmcrypto.KeyPair{
							Generator:   "",
							FingerPrint: accV1.PubKey.Fingerprint(),
							PrivKey:     accV1.PrivKey,
							PubKey:      accV1.PubKey,
						},
						Active: true,
					},
				}
				storeV2.Accounts = append(storeV2.Accounts, *accV2)
			}
			for _, orgV1 := range storeV1.Organisations {
				orgV2 := &OrganisationInfo{}
				orgV2.Addr = orgV1.Addr
				orgV2.Pow = orgV1.Pow
				orgV2.FullName = orgV1.FullName
				orgV2.Validations = orgV1.Validations
				orgV2.Keys = []KeyPair{
					{
						KeyPair: bmcrypto.KeyPair{
							Generator:   "",
							FingerPrint: orgV1.PubKey.Fingerprint(),
							PrivKey:     orgV1.PrivKey,
							PubKey:      orgV1.PubKey,
						},
						Active: true,
					},
				}
				storeV2.Organisations = append(storeV2.Organisations, *orgV2)
			}
			fromVersion++
		case VersionV2:
			return storeV2, nil
		}
	}
	return nil, errors.New("error while running migrations on the vault")
}
func MigrateVault ( data [ ] byte , fromVersion int ) ( * StoreType , error ) { var ( err error storeV0 = make ( [ ] AccountInfoV1 , 0 ) storeV1 = & StoreTypeV1 { } storeV2 = & StoreType { } ) switch fromVersion { case VersionV0 : err = json . Unmarshal ( data , & storeV0 ) case VersionV1 : err = json . Unmarshal ( data , & storeV1 ) case VersionV2 : err = json . Unmarshal ( data , & storeV2 ) } if err != nil { return nil , err } for fromVersion <= LatestVaultVersion { switch fromVersion { case VersionV0 : storeV1 . Accounts = storeV0 storeV1 . Organisations = [ ] OrganisationInfoV1 { } fromVersion ++ case VersionV1 : for _ , accV1 := range storeV1 . Accounts { accV2 := & AccountInfo { } accV2 . Address = accV1 . Address accV2 . Pow = accV1 . Pow accV2 . RoutingID = accV1 . RoutingID accV2 . Name = accV1 . Name accV2 . Settings = accV1 . Settings accV2 . Keys = [ ] KeyPair { { KeyPair : bmcrypto . KeyPair { Generator : " " , FingerPrint : accV1 . PubKey . Fingerprint ( ) , PrivKey : accV1 . PrivKey , PubKey : accV1 . PubKey , } , Active : true , } , } storeV2 . Accounts = append ( storeV2 . Accounts , * accV2 ) } for _ , orgV1 := range storeV1 . Organisations { orgV2 := & OrganisationInfo { } orgV2 . Addr = orgV1 . Addr orgV2 . Pow = orgV1 . Pow orgV2 . FullName = orgV1 . FullName orgV2 . Validations = orgV1 . Validations orgV2 . Keys = [ ] KeyPair { { KeyPair : bmcrypto . KeyPair { Generator : " " , FingerPrint : orgV1 . PubKey . Fingerprint ( ) , PrivKey : orgV1 . PrivKey , PubKey : orgV1 . PubKey , } , Active : true , } , } storeV2 . Organisations = append ( storeV2 . Organisations , * orgV2 ) } fromVersion ++ case VersionV2 : return storeV2 , nil } } return nil , errors . New ( " " ) }
MigrateVault will migrate a vault from a specific version all the way to the latest version
MigrateVault will migrate a vault from a specific version all the way to the latest version
// Read the correct initial data format // Iterate migrations until we reach the latest version /*
			 * V0 -> V1 adds organisations
			 */ /*
			 * V1 -> V2 has multiple keys in both organisation and accounts
			 */ // Latest version, no need to migrate