Skip to content

Commit 275480e

Browse files
committed
feat: add cli option "prefix"
1 parent f4210a9 commit 275480e

File tree

6 files changed

+66
-36
lines changed

6 files changed

+66
-36
lines changed

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Changelog
2+
3+
## [0.3.0] - 2024-06-13
4+
5+
### Added
6+
- Cli option "prefix" for the commented line prefix.
7+
8+
## [0.2.0] - 2024-05-15
9+
10+
### Added
11+
- Initial import
12+

README.md

+14-4
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ This version maintains the clarity and objectives of your original text while sl
3030
## Compatibility
3131
This tool has been developed and tested exclusively on **macOS**. It is not guaranteed to work on other operating systems.
3232

33-
## Limitations
34-
Only comments of the double slash "`//`" type can be used at this time.
35-
3633
## Install
3734
```shell
3835
git clone --depth=1 https://github.com/andreas-timm/code-signature-ts.git ~/.local/share/code-signature-ts
@@ -48,6 +45,20 @@ ln -s ~/.local/share/code-signature-ts/src/code-signature.ts ~/.local/bin/code-s
4845
```
4946

5047
## Usage
48+
### Help
49+
```shell
50+
$ code-signature -h
51+
Usage: code-signature-ts [OPTIONS] <FILE|->
52+
OPTIONS:
53+
--verify, -v — only verify
54+
--write, -f — write file
55+
--silent, -s — silent
56+
--prefix, -p — commented line prefix
57+
--out, -o — output file
58+
ENVIRONMENT:
59+
MNEMONIC — mnemonic
60+
```
61+
5162
### Check
5263
```shell
5364
code-signature -v src/code-signature.ts
@@ -60,7 +71,6 @@ MNEMONIC=$(pass show mnemonic) code-signature --write code-file.ts
6071
```
6172

6273
## Roadmap
63-
6474
- [ ] Add a demonstration static (SSG) frontend.
6575

6676
## License

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "code-signature-ts",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"module": "src/code-signature.ts",
55
"devDependencies": {
66
"@types/bun": "^1.1.2",

src/code-signature.ts

+22-19
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
// To view a copy of this license, visit https://creativecommons.org/licenses/by/4.0/
55
// Author: Andreas Timm
66
// Repository: https://github.com/andreas-timm/code-signature-ts
7-
// Version: 0.2.0
8-
// @sha256sum 0xdaea6ea29e60619ef1050287fb380a9edc234c8d18e01103a5fb8027694f91f4
9-
// @eip191signature 0x96958f5875095d4b2967e37dda0b4a696611e2c2fb543ae30be41056f94b97f31d40970caf2ceeec179b2cfe23a7e37a1ef44a817113dc3a368f9296adab93531c
7+
// Version: 0.3.0
8+
// @sha256sum 0x904d8038d8a3ae6144e5b4275c5f5c76b3d28bddf574ada8cc394fb091a3e2c5
9+
// @eip191signature 0x6b55e4c5241cf7824a70f3f971b16be497ad5e2d808daca00280c975a5604a396a2fdfcf5d30d6f7c15c84a4f5a44d813311d7231acc7b2041a1aa20574e8c851b
1010

1111
import { parseArgs } from 'util'
1212
import { hashMessage, recoverAddress, sha256 } from 'viem'
1313
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'
1515

1616
export function splitIndexLines(content: string) {
1717
return content.split(/(\n+)/).reduce((acc: string[][], part: string, index: number) => {
@@ -24,14 +24,14 @@ export function splitIndexLines(content: string) {
2424
}, [])
2525
}
2626

27-
export function getFilteredContent(content: string, key: string, replace?: string) {
27+
export function getFilteredContent(content: string, key: string, prefix: string, replace?: string) {
2828
const lines = splitIndexLines(content)
2929
let filtered = []
3030
let value: null | `0x${string}` = null
3131
let index
3232

3333
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))) {
3535
value = lines[index][0].match(new RegExp(`${key}\\s+(\\S+)`))![1] as `0x${string}`
3636
if (replace !== undefined) {
3737
filtered.push(
@@ -51,9 +51,9 @@ export function getFilteredContent(content: string, key: string, replace?: strin
5151
return { content: filtered.join(''), value }
5252
}
5353

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)
5757
const sha256sum = sha256(new TextEncoder().encode(sha256Filtered.content))
5858

5959
const address =
@@ -97,19 +97,19 @@ export async function sign(verifyResult: VerifyResult, options: Options): Promis
9797
signature = await account.signMessage({ message: verifyResult.signFiltered.content })
9898

9999
if (verifyResult.signFiltered.value !== null) {
100-
toSha256Content = getFilteredContent(verifyResult.content, '@eip191signature', signature).content
100+
toSha256Content = getFilteredContent(verifyResult.content, '@eip191signature', options.prefix, signature).content
101101
} else {
102-
toSha256Content = ['// @eip191signature ' + signature, verifyResult.content].join('\n')
102+
toSha256Content = [`${options.prefix} @eip191signature ` + signature, verifyResult.content].join('\n')
103103
}
104104

105105
sha256sum = sha256(
106-
new TextEncoder().encode(getFilteredContent(toSha256Content, '@sha256sum').content),
106+
new TextEncoder().encode(getFilteredContent(toSha256Content, '@sha256sum', options.prefix).content),
107107
)
108108

109109
if (verifyResult.sha256Filtered.value !== null) {
110-
signedContent = getFilteredContent(toSha256Content, '@sha256sum', sha256sum).content
110+
signedContent = getFilteredContent(toSha256Content, '@sha256sum', options.prefix, sha256sum).content
111111
} else {
112-
signedContent = ['// @sha256sum ' + sha256sum, toSha256Content].join('\n')
112+
signedContent = [`${options.prefix} @sha256sum ` + sha256sum, toSha256Content].join('\n')
113113
}
114114
}
115115

@@ -128,6 +128,7 @@ function printHelp() {
128128
console.log(' --verify, -v — only verify')
129129
console.log(' --write, -f — write file')
130130
console.log(' --silent, -s — silent')
131+
console.log(' --prefix, -p — commented line prefix')
131132
console.log(' --out, -o — output file')
132133
console.log('ENVIRONMENT:')
133134
console.log(' MNEMONIC — mnemonic')
@@ -156,7 +157,7 @@ export async function codeSignature(options: Options) {
156157
content = await Bun.file(options.filePath).text()
157158
}
158159

159-
const verifyResult = await verify(content)
160+
const verifyResult = await verify(content, options.prefix)
160161
let signResult: null | SignResult = null
161162
let fail = false
162163

@@ -173,8 +174,8 @@ export async function codeSignature(options: Options) {
173174
} else if (options.write) {
174175
await write(options, signResult.signedContent)
175176
} 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}`)
178179
}
179180
} else {
180181
if (!options.silent) {
@@ -187,18 +188,20 @@ export async function codeSignature(options: Options) {
187188
}
188189

189190
if (import.meta.main) {
190-
const { values, positionals } = parseArgs({
191+
const {values, positionals} = parseArgs({
191192
args: Bun.argv,
192193
options: {
193194
help: { type: 'boolean', short: 'h' },
194195
write: { type: 'boolean', short: 'w' },
195196
verify: { type: 'boolean', short: 'v' },
196197
silent: { type: 'boolean', short: 's' },
198+
prefix: { type: 'string', short: 'p', default: '//' },
197199
out: { type: 'string', short: 'o' },
198200
},
199201
strict: true,
200202
allowPositionals: true,
201203
})
204+
const cliOptions: CliOptions = values as CliOptions
202205

203206
if (values.help) {
204207
printHelp()
@@ -212,7 +215,7 @@ if (import.meta.main) {
212215

213216
const mnemonic = process.env.MNEMONIC
214217

215-
const res = await codeSignature({ filePath: positionals[2], ...values, mnemonic })
218+
const res = await codeSignature({ filePath: positionals[2], ...cliOptions, mnemonic })
216219

217220
if (res.fail) {
218221
process.exit(1)

src/types.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import type { Address } from 'viem'
22

3-
export type Options = {
3+
export type CliOptions = {
4+
help?: boolean,
5+
write?: boolean,
6+
verify?: boolean,
7+
silent?: boolean,
8+
prefix: string,
9+
out?: string,
10+
}
11+
12+
export type Options = CliOptions & {
413
filePath: string
5-
verify?: boolean
6-
write?: boolean
7-
silent?: boolean
8-
out?: string
914
mnemonic?: string
1015
}
1116

test/code-signature.test.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ test('main', async () => {
1515

1616
const tmpFileObj = tmp.fileSync()
1717

18-
await codeSignature({ filePath, write: true, mnemonic, out: tmpFileObj.name, silent: true })
18+
await codeSignature({ filePath, write: true, mnemonic, out: tmpFileObj.name, prefix: '//', silent: true })
1919
const signedContent = await Bun.file(tmpFileObj.name).text()
2020

21+
expect(signedContent).toContain(`// @sha256sum 0x786045f7ccc832aa68563116823b53c9d4a0d2755b06badd38c9b41f7accb694`)
2122
expect(signedContent).toContain(
22-
`// @sha256sum 0x786045f7ccc832aa68563116823b53c9d4a0d2755b06badd38c9b41f7accb694`,
23+
[
24+
'// @sha256sum 0x786045f7ccc832aa68563116823b53c9d4a0d2755b06badd38c9b41f7accb694',
25+
'// @eip191signature 0xde9fec4e5fd25321eafc169b0c3e80d28e98adbbbfcfc4339a6995b90174ad197912f0552867f8d562c6c33b42eaac525c05acc1cd21161206d4734f1ac3f6821b',
26+
].join('\n'),
2327
)
24-
expect(signedContent).toContain([
25-
'// @sha256sum 0x786045f7ccc832aa68563116823b53c9d4a0d2755b06badd38c9b41f7accb694',
26-
'// @eip191signature 0xde9fec4e5fd25321eafc169b0c3e80d28e98adbbbfcfc4339a6995b90174ad197912f0552867f8d562c6c33b42eaac525c05acc1cd21161206d4734f1ac3f6821b',
27-
].join('\n'))
2828

29-
const { verifyResult } = await codeSignature({ filePath: tmpFileObj.name, verify: true })
29+
const { verifyResult } = await codeSignature({ filePath: tmpFileObj.name, prefix: '//', verify: true })
3030
expect(verifyResult.address).toEqual(account.address)
3131

3232
unlinkSync(tmpFileObj.name)

0 commit comments

Comments
 (0)