-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
605 additions
and
120 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
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 |
---|---|---|
@@ -1,3 +1,61 @@ | ||
package keeper | ||
|
||
type Keeper struct{} | ||
import ( | ||
"errors" | ||
|
||
"github.com/tendermint/tendermint/libs/log" | ||
|
||
"github.com/Finschia/finschia-sdk/codec" | ||
sdk "github.com/Finschia/finschia-sdk/types" | ||
"github.com/Finschia/finschia-sdk/x/fbridge/types" | ||
"strings" | ||
) | ||
|
||
type Keeper struct { | ||
storeKey sdk.StoreKey | ||
cdc codec.BinaryCodec | ||
authKeeper types.AccountKeeper | ||
bankKeeper types.BankKeeper | ||
|
||
// the target denom for the bridge | ||
targetDenom string | ||
|
||
// the authority address that can execute privileged operations only if the guardian group is not set | ||
// - UpdateParams | ||
// - SuggestRole | ||
authority string | ||
} | ||
|
||
func NewKeeper( | ||
cdc codec.BinaryCodec, | ||
key sdk.StoreKey, | ||
authKeeper types.AccountKeeper, | ||
bankKeeper types.BankKeeper, | ||
targetDenom string, | ||
authority string, | ||
) Keeper { | ||
if addr := authKeeper.GetModuleAddress(types.ModuleName); addr == nil { | ||
panic(errors.New("fbridge module account has not been set")) | ||
} | ||
|
||
if strings.TrimSpace(authority) == "" { | ||
panic(errors.New("authority address cannot be empty")) | ||
} | ||
|
||
return Keeper{ | ||
storeKey: key, | ||
cdc: cdc, | ||
authKeeper: authKeeper, | ||
bankKeeper: bankKeeper, | ||
targetDenom: targetDenom, | ||
authority: authority, | ||
} | ||
} | ||
|
||
func (k Keeper) Logger(ctx sdk.Context) log.Logger { | ||
return ctx.Logger().With("module", "x/"+types.ModuleName) | ||
} | ||
|
||
func (k Keeper) GetAuthority() string { | ||
return k.authority | ||
} |
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,78 @@ | ||
package keeper | ||
|
||
import ( | ||
"encoding/binary" | ||
"encoding/hex" | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
"golang.org/x/crypto/sha3" | ||
|
||
sdk "github.com/Finschia/finschia-sdk/types" | ||
"github.com/Finschia/finschia-sdk/x/fbridge/types" | ||
) | ||
|
||
func (k Keeper) handleBridgeTransfer(ctx sdk.Context, sender sdk.AccAddress, amount sdk.Int) (uint64, error) { | ||
token := sdk.Coins{sdk.Coin{Denom: k.targetDenom, Amount: amount}} | ||
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, token); err != nil { | ||
panic(err) | ||
} | ||
|
||
if err := k.bankKeeper.BurnCoins(ctx, types.ModuleName, token); err != nil { | ||
panic(fmt.Errorf("cannot burn coins after a successful send to a module account: %v", err)) | ||
} | ||
|
||
nextSeq := k.GetNextSequence(ctx) + 1 | ||
k.setNextSequence(ctx, nextSeq) | ||
|
||
return nextSeq, nil | ||
} | ||
|
||
func (k Keeper) GetNextSequence(ctx sdk.Context) uint64 { | ||
store := ctx.KVStore(k.storeKey) | ||
bz := store.Get(types.KeyNextSeqSend) | ||
if len(bz) == 0 { | ||
panic(errors.New("sending next sequence should have been set")) | ||
} | ||
|
||
return binary.BigEndian.Uint64(bz) | ||
} | ||
|
||
func (k Keeper) setNextSequence(ctx sdk.Context, seq uint64) { | ||
store := ctx.KVStore(k.storeKey) | ||
bz := make([]byte, 8) | ||
binary.BigEndian.PutUint64(bz, seq) | ||
store.Set(types.KeyNextSeqSend, bz) | ||
} | ||
|
||
func IsValidEthereumAddress(address string) bool { | ||
matched, err := regexp.MatchString(`^0x[a-fA-F0-9]{40}$`, address) | ||
if err != nil || !matched { | ||
return false | ||
} | ||
|
||
address = address[2:] | ||
addressLower := strings.ToLower(address) | ||
|
||
hasher := sha3.NewLegacyKeccak256() | ||
hasher.Write([]byte(addressLower)) | ||
addressHash := hex.EncodeToString(hasher.Sum(nil)) | ||
|
||
checksumAddress := "" | ||
for i := 0; i < len(addressLower); i++ { | ||
c, err := strconv.ParseUint(string(addressHash[i]), 16, 4) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if c < 8 { | ||
checksumAddress += string(addressLower[i]) | ||
} else { | ||
checksumAddress += strings.ToUpper(string(addressLower[i])) | ||
} | ||
} | ||
|
||
return address == checksumAddress | ||
} |
Oops, something went wrong.