func (r *fileRepo) StoreKey(addr hash.Hash, key bmcrypto.PubKey) error {
	// Lock our key file for writing
	lockfilePath := r.getPath(addr, keysFile+".lock")
	lock, err := r.lockFile(lockfilePath)
	if err != nil {
		return err
	}

	err = r.tryLockFile(lock)
	if err != nil {
		return err
	}

	defer func() {
		_ = r.unLockFile(lock)
	}()

	// Read keys
	pk := &PubKeys{}
	err = r.fetchJSON(addr, keysFile, pk)
	if err != nil && os.IsNotExist(err) {
		err = r.createPubKeyFile(addr)
	}
	if err != nil {
		return err
	}

	// Add new key
	pk.PubKeys = append(pk.PubKeys, key)

	// Convert back to string
	data, err := json.MarshalIndent(pk, "", "  ")
	if err != nil {
		return err
	}

	// And store
	return r.store(addr, keysFile, data)
}
// Store the public key for this account
Store the public key for this account
// Store the public key for this account
func (r *fileRepo) StoreKey(addr hash.Hash, key bmcrypto.PubKey) error {
	lockfilePath := r.getPath(addr, keysFile+".lock")
	lock, err := r.lockFile(lockfilePath)
	if err != nil {
		return err
	}
	err = r.tryLockFile(lock)
	if err != nil {
		return err
	}
	defer func() {
		_ = r.unLockFile(lock)
	}()
	pk := &PubKeys{}
	err = r.fetchJSON(addr, keysFile, pk)
	if err != nil && os.IsNotExist(err) {
		err = r.createPubKeyFile(addr)
	}
	if err != nil {
		return err
	}
	pk.PubKeys = append(pk.PubKeys, key)
	data, err := json.MarshalIndent(pk, "", "  ")
	if err != nil {
		return err
	}
	return r.store(addr, keysFile, data)
}
func ( r * fileRepo ) StoreKey ( addr hash . Hash , key bmcrypto . PubKey ) error { lockfilePath := r . getPath ( addr , keysFile + " " ) lock , err := r . lockFile ( lockfilePath ) if err != nil { return err } err = r . tryLockFile ( lock ) if err != nil { return err } defer func ( ) { _ = r . unLockFile ( lock ) } ( ) pk := & PubKeys { } err = r . fetchJSON ( addr , keysFile , pk ) if err != nil && os . IsNotExist ( err ) { err = r . createPubKeyFile ( addr ) } if err != nil { return err } pk . PubKeys = append ( pk . PubKeys , key ) data , err := json . MarshalIndent ( pk , " " , " " ) if err != nil { return err } return r . store ( addr , keysFile , data ) }
Store the public key for this account
Store the public key for this account
// Lock our key file for writing // Read keys // Add new key // Convert back to string // And store