4
4
// To view a copy of this license, visit https://creativecommons.org/licenses/by/4.0/
5
5
// Author: Andreas Timm
6
6
// Repository: https://github.com/andreas-timm/code-signature-ts
7
- // Version: 0.2 .0
8
- // @sha 256sum 0xdaea6ea29e60619ef1050287fb380a9edc234c8d18e01103a5fb8027694f91f4
9
- // @eip 191signature 0x96958f5875095d4b2967e37dda0b4a696611e2c2fb543ae30be41056f94b97f31d40970caf2ceeec179b2cfe23a7e37a1ef44a817113dc3a368f9296adab93531c
7
+ // Version: 0.3 .0
8
+ // @sha 256sum 0x904d8038d8a3ae6144e5b4275c5f5c76b3d28bddf574ada8cc394fb091a3e2c5
9
+ // @eip 191signature 0x6b55e4c5241cf7824a70f3f971b16be497ad5e2d808daca00280c975a5604a396a2fdfcf5d30d6f7c15c84a4f5a44d813311d7231acc7b2041a1aa20574e8c851b
10
10
11
11
import { parseArgs } from 'util'
12
12
import { hashMessage , recoverAddress , sha256 } from 'viem'
13
13
import { english , generateMnemonic , mnemonicToAccount } from 'viem/accounts'
14
- import type { Options , SignResult , VerifyResult } from './types.ts'
14
+ import type { Options , SignResult , VerifyResult , CliOptions } from './types.ts'
15
15
16
16
export function splitIndexLines ( content : string ) {
17
17
return content . split ( / ( \n + ) / ) . reduce ( ( acc : string [ ] [ ] , part : string , index : number ) => {
@@ -24,14 +24,14 @@ export function splitIndexLines(content: string) {
24
24
} , [ ] )
25
25
}
26
26
27
- export function getFilteredContent ( content : string , key : string , replace ?: string ) {
27
+ export function getFilteredContent ( content : string , key : string , prefix : string , replace ?: string ) {
28
28
const lines = splitIndexLines ( content )
29
29
let filtered = [ ]
30
30
let value : null | `0x${string } ` = null
31
31
let index
32
32
33
33
for ( index = 0 ; index < lines . length ; index ++ ) {
34
- if ( lines [ index ] [ 0 ] . match ( new RegExp ( ' ^\\s*([*#;"]|// )?\\s*' + key ) ) ) {
34
+ if ( lines [ index ] [ 0 ] . match ( new RegExp ( ` ^\\s*([*#;"]|${ prefix } )?\\s*` + key ) ) ) {
35
35
value = lines [ index ] [ 0 ] . match ( new RegExp ( `${ key } \\s+(\\S+)` ) ) ! [ 1 ] as `0x${string } `
36
36
if ( replace !== undefined ) {
37
37
filtered . push (
@@ -51,9 +51,9 @@ export function getFilteredContent(content: string, key: string, replace?: strin
51
51
return { content : filtered . join ( '' ) , value }
52
52
}
53
53
54
- export async function verify ( content : string ) : Promise < VerifyResult > {
55
- const sha256Filtered = getFilteredContent ( content , '@sha256sum' )
56
- const signFiltered = getFilteredContent ( sha256Filtered . content , '@eip191signature' )
54
+ export async function verify ( content : string , prefix : string ) : Promise < VerifyResult > {
55
+ const sha256Filtered = getFilteredContent ( content , '@sha256sum' , prefix )
56
+ const signFiltered = getFilteredContent ( sha256Filtered . content , '@eip191signature' , prefix )
57
57
const sha256sum = sha256 ( new TextEncoder ( ) . encode ( sha256Filtered . content ) )
58
58
59
59
const address =
@@ -97,19 +97,19 @@ export async function sign(verifyResult: VerifyResult, options: Options): Promis
97
97
signature = await account . signMessage ( { message : verifyResult . signFiltered . content } )
98
98
99
99
if ( verifyResult . signFiltered . value !== null ) {
100
- toSha256Content = getFilteredContent ( verifyResult . content , '@eip191signature' , signature ) . content
100
+ toSha256Content = getFilteredContent ( verifyResult . content , '@eip191signature' , options . prefix , signature ) . content
101
101
} else {
102
- toSha256Content = [ '// @eip191signature ' + signature , verifyResult . content ] . join ( '\n' )
102
+ toSha256Content = [ ` ${ options . prefix } @eip191signature ` + signature , verifyResult . content ] . join ( '\n' )
103
103
}
104
104
105
105
sha256sum = sha256 (
106
- new TextEncoder ( ) . encode ( getFilteredContent ( toSha256Content , '@sha256sum' ) . content ) ,
106
+ new TextEncoder ( ) . encode ( getFilteredContent ( toSha256Content , '@sha256sum' , options . prefix ) . content ) ,
107
107
)
108
108
109
109
if ( verifyResult . sha256Filtered . value !== null ) {
110
- signedContent = getFilteredContent ( toSha256Content , '@sha256sum' , sha256sum ) . content
110
+ signedContent = getFilteredContent ( toSha256Content , '@sha256sum' , options . prefix , sha256sum ) . content
111
111
} else {
112
- signedContent = [ '// @sha256sum ' + sha256sum , toSha256Content ] . join ( '\n' )
112
+ signedContent = [ ` ${ options . prefix } @sha256sum ` + sha256sum , toSha256Content ] . join ( '\n' )
113
113
}
114
114
}
115
115
@@ -128,6 +128,7 @@ function printHelp() {
128
128
console . log ( ' --verify, -v — only verify' )
129
129
console . log ( ' --write, -f — write file' )
130
130
console . log ( ' --silent, -s — silent' )
131
+ console . log ( ' --prefix, -p — commented line prefix' )
131
132
console . log ( ' --out, -o — output file' )
132
133
console . log ( 'ENVIRONMENT:' )
133
134
console . log ( ' MNEMONIC — mnemonic' )
@@ -156,7 +157,7 @@ export async function codeSignature(options: Options) {
156
157
content = await Bun . file ( options . filePath ) . text ( )
157
158
}
158
159
159
- const verifyResult = await verify ( content )
160
+ const verifyResult = await verify ( content , options . prefix )
160
161
let signResult : null | SignResult = null
161
162
let fail = false
162
163
@@ -173,8 +174,8 @@ export async function codeSignature(options: Options) {
173
174
} else if ( options . write ) {
174
175
await write ( options , signResult . signedContent )
175
176
} else if ( ! options . silent ) {
176
- console . log ( `// @sha256sum ${ signResult . sha256sum } ` )
177
- console . log ( `// @eip191signature ${ signResult . signature } ` )
177
+ console . log ( `${ options . prefix } @sha256sum ${ signResult . sha256sum } ` )
178
+ console . log ( `${ options . prefix } @eip191signature ${ signResult . signature } ` )
178
179
}
179
180
} else {
180
181
if ( ! options . silent ) {
@@ -187,18 +188,20 @@ export async function codeSignature(options: Options) {
187
188
}
188
189
189
190
if ( import . meta. main ) {
190
- const { values, positionals } = parseArgs ( {
191
+ const { values, positionals} = parseArgs ( {
191
192
args : Bun . argv ,
192
193
options : {
193
194
help : { type : 'boolean' , short : 'h' } ,
194
195
write : { type : 'boolean' , short : 'w' } ,
195
196
verify : { type : 'boolean' , short : 'v' } ,
196
197
silent : { type : 'boolean' , short : 's' } ,
198
+ prefix : { type : 'string' , short : 'p' , default : '//' } ,
197
199
out : { type : 'string' , short : 'o' } ,
198
200
} ,
199
201
strict : true ,
200
202
allowPositionals : true ,
201
203
} )
204
+ const cliOptions : CliOptions = values as CliOptions
202
205
203
206
if ( values . help ) {
204
207
printHelp ( )
@@ -212,7 +215,7 @@ if (import.meta.main) {
212
215
213
216
const mnemonic = process . env . MNEMONIC
214
217
215
- const res = await codeSignature ( { filePath : positionals [ 2 ] , ...values , mnemonic } )
218
+ const res = await codeSignature ( { filePath : positionals [ 2 ] , ...cliOptions , mnemonic } )
216
219
217
220
if ( res . fail ) {
218
221
process . exit ( 1 )
0 commit comments