-
Notifications
You must be signed in to change notification settings - Fork 16
/
proof.go
50 lines (46 loc) · 1.35 KB
/
proof.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
package tonconnect
import (
"crypto/ed25519"
"encoding/base64"
"time"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
)
// ProofOptions configures particular aspects of a proof.
type ProofOptions struct {
Timestamp time.Time
Domain string
}
// CreateSignedProof returns a proof that the caller posses a private key of a particular account.
// This can be used on the client side,
// when the server side runs tonconnect.Server or any other server implementation of ton-connect.
func CreateSignedProof(payload string, accountID tongo.AccountID, privateKey ed25519.PrivateKey, stateInit tlb.StateInit, options ProofOptions) (*Proof, error) {
stateInitCell := boc.NewCell()
if err := tlb.Marshal(stateInitCell, stateInit); err != nil {
return nil, err
}
stateInitBase64, err := stateInitCell.ToBocBase64()
if err != nil {
return nil, err
}
proof := Proof{
Address: accountID.String(),
Proof: ProofData{
Timestamp: options.Timestamp.Unix(),
Domain: options.Domain,
Payload: payload,
StateInit: stateInitBase64,
},
}
parsedMsg, err := convertTonProofMessage(&proof)
if err != nil {
return nil, err
}
msg, err := createMessage(parsedMsg)
if err != nil {
return nil, err
}
proof.Proof.Signature = base64.StdEncoding.EncodeToString(signMessage(privateKey, msg))
return &proof, nil
}