-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
11 changed files
with
427 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* eslint-disable */ | ||
// sequence-waas-intents v0.1.0 b4f2c5263bf9e85831646dacc7ac913b72430819 | ||
// sequence-waas-intents v0.1.0 aa1ecacec8e54e06aeffbf733fbbad763d533fcb | ||
// -- | ||
// Code generated by [email protected] with typescript generator. DO NOT EDIT. | ||
// | ||
|
@@ -12,7 +12,7 @@ export const WebRPCVersion = "v1" | |
export const WebRPCSchemaVersion = "v0.1.0" | ||
|
||
// Schema hash generated from your RIDL schema | ||
export const WebRPCSchemaHash = "b4f2c5263bf9e85831646dacc7ac913b72430819" | ||
export const WebRPCSchemaHash = "aa1ecacec8e54e06aeffbf733fbbad763d533fcb" | ||
|
||
// | ||
// Types | ||
|
@@ -64,7 +64,7 @@ export interface IntentDataGetSession { | |
wallet: string | ||
} | ||
|
||
export interface IntentDataSign { | ||
export interface IntentDataSignMessage { | ||
network: string | ||
wallet: string | ||
message: string | ||
|
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,48 @@ | ||
package intents | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/0xsequence/ethkit/go-ethereum/common" | ||
"github.com/0xsequence/go-sequence" | ||
) | ||
|
||
func (p *IntentDataSignMessage) chainID() (*big.Int, error) { | ||
n, ok := sequence.ParseHexOrDec(p.Network) | ||
if !ok { | ||
return nil, fmt.Errorf("invalid network id '%s'", p.Network) | ||
} | ||
|
||
return n, nil | ||
} | ||
|
||
func (p *IntentDataSignMessage) message() []byte { | ||
return common.FromHex(p.Message) | ||
} | ||
|
||
func (p *IntentDataSignMessage) wallet() common.Address { | ||
return common.HexToAddress(p.Wallet) | ||
} | ||
|
||
func (p *IntentDataSignMessage) subdigest() ([]byte, error) { | ||
chainID, err := p.chainID() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return sequence.SubDigest(chainID, p.wallet(), sequence.MessageDigest(p.message())) | ||
} | ||
|
||
// A SignMessagePacket (intent) *MUST* be mapped to a regular "SignMessage" Sequence action, this means that | ||
// it must adhere to the following rules: | ||
// - the subdigest must match `SubDigest(chainID, Wallet, Digest(Message))` | ||
func (p *IntentDataSignMessage) IsValidInterpretation(subdigest common.Hash) bool { | ||
selfSubDigest, err := p.subdigest() | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return bytes.Equal(selfSubDigest, subdigest[:]) | ||
} |
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,67 @@ | ||
package intents | ||
|
||
import ( | ||
"encoding/json" | ||
"math/big" | ||
"testing" | ||
"time" | ||
|
||
"github.com/0xsequence/ethkit/ethwallet" | ||
"github.com/0xsequence/ethkit/go-ethereum/common" | ||
"github.com/0xsequence/go-sequence" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRecoverMessageIntent(t *testing.T) { | ||
data := `{ | ||
"version": "1", | ||
"name": "signMessage", | ||
"issued": 0, | ||
"expires": 0, | ||
"data": { | ||
"wallet": "0xD67FC48b298B09Ed3D03403d930769C527186c4e", | ||
"network": "1", | ||
"message": "0xdeadbeef" | ||
}, | ||
"signatures": [] | ||
}` | ||
|
||
intent := &Intent{} | ||
err := json.Unmarshal([]byte(data), intent) | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, "1", intent.Version) | ||
assert.Equal(t, "signMessage", intent.Name) | ||
|
||
hash, err := intent.Hash() | ||
assert.Nil(t, err) | ||
assert.NotNil(t, common.Bytes2Hex(hash)) | ||
|
||
intent.IssuedAt = uint64(time.Now().Unix()) | ||
intent.ExpiresAt = uint64(time.Now().Unix()) + 60 | ||
|
||
wallet, err := ethwallet.NewWalletFromRandomEntropy() | ||
require.Nil(t, err) | ||
|
||
session := NewSessionP256K1(wallet) | ||
|
||
err = session.Sign(intent) | ||
require.Nil(t, err) | ||
|
||
intentTyped, err := NewIntentTypedFromIntent[IntentDataSignMessage](intent) | ||
require.NoError(t, err) | ||
|
||
signers := intent.Signers() | ||
assert.Equal(t, 1, len(signers)) | ||
assert.Equal(t, "0x"+common.Bytes2Hex(append([]byte{0x00}, wallet.Address().Bytes()...)), signers[0]) | ||
|
||
subdigest, err := sequence.SubDigest( | ||
big.NewInt(1), | ||
common.HexToAddress("0xD67FC48b298B09Ed3D03403d930769C527186c4e"), | ||
sequence.MessageDigest(common.Hex2Bytes("deadbeef")), | ||
) | ||
|
||
assert.Nil(t, err) | ||
assert.True(t, intentTyped.Data.IsValidInterpretation(common.BytesToHash(subdigest))) | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.