Skip to content
Snippets Groups Projects
Select Git revision
  • dce240a41b4514a58736a3b328834d5c3b5500f2
  • release default protected
  • 11-22-implement-kv-interface-defined-in-collectiveversionedkvgo
  • hotfix/TestHostPool_UpdateNdf_AddFilter
  • XX-4719/announcementChannels
  • xx-4717/logLevel
  • jonah/noob-channel
  • master protected
  • XX-4707/tagDiskJson
  • xx-4698/notification-retry
  • hotfix/notifylockup
  • syncNodes
  • hotfix/localCB
  • XX-4677/NewChanManagerMobile
  • XX-4689/DmSync
  • duplicatePrefix
  • XX-4601/HavenInvites
  • finalizedUICallbacks
  • XX-4673/AdminKeySync
  • debugNotifID
  • anne/test
  • v4.7.5
  • v4.7.4
  • v4.7.3
  • v4.7.2
  • v4.7.1
  • v4.6.3
  • v4.6.1
  • v4.5.0
  • v4.4.4
  • v4.3.11
  • v4.3.8
  • v4.3.7
  • v4.3.6
  • v4.3.5
  • v4.2.0
  • v4.3.0
  • v4.3.4
  • v4.3.3
  • v4.3.2
  • v4.3.1
41 results

remove_test.go

Blame
  • precan.go 3.13 KiB
    ///////////////////////////////////////////////////////////////////////////////
    // Copyright © 2020 xx network SEZC                                          //
    //                                                                           //
    // Use of this source code is governed by a license that can be found in the //
    // LICENSE file                                                              //
    ///////////////////////////////////////////////////////////////////////////////
    
    // precan.go handles functions for precan users, which are not usable
    // unless you are on a localized test network.
    package cmd
    
    import (
    	"github.com/pkg/errors"
    	jww "github.com/spf13/jwalterweatherman"
    	"github.com/spf13/viper"
    	"gitlab.com/elixxir/client/xxdk"
    	"gitlab.com/elixxir/crypto/contact"
    	"gitlab.com/xx_network/primitives/id"
    	"io/fs"
    	"io/ioutil"
    	"os"
    )
    
    // loadOrInitPrecan will build a new xxdk.E2e from existing storage
    // or from a new storage that it will create if none already exists
    func loadOrInitPrecan(precanId uint, password []byte, storeDir string,
    	cmixParams xxdk.CMIXParams, e2eParams xxdk.E2EParams) *xxdk.E2e {
    
    	// create a new client if none exist
    	var net *xxdk.Cmix
    	var identity xxdk.ReceptionIdentity
    	if _, err := os.Stat(storeDir); errors.Is(err, fs.ErrNotExist) {
    		// Initialize from scratch
    		ndfJson, err := ioutil.ReadFile(viper.GetString("ndf"))
    		if err != nil {
    			jww.FATAL.Panicf("%+v", err)
    		}
    
    		err = xxdk.NewPrecannedClient(precanId, string(ndfJson), storeDir, password)
    		net, err = xxdk.LoadCmix(storeDir, password, cmixParams)
    		if err != nil {
    			jww.FATAL.Panicf("%+v", err)
    		}
    
    		identity, err = xxdk.MakeLegacyReceptionIdentity(net)
    		if err != nil {
    			return nil
    		}
    
    		err = xxdk.StoreReceptionIdentity(identityStorageKey, identity, net)
    		if err != nil {
    			jww.FATAL.Panicf("%+v", err)
    		}
    	} else {
    		// Initialize from storage
    		net, err = xxdk.LoadCmix(storeDir, password, cmixParams)
    		if err != nil {
    			jww.FATAL.Panicf("%+v", err)
    		}
    		identity, err = xxdk.LoadReceptionIdentity(identityStorageKey, net)
    		if err != nil {
    			jww.FATAL.Panicf("%+v", err)
    		}
    	}
    
    	jww.INFO.Printf("Using Precanned sender")
    	messenger, err := xxdk.Login(net, authCbs, identity, e2eParams)
    	if err != nil {
    		jww.FATAL.Panicf("%+v", err)
    	}
    	return messenger
    }
    
    func isPrecanID(id *id.ID) bool {
    	// check if precanned
    	rBytes := id.Bytes()
    	for i := 0; i < 32; i++ {
    		if i != 7 && rBytes[i] != 0 {
    			return false
    		}
    	}
    	if rBytes[7] != byte(0) && rBytes[7] <= byte(40) {
    		return true
    	}
    	return false
    }
    
    func getPrecanID(recipientID *id.ID) uint {
    	return uint(recipientID.Bytes()[7])
    }
    
    func addPrecanAuthenticatedChannel(client *xxdk.E2e, recipientID *id.ID,
    	recipient contact.Contact) {
    	jww.WARN.Printf("Precanned user id detected: %s", recipientID)
    	preUsr, err := client.MakePrecannedAuthenticatedChannel(
    		getPrecanID(recipientID))
    	if err != nil {
    		jww.FATAL.Panicf("%+v", err)
    	}
    	// Sanity check, make sure user id's haven't changed
    	preBytes := preUsr.ID.Bytes()
    	idBytes := recipientID.Bytes()
    	for i := 0; i < len(preBytes); i++ {
    		if idBytes[i] != preBytes[i] {
    			jww.FATAL.Panicf("no id match: %v %v",
    				preBytes, idBytes)
    		}
    	}
    }