| 
									
										
										
										
											2015-01-25 02:07:20 +01:00
										 |  |  | package accounts | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2015-03-10 00:09:39 +01:00
										 |  |  | 	"io/ioutil" | 
					
						
							|  |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2015-01-25 02:07:20 +01:00
										 |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2015-03-08 00:18:13 +01:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-20 11:36:50 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/crypto" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/crypto/randentropy" | 
					
						
							| 
									
										
										
										
											2015-01-25 02:07:20 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 00:09:39 +01:00
										 |  |  | func TestSign(t *testing.T) { | 
					
						
							|  |  |  | 	dir, ks := tmpKeyStore(t, crypto.NewKeyStorePlain) | 
					
						
							|  |  |  | 	defer os.RemoveAll(dir) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	am := NewManager(ks) | 
					
						
							| 
									
										
										
										
											2015-01-25 02:07:20 +01:00
										 |  |  | 	pass := "" // not used but required by API | 
					
						
							|  |  |  | 	a1, err := am.NewAccount(pass) | 
					
						
							| 
									
										
										
										
											2015-02-20 11:36:50 +01:00
										 |  |  | 	toSign := randentropy.GetEntropyCSPRNG(32) | 
					
						
							| 
									
										
										
										
											2015-03-10 00:09:39 +01:00
										 |  |  | 	am.Unlock(a1.Address, "") | 
					
						
							| 
									
										
										
										
											2015-02-25 17:29:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 00:09:39 +01:00
										 |  |  | 	_, err = am.Sign(a1, toSign) | 
					
						
							| 
									
										
										
										
											2015-02-25 17:29:23 +01:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 00:09:39 +01:00
										 |  |  | func TestTimedUnlock(t *testing.T) { | 
					
						
							|  |  |  | 	dir, ks := tmpKeyStore(t, crypto.NewKeyStorePassphrase) | 
					
						
							|  |  |  | 	defer os.RemoveAll(dir) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	am := NewManager(ks) | 
					
						
							| 
									
										
										
										
											2015-02-25 17:29:23 +01:00
										 |  |  | 	pass := "foo" | 
					
						
							|  |  |  | 	a1, err := am.NewAccount(pass) | 
					
						
							|  |  |  | 	toSign := randentropy.GetEntropyCSPRNG(32) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Signing without passphrase fails because account is locked | 
					
						
							|  |  |  | 	_, err = am.Sign(a1, toSign) | 
					
						
							|  |  |  | 	if err != ErrLocked { | 
					
						
							| 
									
										
										
										
											2015-03-10 00:09:39 +01:00
										 |  |  | 		t.Fatal("Signing should've failed with ErrLocked before unlocking, got ", err) | 
					
						
							| 
									
										
										
										
											2015-02-25 17:29:23 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Signing with passphrase works | 
					
						
							| 
									
										
										
										
											2015-03-10 00:09:39 +01:00
										 |  |  | 	if err = am.TimedUnlock(a1.Address, pass, 100*time.Millisecond); err != nil { | 
					
						
							| 
									
										
										
										
											2015-02-25 17:29:23 +01:00
										 |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Signing without passphrase works because account is temp unlocked | 
					
						
							|  |  |  | 	_, err = am.Sign(a1, toSign) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2015-03-10 00:09:39 +01:00
										 |  |  | 		t.Fatal("Signing shouldn't return an error after unlocking, got ", err) | 
					
						
							| 
									
										
										
										
											2015-02-25 17:29:23 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 00:09:39 +01:00
										 |  |  | 	// Signing fails again after automatic locking | 
					
						
							|  |  |  | 	time.Sleep(150 * time.Millisecond) | 
					
						
							| 
									
										
										
										
											2015-02-25 17:29:23 +01:00
										 |  |  | 	_, err = am.Sign(a1, toSign) | 
					
						
							|  |  |  | 	if err != ErrLocked { | 
					
						
							| 
									
										
										
										
											2015-03-10 00:09:39 +01:00
										 |  |  | 		t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err) | 
					
						
							| 
									
										
										
										
											2015-02-25 17:29:23 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-10 00:09:39 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-02-25 17:29:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 00:09:39 +01:00
										 |  |  | func tmpKeyStore(t *testing.T, new func(string) crypto.KeyStore2) (string, crypto.KeyStore2) { | 
					
						
							|  |  |  | 	d, err := ioutil.TempDir("", "eth-keystore-test") | 
					
						
							| 
									
										
										
										
											2015-02-24 18:03:10 +01:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-10 00:09:39 +01:00
										 |  |  | 	return d, new(d) | 
					
						
							| 
									
										
										
										
											2015-01-25 02:07:20 +01:00
										 |  |  | } |