-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
527d605
commit a86fa66
Showing
18 changed files
with
252 additions
and
100 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,8 @@ | ||
defmodule TTEth.Behaviours.Wallet do | ||
@moduledoc """ | ||
Behaviour for wallet adapters. | ||
""" | ||
@type config :: map() | binary() | ||
|
||
@callback new(config) :: struct() | ||
end |
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,76 @@ | ||
defmodule TTEth.LocalWallet do | ||
@moduledoc """ | ||
A local wallet, derived from a private key. | ||
## Config | ||
Expects the config to look like so: | ||
config TTEth, | ||
wallet_adapter: TTEth.LocalWallet, | ||
wallets: [ | ||
primary: "0x0aF6...0000", | ||
] | ||
""" | ||
alias TTEth.Type.{Address, PublicKey, PrivateKey} | ||
alias TTEth.Secp256k1 | ||
alias TTEth.Behaviours.Wallet, as: WalletBehaviour | ||
|
||
@type t :: %__MODULE__{} | ||
|
||
@behaviour WalletBehaviour | ||
|
||
defstruct [ | ||
:private_key, | ||
:human_private_key | ||
] | ||
|
||
defimpl TTEth.Protocols.Wallet, for: __MODULE__ do | ||
def new_attrs(%@for{} = wallet) do | ||
pub = | ||
wallet.private_key | ||
|> PublicKey.from_private_key!() | ||
|> PublicKey.from_human!() | ||
|
||
address = pub |> Address.from_public_key!() | ||
|
||
[ | ||
adapter: @for, | ||
address: address, | ||
public_key: pub, | ||
human_address: address |> Address.to_human!(), | ||
human_public_key: pub |> PublicKey.to_human!(), | ||
_adapter: wallet | ||
] | ||
end | ||
|
||
def sign(%@for{} = wallet, "" <> digest), | ||
do: | ||
digest | ||
|> Secp256k1.ecdsa_sign_compact(wallet.private_key) | ||
end | ||
|
||
@impl WalletBehaviour | ||
def new(%{private_key: private_key} = _config), | ||
do: | ||
__MODULE__ | ||
|> struct!(%{ | ||
private_key: private_key |> PrivateKey.from_human!(), | ||
human_private_key: private_key | ||
}) | ||
|
||
def new("" <> private_key = _config), | ||
do: new(%{private_key: private_key}) | ||
|
||
## Helpers. | ||
|
||
@doc """ | ||
Generates a new `TTEth.LocalWallet.t` with a random private key. | ||
""" | ||
def generate() do | ||
{_pub, priv} = TTEth.new_keypair() | ||
|
||
%{private_key: priv} | ||
|> new() | ||
end | ||
end |
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,16 @@ | ||
defprotocol TTEth.Protocols.Wallet do | ||
@moduledoc """ | ||
Protocol for wallet adapters. | ||
""" | ||
alias TTEth.Wallet | ||
Check warning on line 5 in lib/tt_eth/protocols/wallet.ex GitHub Actions / OTP 22.x / Elixir 1.11.4
Check warning on line 5 in lib/tt_eth/protocols/wallet.ex GitHub Actions / OTP 22.x / Elixir 1.13.4
Check warning on line 5 in lib/tt_eth/protocols/wallet.ex GitHub Actions / OTP 24.x / Elixir 1.11.4
|
||
|
||
@doc """ | ||
Given a wallet and returns a map of attributes used to construct a `TTEth.Wallet.t()`. | ||
""" | ||
def new_attrs(t) | ||
|
||
@doc """ | ||
Given a wallet and a hash digest and returns a signature. | ||
""" | ||
def sign(t, digest) | ||
end |
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
Oops, something went wrong.