-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathweb3-wallet.html
187 lines (151 loc) · 5.07 KB
/
web3-wallet.html
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-localstorage/iron-localstorage.html">
<script src="../web3/dist/web3.min.js"></script>
<script src="../hooked-web3-provider/build/hooked-web3-provider.min.js"></script>
<script src="../eth-lightwallet/dist/lightwallet.min.js"></script>
<!--
A polymer wrapper to web3js and lightwallet libraries to build blockchain apps in polymer.
Example:
<web3-wallet password="{{password}}" host="http://<yourRPCnode>:8545" account="{{account}}" web3="{{web3}}" keystore="{{keystore}}"></web3-wallet>
@demo
-->
<dom-module id="web3-wallet">
<template>
<iron-localstorage
id="localstorage"
name="{{walletname}}"
value="{{walletData}}"
on-iron-localstorage-load-empty="_createWallet"
on-iron-localstorage-load="_loadWallet"></iron-localstorage>
</template>
</dom-module>
<script>
Polymer({
is: 'web3-wallet',
properties: {
/** key in localstorage under which to store your serialized wallet. */
walletname: {
type: String,
value: 'web3-wallet'
},
/** password to encrypt your wallet with. */
password: {
type: String,
observer: '_initwallet'
},
/** number of addresses to create when creating a new wallet. */
generateaddresses: {
type: Number,
value: 1
},
/** URL of the ethereum node to connect to */
host: {
type: String,
value: "http://localhost:8545"
},
/** address of the first account in your wallet */
account: {
type: String,
notify: true
},
/** balance in Wei of the first account in your wallet ( convert to Ether = balance/1e18 ) */
balance: {
type:String,
notify: true
},
/** your encrypted keystore in JSON format */
keystore: {
type: Object,
notify: true
},
web3: {
type: Object,
notify: true
},
},
// Element Lifecycle
ready: function() {
// `ready` is called after all elements have been configured, but
// propagates bottom-up. This element's children are ready, but parents
// are not.
//
// This is the point where you should make modifications to the DOM (when
// necessary), or kick off any processes the element wants to perform.
},
attached: function() {
// `attached` fires once the element and its parents have been inserted
// into a document.
//
// This is a good place to perform any work related to your element's
// visual state or active behavior (measuring sizes, beginning animations,
// loading resources, etc).
},
detached: function() {
// The analog to `attached`, `detached` fires when the element has been
// removed from a document.
//
// Use this to clean up anything you did in `attached`.
},
// Element Behavior
/**
* fetches the balance of the first account - updates the property 'balance'
*/
updateBalance: function(){
this.balance = this.web3.eth.getBalance(this.account).toNumber();
return this.balance;
},
_loadWallet: function() {
this.keystore = new lightwallet.keystore.deserialize(this.walletData);
this.createwallet = false;
this._initwallet();
},
_createWallet: function() {
this.createwallet = true;
this._initwallet();
},
_initwallet: function() {
if (!this.password){
return;
}
if (this.createwallet){
var self = this;
var secretSeed = lightwallet.keystore.generateRandomSeed();
lightwallet.keystore.deriveKeyFromPassword(this.password, function(err, pwDerivedKey) {
self.keystore = new lightwallet.keystore(secretSeed, pwDerivedKey);
self.keystore.generateNewAddress(pwDerivedKey, self.generateaddresses);
self.set('walletData', self.keystore.serialize());
self._initweb3();
});
}else{
if (this.keystore){
this._initweb3();
}
}
},
_initweb3: function(){
this.account = this.keystore.getAddresses()[0];
var web3Provider = new HookedWeb3Provider({
host: this.host,
transaction_signer: this.keystore
});
var web3 = new Web3();
web3.setProvider(web3Provider);
var self=this;
this.keystore.passwordProvider = function(callback) {
callback(null, self.password);
};
this.web3 = web3;
this.updateBalance();
this.fire('web3-ready');
}
});
</script>