-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from tonkeeper/tonconnect-proof
Add tonconnect.CreateSignedProof to generate proof for tonconnect challenge
- Loading branch information
Showing
4 changed files
with
130 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
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,71 @@ | ||
package tonconnect | ||
|
||
import ( | ||
"context" | ||
"crypto/ed25519" | ||
"testing" | ||
"time" | ||
|
||
"github.com/tonkeeper/tongo/liteapi" | ||
"github.com/tonkeeper/tongo/wallet" | ||
) | ||
|
||
func TestCreateSignedProof(t *testing.T) { | ||
cli, err := liteapi.NewClient(liteapi.Testnet()) | ||
if err != nil { | ||
t.Fatalf("liteapi.NewClient() failed: %v", err) | ||
} | ||
tests := []struct { | ||
name string | ||
version wallet.Version | ||
secret string | ||
}{ | ||
{ | ||
name: "v4r2", | ||
version: wallet.V4R2, | ||
secret: "some-random-secret", | ||
}, | ||
{ | ||
name: "v4r1", | ||
version: wallet.V4R1, | ||
secret: "another-random-secret", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
srv, err := NewTonConnect(cli, tt.secret) | ||
if err != nil { | ||
t.Fatalf("NewTonConnect() failed: %v", err) | ||
} | ||
payload, err := srv.GeneratePayload() | ||
if err != nil { | ||
t.Fatalf("GeneratePayload() failed: %v", err) | ||
} | ||
seed := wallet.RandomSeed() | ||
privateKey, err := wallet.SeedToPrivateKey(seed) | ||
if err != nil { | ||
t.Fatalf("SeedToPrivateKey() failed: %v", err) | ||
} | ||
publicKey := privateKey.Public().(ed25519.PublicKey) | ||
stateInit, err := wallet.GenerateStateInit(publicKey, tt.version, 0, nil) | ||
if err != nil { | ||
t.Fatalf("GenerateStateInit() failed: %v", err) | ||
} | ||
accountID, err := wallet.GenerateWalletAddress(publicKey, tt.version, 0, nil) | ||
if err != nil { | ||
t.Fatalf("GenerateWalletAddress() failed: %v", err) | ||
} | ||
signedProof, err := CreateSignedProof(payload, accountID, privateKey, stateInit, ProofOptions{Timestamp: time.Now(), Domain: "web"}) | ||
if err != nil { | ||
t.Fatalf("CreateSignedProof() failed: %v", err) | ||
} | ||
verified, _, err := srv.CheckProof(context.Background(), signedProof) | ||
if err != nil { | ||
t.Fatalf("CheckProof() failed: %v", err) | ||
} | ||
if verified != true { | ||
t.Fatalf("proof is invalid") | ||
} | ||
}) | ||
} | ||
} |
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