/
auth.go
57 lines (47 loc) · 1.24 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package pkg
import (
"encoding/json"
"log"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/sourcenetwork/sourcehub/app"
)
type Session struct {
// SourceHub address of Authenticated user
Actor string
PrivKey cryptotypes.PrivKey
}
func NewSession(key string) Session {
priv := &secp256k1.PrivKey{}
json.Unmarshal([]byte(key), priv)
addr, err := bech32.ConvertAndEncode(app.AccountAddressPrefix, sdk.AccAddress(priv.PubKey().Address()))
if err != nil {
panic(err)
}
log.Printf("authenticated as user: %v", addr)
return Session{
PrivKey: priv,
Actor: addr,
}
}
// Generate a Key Pair, generate a cosmos addr, submit a tx to the Faucet
func NewActor() (string, *secp256k1.PrivKey) {
key := secp256k1.GenPrivKey()
addr := key.PubKey().Address()
accAddr := sdk.AccAddress(addr)
accAddrEnc, err := bech32.ConvertAndEncode(app.AccountAddressPrefix, accAddr)
if err != nil {
panic(err)
}
// TODO register in faucet
return accAddrEnc, key
}
func DumpKey(key *secp256k1.PrivKey) string {
res, err := json.Marshal(key)
if err != nil {
panic(err)
}
return string(res)
}