User Signing
This is a relatively advanced use case. If you use the signed typed data JSONRPC endpoint, Fortmatic will support this as well.

Signing Methods

Web3 Pre-1.0 Personal Sign
Web3 1.0-Beta Personal Sign
Sign Typed Data v1
Sign Typed Data v3
Sign Typed Data v4
1
// Initialize provider
2
import Fortmatic from 'fortmatic';
3
import Web3 from 'web3';
4
​
5
// Required to convert message to Hex
6
const ethUtil = require('ethereumjs-util');
7
​
8
const fm = new Fortmatic('YOUR_API_KEY');
9
window.web3 = new Web3(fm.getProvider());
10
​
11
web3.eth.getAccounts((error, accounts) => {
12
13
if (error) throw error;
14
15
const from = accounts[0];
16
const msg = ethUtil.bufferToHex(new Buffer('YOUR_MESSAGE', 'utf8'));
17
const params = [msg, from];
18
const method = 'personal_sign';
19
20
web3.currentProvider.sendAsync({
21
id: 1,
22
method,
23
params,
24
from,
25
}, function(error, result) {
26
if (error) throw error;
27
console.log(result);
28
});
29
​
30
});
Copied!
1
// Initialize provider
2
import Fortmatic from 'fortmatic';
3
import Web3 from 'web3';
4
​
5
const fm = new Fortmatic('YOUR_API_KEY');
6
window.web3 = new Web3(fm.getProvider());
7
​
8
// Personal Sign
9
const text = 'YOUR_MESSAGE';
10
​
11
const accounts = await web3.eth.getAccounts();
12
const from = accounts[0];
13
​
14
const result = await web3.eth.personal.sign(text, from);
Copied!
1
// Initialize provider
2
import Fortmatic from 'fortmatic';
3
import Web3 from 'web3';
4
​
5
const fm = new Fortmatic('YOUR_API_KEY');
6
window.web3 = new Web3(fm.getProvider());
7
​
8
web3.eth.getAccounts(function(error, accounts) {
9
10
if (error) throw error;
11
12
const from = accounts[0];
13
const msg = [
14
{
15
type: 'string',
16
name: 'fullName',
17
value: 'John Doe'
18
},
19
{
20
type: 'uint32',
21
name: 'userId',
22
value: '1234'
23
}
24
];
25
const params = [msg, from];
26
const method = 'eth_signTypedData';
27
28
web3.currentProvider.sendAsync({
29
id: 1,
30
method,
31
params,
32
from,
33
}, function(error, result) {
34
if (error) throw error;
35
console.log(result);
36
});
37
​
38
});
Copied!
​
On Web3 1.0 Beta 55? For all signing methods, replace this...
1
web3.currentProvider.sendAsync({
2
id: 1,
3
method,
4
params,
5
from,
6
}, function(error, result) {
7
if (error) throw error;
8
console.log(result);
9
});
Copied!
...with this
1
web3.currentProvider.sendPayload({
2
id: 1,
3
method,
4
params,
5
from,
6
}).then(console.log);
Copied!
This is based on EIP 712.
1
// Initialize provider
2
import Fortmatic from 'fortmatic';
3
import Web3 from 'web3';
4
​
5
const fm = new Fortmatic('YOUR_API_KEY');
6
window.web3 = new Web3(fm.getProvider());
7
​
8
web3.eth.getAccounts(function(error, accounts) {
9
10
if (error) throw error;
11
12
const from = accounts[0];
13
const payload = {
14
"types": {
15
"EIP712Domain": [
16
{
17
"name":"name",
18
"type":"string"
19
},
20
{
21
"name":"version",
22
"type":"string"
23
},
24
{
25
"name":"verifyingContract",
26
"type":"address"
27
}
28
],
29
"Greeting": [
30
{
31
"name":"contents",
32
"type":"string"
33
}
34
]
35
},
36
"primaryType":"Greeting",
37
"domain":{
38
"name":"Fortmatic",
39
"version":"1",
40
"verifyingContract":"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
41
},
42
"message":{
43
"contents":"Hello, from Fortmatic!"
44
}
45
}
46
47
const params = [from, payload];
48
const method = 'eth_signTypedData_v3';
49
50
web3.currentProvider.sendAsync({
51
id: 1,
52
method,
53
params,
54
from,
55
}, function(error, result) {
56
if (error) throw error;
57
console.log(result);
58
});
59
60
});
Copied!
​
On Web3 1.0 Beta 55? For all signing methods, replace this...
1
web3.currentProvider.sendAsync({
2
id: 1,
3
method,
4
params,
5
from,
6
}, function(error, result) {
7
if (error) throw error;
8
console.log(result);
9
});
Copied!
...with this
1
web3.currentProvider.sendPayload({
2
id: 1,
3
method,
4
params,
5
from,
6
}).then(console.log);
Copied!
This is based on EIP 712.
1
// Initialize provider
2
import Fortmatic from 'fortmatic';
3
import Web3 from 'web3';
4
​
5
const fm = new Fortmatic('YOUR_API_KEY');
6
window.web3 = new Web3(fm.getProvider());
7
​
8
web3.eth.getAccounts(function(error, accounts) {
9
10
if (error) throw error;
11
12
const from = accounts[0];
13
const payload = {
14
"types": {
15
"EIP712Domain": [
16
{
17
"name":"name",
18
"type":"string"
19
},
20
{
21
"name":"version",
22
"type":"string"
23
},
24
{
25
"name":"verifyingContract",
26
"type":"address"
27
}
28
],
29
"Greeting": [
30
{
31
"name":"contents",
32
"type":"string"
33
}
34
]
35
},
36
"primaryType":"Greeting",
37
"domain":{
38
"name":"Fortmatic",
39
"version":"1",
40
"verifyingContract":"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
41
},
42
"message":{
43
"contents":"Hello, from Fortmatic!"
44
}
45
}
46
47
const params = [from, payload];
48
const method = 'eth_signTypedData_v4';
49
50
web3.currentProvider.sendAsync({
51
id: 1,
52
method,
53
params,
54
from,
55
}, function(error, result) {
56
if (error) throw error;
57
console.log(result);
58
});
59
60
});
Copied!
On Web3 1.0 Beta 55? For all signing methods, replace this...
1
web3.currentProvider.sendAsync({
2
id: 1,
3
method,
4
params,
5
from,
6
}, function(error, result) {
7
if (error) throw error;
8
console.log(result);
9
});
Copied!
...with this
1
web3.currentProvider.sendPayload({
2
id: 1,
3
method,
4
params,
5
from,
6
}).then(console.log);
Copied!
​
​
​
​
Last modified 1yr ago
Copy link