forked from btcsuite/btcwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
permenent storage for address mapping (#16)
* gkv storage for btc to eth address mapping * fixes * fixes test --------- Co-authored-by: dpiatkivskyi <dmytro.piatkikvskyi@gmailcom>
- Loading branch information
1 parent
83ab76c
commit 9d2bb6c
Showing
7 changed files
with
142 additions
and
22 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package wallet | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/steveyen/gkvlite" | ||
"os" | ||
"sync" | ||
) | ||
|
||
const DefaultStorageFileName = "addressMap.gkv" | ||
const collectionName = "ethAddress" | ||
|
||
type AddressMapStorage struct { | ||
store *gkvlite.Store | ||
lock *sync.RWMutex | ||
} | ||
|
||
// NewAddressMapStorage creates a new storage instance using gkvlite with file path param | ||
func NewAddressMapStorage(storageFilePath string) (*AddressMapStorage, error) { | ||
if storageFilePath == "" { | ||
storageFilePath = DefaultStorageFileName | ||
} | ||
|
||
// Open the storage file | ||
file, err := os.OpenFile(storageFilePath, os.O_RDWR|os.O_CREATE, 0644) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot open storage file: %w", err) | ||
} | ||
|
||
store, err := gkvlite.NewStore(file) | ||
if err != nil { | ||
_ = file.Close() | ||
return nil, fmt.Errorf("cannot create storage: %w", err) | ||
} | ||
|
||
store.SetCollection(collectionName, nil) | ||
|
||
return &AddressMapStorage{ | ||
store: store, | ||
lock: new(sync.RWMutex), | ||
}, nil | ||
} | ||
|
||
func (s *AddressMapStorage) GetEthAddress(btcAddress string) (string, error) { | ||
s.lock.RLock() | ||
defer s.lock.RUnlock() | ||
c := s.store.GetCollection(collectionName) | ||
if c == nil { | ||
return "", errors.New("no such collection: " + collectionName) | ||
} | ||
ethAddress, err := c.Get([]byte(btcAddress)) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(ethAddress), err | ||
} | ||
|
||
func (s *AddressMapStorage) SetEthAddress(btcAddress, ethAddress string) error { | ||
s.lock.Lock() | ||
defer s.lock.Unlock() | ||
c := s.store.GetCollection(collectionName) | ||
if c == nil { | ||
return errors.New("no such collection: " + collectionName) | ||
} | ||
if err := c.Set([]byte(btcAddress), []byte(ethAddress)); err != nil { | ||
return fmt.Errorf("cannot set: %w", err) | ||
} | ||
|
||
return s.store.Flush() | ||
} |
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,30 @@ | ||
package wallet | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestStorage(t *testing.T) { | ||
storage, err := NewAddressMapStorage("") | ||
require.NoError(t, err) | ||
|
||
err = storage.SetEthAddress("btcAddress", "ethAddress") | ||
require.NoError(t, err) | ||
|
||
storage.store.Close() | ||
|
||
storage2, err := NewAddressMapStorage("") | ||
require.NoError(t, err) | ||
|
||
ethAddress, err := storage2.GetEthAddress("btcAddress") | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, "ethAddress", ethAddress) | ||
|
||
t.Cleanup(func() { | ||
storage2.store.Close() | ||
_ = os.Remove(DefaultStorageFileName) | ||
}) | ||
} |
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