Skip to content

Commit 3d1a6bb

Browse files
committed
fix: update, bump version 0.4.0
1 parent 275480e commit 3d1a6bb

File tree

4 files changed

+72
-73
lines changed

4 files changed

+72
-73
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [0.4.0] - 2024-06-14
4+
- Fix new line
5+
- Update / improve
6+
37
## [0.3.0] - 2024-06-13
48

59
### Added

package.json

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

src/code-signature.ts

+61-66
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,38 @@
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.3.0
8-
// @sha256sum 0x904d8038d8a3ae6144e5b4275c5f5c76b3d28bddf574ada8cc394fb091a3e2c5
9-
// @eip191signature 0x6b55e4c5241cf7824a70f3f971b16be497ad5e2d808daca00280c975a5604a396a2fdfcf5d30d6f7c15c84a4f5a44d813311d7231acc7b2041a1aa20574e8c851b
7+
// Version: 0.4.0
8+
// @sha256sum 0xe901425d12469ef7e0fdd010e2d158d5398a8a6ae7e1a79bce1f77a6b9d3237b
9+
// @eip191signature 0x3b08f924e994205005312fec95c30e181fb066744ca51a17d122221953cda7e37abec4184e1755385cae7837cea1a6e6f74b3d336484026338bf6d2426e9f9be1c
1010

1111
import { parseArgs } from 'util'
1212
import { hashMessage, recoverAddress, sha256 } from 'viem'
1313
import { english, generateMnemonic, mnemonicToAccount } from 'viem/accounts'
1414
import type { Options, SignResult, VerifyResult, CliOptions } from './types.ts'
1515

16-
export function splitIndexLines(content: string) {
17-
return content.split(/(\n+)/).reduce((acc: string[][], part: string, index: number) => {
18-
if (index % 2 === 0) {
19-
acc.push([part])
20-
} else {
21-
acc[acc.length - 1].push(part)
22-
}
23-
return acc
24-
}, [])
25-
}
26-
2716
export function getFilteredContent(content: string, key: string, prefix: string, replace?: string) {
28-
const lines = splitIndexLines(content)
2917
let filtered = []
3018
let value: null | `0x${string}` = null
19+
const lines = content.split('\n')
3120
let index
21+
let line: string
3222

3323
for (index = 0; index < lines.length; index++) {
34-
if (lines[index][0].match(new RegExp(`^\\s*([*#;"]|${prefix})?\\s*` + key))) {
35-
value = lines[index][0].match(new RegExp(`${key}\\s+(\\S+)`))![1] as `0x${string}`
24+
line = lines[index]
25+
26+
if (value === null && line.match(new RegExp(`^\\s*([*#;"]|${prefix})?\\s*${key} 0x\\S+`))) {
27+
value = line.match(new RegExp(`${key}\\s+(0x\\S+)`))![1] as `0x${string}`
3628
if (replace !== undefined) {
37-
filtered.push(
38-
[
39-
lines[index][0].replace(new RegExp(`(${key}\\s+)\\S+`), `\$1${replace}`),
40-
lines[index][1],
41-
].join(''),
42-
)
29+
line = line.replace(new RegExp(`(${key}\\s+)\\S+`), `\$1${replace}`)
30+
} else {
31+
continue
4332
}
44-
filtered = filtered.concat(lines.splice(index + 1).map((line) => line.join('')))
45-
break
4633
}
4734

48-
filtered.push(lines[index].join(''))
35+
filtered.push(line)
4936
}
5037

51-
return { content: filtered.join(''), value }
38+
return { content: filtered.join('\n'), value }
5239
}
5340

5441
export async function verify(content: string, prefix: string): Promise<VerifyResult> {
@@ -87,30 +74,32 @@ export async function sign(verifyResult: VerifyResult, options: Options): Promis
8774
console.log('Address:', account.address)
8875
}
8976

90-
let valid = account.address === verifyResult.address
91-
let signature: null | `0x${string}` = null
92-
let toSha256Content: null | string = null
93-
let sha256sum: null | `0x${string}` = null
94-
let signedContent: null | string = null
77+
let signature: null | `0x${string}`
78+
let toSha256Content: null | string
79+
let sha256sum: null | `0x${string}`
80+
let signedContent: null | string
9581

96-
if (!valid || !verifyResult.sha256Valid) {
97-
signature = await account.signMessage({ message: verifyResult.signFiltered.content })
82+
signature = await account.signMessage({ message: verifyResult.signFiltered.content })
9883

99-
if (verifyResult.signFiltered.value !== null) {
100-
toSha256Content = getFilteredContent(verifyResult.content, '@eip191signature', options.prefix, signature).content
101-
} else {
102-
toSha256Content = [`${options.prefix} @eip191signature ` + signature, verifyResult.content].join('\n')
103-
}
84+
if (verifyResult.signFiltered.value !== null) {
85+
toSha256Content = getFilteredContent(
86+
verifyResult.content,
87+
'@eip191signature',
88+
options.prefix,
89+
signature,
90+
).content
91+
} else {
92+
toSha256Content = [`${options.prefix} @eip191signature ` + signature, verifyResult.content].join('\n')
93+
}
10494

105-
sha256sum = sha256(
106-
new TextEncoder().encode(getFilteredContent(toSha256Content, '@sha256sum', options.prefix).content),
107-
)
95+
sha256sum = sha256(
96+
new TextEncoder().encode(getFilteredContent(toSha256Content, '@sha256sum', options.prefix).content),
97+
)
10898

109-
if (verifyResult.sha256Filtered.value !== null) {
110-
signedContent = getFilteredContent(toSha256Content, '@sha256sum', options.prefix, sha256sum).content
111-
} else {
112-
signedContent = [`${options.prefix} @sha256sum ` + sha256sum, toSha256Content].join('\n')
113-
}
99+
if (verifyResult.sha256Filtered.value !== null) {
100+
signedContent = getFilteredContent(toSha256Content, '@sha256sum', options.prefix, sha256sum).content
101+
} else {
102+
signedContent = [`${options.prefix} @sha256sum ` + sha256sum, toSha256Content].join('\n')
114103
}
115104

116105
return {
@@ -126,7 +115,7 @@ function printHelp() {
126115
console.log('Usage: code-signature-ts [OPTIONS] <FILE|->')
127116
console.log('OPTIONS:')
128117
console.log(' --verify, -v — only verify')
129-
console.log(' --write, -f — write file')
118+
console.log(' --write, -w — write file')
130119
console.log(' --silent, -s — silent')
131120
console.log(' --prefix, -p — commented line prefix')
132121
console.log(' --out, -o — output file')
@@ -159,42 +148,48 @@ export async function codeSignature(options: Options) {
159148

160149
const verifyResult = await verify(content, options.prefix)
161150
let signResult: null | SignResult = null
162-
let fail = false
151+
let fail = true
163152

164153
if (verifyResult.sha256Valid) {
165154
if (!options.silent) {
166155
console.log(`OK: ${verifyResult.address}`)
167156
}
168-
} else if (!options.verify) {
157+
fail = false
158+
}
159+
160+
if (!options.verify) {
169161
signResult = await sign(verifyResult, options)
170162

171-
if (signResult.signedContent === null) {
163+
if (signResult.signedContent !== null) {
164+
if (options.write && fail) {
165+
await write(options, signResult.signedContent)
166+
}
167+
168+
if (fail && !options.write && !options.silent) {
169+
console.log(`${options.prefix} @sha256sum ${signResult.sha256sum}`)
170+
console.log(`${options.prefix} @eip191signature ${signResult.signature}`)
171+
}
172+
} else {
172173
console.log('ERROR: no signed content')
173174
fail = true
174-
} else if (options.write) {
175-
await write(options, signResult.signedContent)
176-
} else if (!options.silent) {
177-
console.log(`${options.prefix} @sha256sum ${signResult.sha256sum}`)
178-
console.log(`${options.prefix} @eip191signature ${signResult.signature}`)
179175
}
180-
} else {
181-
if (!options.silent) {
182-
console.log('SHA256: ERROR')
183-
}
184-
fail = true
176+
}
177+
178+
if (fail && !options.silent) {
179+
console.log('SHA256: ERROR')
185180
}
186181

187182
return { verifyResult, signResult, fail }
188183
}
189184

190185
if (import.meta.main) {
191-
const {values, positionals} = parseArgs({
186+
const { values, positionals } = parseArgs({
192187
args: Bun.argv,
193188
options: {
194-
help: { type: 'boolean', short: 'h' },
195-
write: { type: 'boolean', short: 'w' },
196-
verify: { type: 'boolean', short: 'v' },
197-
silent: { type: 'boolean', short: 's' },
189+
help: { type: 'boolean', short: 'h', default: false },
190+
write: { type: 'boolean', short: 'w', default: false },
191+
verify: { type: 'boolean', short: 'v', default: false },
192+
silent: { type: 'boolean', short: 's', default: false },
198193
prefix: { type: 'string', short: 'p', default: '//' },
199194
out: { type: 'string', short: 'o' },
200195
},

src/types.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { Address } from 'viem'
22

33
export type CliOptions = {
4-
help?: boolean,
5-
write?: boolean,
6-
verify?: boolean,
7-
silent?: boolean,
8-
prefix: string,
9-
out?: string,
4+
help: boolean
5+
write: boolean
6+
force: boolean
7+
verify: boolean
8+
silent: boolean
9+
prefix: string
10+
out?: string
1011
}
1112

1213
export type Options = CliOptions & {

0 commit comments

Comments
 (0)