-
Notifications
You must be signed in to change notification settings - Fork 0
/
web23.go
84 lines (74 loc) · 1.96 KB
/
web23.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package pkg
import (
"crypto/ecdsa"
"encoding/json"
"errors"
"selfweb3/backend/pkg/rscrypto"
"github.com/ethereum/go-ethereum/common/hexutil"
)
const (
C_SelfID = "SelfID"
C_Web2Data = "Web2Data"
C_Web2Address = "Web2Address"
C_AuthorizeID = "AuthorizeID"
C_AuthorizeCode = "AuthorizeCode"
C_Web2NetPublic = "Web2NetPublic"
)
type Web2Data struct {
TOTPKey []byte
Web2DHKey []byte
Web2Public []byte
Web3Public []byte
WebAuthnKey []byte
SelfAddress string
// TODO: web3 storage
VerifyNonce int64
}
func Web2EncodeEx(priavateKey *ecdsa.PrivateKey, public string, data any) (string, error) {
publicKey, err := rscrypto.GetPublicKey(public)
if err != nil {
return "", err
}
return Web2Encode(priavateKey, publicKey, data)
}
func Web2Encode(priavateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey, data any) (string, error) {
if publicKey == nil {
return "", errors.New("invalid web2 network public key")
}
dataBuf, err := json.Marshal(data)
if err != nil {
return "", err
}
dhKey, err := rscrypto.GetDhKey(publicKey, priavateKey)
if err != nil {
return "", err
}
return hexutil.Encode(rscrypto.AesEncryptECB(dataBuf, []byte(dhKey))), nil
}
func Web2DecodeEx(privateKey *ecdsa.PrivateKey, public, data string, ptrObject any) error {
publicKey, err := rscrypto.GetPublicKey(public)
if err != nil {
return err
}
return Web2Decode(privateKey, publicKey, data, ptrObject)
}
func Web2Decode(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey, data string, ptrObject any) error {
if publicKey == nil {
return errors.New("invalid web2 network public key")
}
dataBuf, err := hexutil.Decode(data)
if err != nil {
return err
}
dhKey, err := rscrypto.GetDhKey(publicKey, privateKey)
if err != nil {
return err
}
if ptrObject == nil {
return errors.New("invalid ptrObject for Web2Decode")
}
if err := json.Unmarshal(rscrypto.AesDecryptECB(dataBuf, []byte(dhKey)), ptrObject); err != nil {
return err
}
return nil
}