forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SignTypedData to native Keystore interface (ethereum#1220)
* Add SignTypedData to native Keystore interface * Add simple smoke test for the signTypedData method in the native interface
- Loading branch information
Showing
3 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
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,123 @@ | ||
package geth | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/keystore" | ||
) | ||
|
||
const jsonTypedData = ` | ||
{ | ||
"types": { | ||
"EIP712Domain": [ | ||
{ | ||
"name": "name", | ||
"type": "string" | ||
}, | ||
{ | ||
"name": "version", | ||
"type": "string" | ||
}, | ||
{ | ||
"name": "chainId", | ||
"type": "uint256" | ||
}, | ||
{ | ||
"name": "verifyingContract", | ||
"type": "address" | ||
} | ||
], | ||
"Person": [ | ||
{ | ||
"name": "name", | ||
"type": "string" | ||
}, | ||
{ | ||
"name": "test", | ||
"type": "uint8" | ||
}, | ||
{ | ||
"name": "wallet", | ||
"type": "address" | ||
} | ||
], | ||
"Mail": [ | ||
{ | ||
"name": "from", | ||
"type": "Person" | ||
}, | ||
{ | ||
"name": "to", | ||
"type": "Person" | ||
}, | ||
{ | ||
"name": "contents", | ||
"type": "string" | ||
} | ||
] | ||
}, | ||
"primaryType": "Mail", | ||
"domain": { | ||
"name": "Ether Mail", | ||
"version": "1", | ||
"chainId": "1", | ||
"verifyingContract": "0xCCCcccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" | ||
}, | ||
"message": { | ||
"from": { | ||
"name": "Cow", | ||
"test": 3, | ||
"wallet": "0xcD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" | ||
}, | ||
"to": { | ||
"name": "Bob", | ||
"test": 4, | ||
"wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB" | ||
}, | ||
"contents": "Hello, Bob!" | ||
} | ||
} | ||
` | ||
|
||
const scryptN = 2 | ||
const scryptP = 1 | ||
|
||
// copied from the keystore_test.go file in the keystore package | ||
func tmpKeyStore(t *testing.T, encrypted bool) (string, *keystore.KeyStore) { | ||
d, err := ioutil.TempDir("", "eth-keystore-test") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
newKs := keystore.NewPlaintextKeyStore | ||
if encrypted { | ||
newKs = func(kd string) *keystore.KeyStore { | ||
return keystore.NewKeyStore(kd, scryptN, scryptP) | ||
} | ||
} | ||
return d, newKs(d) | ||
} | ||
|
||
func TestSignTypedData(t *testing.T) { | ||
dir, ks := tmpKeyStore(t, true) | ||
defer os.RemoveAll(dir) | ||
|
||
pass := "" // not used but required by API | ||
a1, err := ks.NewAccount(pass) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
nks := &KeyStore{keystore: ks} | ||
nAcc := &Account{account: a1} | ||
if err := nks.Unlock(nAcc, ""); err != nil { | ||
t.Fatal(err) | ||
} | ||
signature, err := nks.SignTypedData(nAcc, []byte(jsonTypedData)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if signature == nil || len(signature) != 65 { | ||
t.Errorf("Expected 65 byte signature (got %d bytes)", len(signature)) | ||
} | ||
} |