-
Notifications
You must be signed in to change notification settings - Fork 10
/
sign-message.ts
185 lines (169 loc) · 5.15 KB
/
sign-message.ts
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
/* eslint-disable @typescript-eslint/restrict-template-expressions */
import { copyable, divider, heading, panel, text } from '@metamask/snaps-ui'
import { base64pad } from 'iso-base/rfc4648'
import * as Address from 'iso-filecoin/address'
import { Message, Schemas } from 'iso-filecoin/message'
import { Token } from 'iso-filecoin/token'
import { signMessage as filSignMessage, sign } from 'iso-filecoin/wallet'
import { z } from 'zod'
import type { SignedMessage, SnapContext, SnapResponse } from '../types'
import { serializeError, snapDialog } from '../utils'
import { getAccount } from '../account'
import { RPC } from 'iso-filecoin/rpc'
import { parseDerivationPath } from 'iso-filecoin/utils'
// Schemas
export const signMessageParams = Schemas.messagePartial.omit({
from: true,
})
export const signMessageRawParams = z.object({
message: z.string(),
})
// Types
export type SignMessageParams = z.infer<typeof signMessageParams>
export type SignMessageRawParams = z.infer<typeof signMessageRawParams>
export interface SignMessageRequest {
method: 'fil_signMessage'
params: SignMessageParams
}
export interface SignMessageRawRequest {
method: 'fil_signMessageRaw'
params: SignMessageRawParams
}
export type SignMessageResponse = SnapResponse<SignedMessage>
export type SignMessageRawResponse = SnapResponse<string>
/**
* Sign a message
*
* @param ctx - Snap context
* @param params - The message request
*/
export async function signMessage(
ctx: SnapContext,
params: SignMessageParams
): Promise<SignMessageResponse> {
const config = await ctx.state.get(ctx.origin)
if (config == null) {
return serializeError(
`No configuration found for ${ctx.origin}. Connect to Filsnap first.`
)
}
const _params = signMessageParams.safeParse(params)
if (!_params.success) {
return serializeError(
`Invalid params ${_params.error.message}`,
_params.error
)
}
const account = await getAccount(snap, config)
const rpc = new RPC({
api: config.rpc.url,
network: config.network,
})
// create Message
const message = await new Message({
to: Address.from(_params.data.to, config.network).toString(),
from: account.address.toString(),
value: _params.data.value,
nonce: _params.data.nonce,
gasFeeCap: _params.data.gasFeeCap,
gasLimit: _params.data.gasLimit,
gasPremium: _params.data.gasPremium,
params: _params.data.params,
method: _params.data.method,
}).prepare(rpc)
const gas = Token.fromAttoFIL(message.gasPremium).mul(message.gasLimit)
const total = Token.fromAttoFIL(message.value).add(gas)
const { account: accountNumber } = parseDerivationPath(config.derivationPath)
const conf = await snapDialog(ctx.snap, {
type: 'confirmation',
content: panel([
heading(`Request from ${ctx.origin}`),
text(
`Send **${Token.fromAttoFIL(message.value).toFIL().toString()} ${
config.unit?.symbol ?? 'FIL'
}** from Account ${accountNumber}`
),
copyable(message.from),
text(`to`),
copyable(message.to),
divider(),
heading('Details'),
text(`Gas _(estimated ${config.unit?.symbol ?? 'FIL'})_:`),
copyable(
gas.toFIL().toFormat({
decimalPlaces: config.unit?.decimals,
})
),
text(`Total _(amount + gas ${config.unit?.symbol ?? 'FIL'})_:`),
copyable(
total.toFIL().toFormat({
decimalPlaces: config.unit?.decimals,
})
),
text('API:'),
copyable(config.rpc.url),
text('Network:'),
copyable(config.network),
]),
})
if (conf) {
const sig = filSignMessage(account.privateKey, 'SECP256K1', message)
return {
error: null,
result: {
message,
signature: {
data: base64pad.encode(sig),
type: 'SECP256K1',
},
},
}
}
return serializeError('User denied message signing')
}
/**
* Sign a raw message
*
* @param ctx - Snap context
* @param params - Parameters
* @param params.message - The raw message
*/
export async function signMessageRaw(
ctx: SnapContext,
params: SignMessageRawParams
): Promise<SignMessageRawResponse> {
const config = await ctx.state.get(ctx.origin)
if (config == null) {
return serializeError(
`No configuration found for ${ctx.origin}. Connect to Filsnap first.`
)
}
const _params = signMessageRawParams.safeParse(params)
if (!_params.success) {
return serializeError(
`Invalid params ${_params.error.message}`,
_params.error
)
}
const { message } = _params.data
const account = await getAccount(snap, config)
const { account: accountNumber } = parseDerivationPath(config.derivationPath)
const conf = await snapDialog(ctx.snap, {
type: 'confirmation',
content: panel([
heading(`Request from ${ctx.origin}`),
text(
`Do you want to sign this message with **Account ${accountNumber}** _${account.address.toString()}_?`
),
copyable(message),
]),
})
if (conf) {
const sig = sign(account.privateKey, 'SECP256K1', message)
return {
error: null,
result: base64pad.encode(sig),
}
}
return serializeError('User denied message signing')
}