-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2011517
commit 4f468dc
Showing
12 changed files
with
264 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
node_modules | ||
lib | ||
wbn-sign*.tgz | ||
*.wbn | ||
*.wbn | ||
*.swbn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,91 @@ | ||
import commander from 'commander'; | ||
import { Command, Option } from 'commander'; | ||
import { | ||
NodeCryptoSigningStrategy, | ||
IntegrityBlockSigner, | ||
WebBundleId, | ||
} from './wbn-sign.js'; | ||
import * as fs from 'fs'; | ||
import { greenConsoleLog, parseMaybeEncryptedKey } from './utils/cli-utils.js'; | ||
import { | ||
greenConsoleLog, | ||
parseMaybeEncryptedKeyFromFile, | ||
} from './utils/cli-utils.js'; | ||
import { KeyObject } from 'crypto'; | ||
|
||
const program = new commander.Command() | ||
const program = new Command() | ||
.name('wbn-sign') | ||
.description( | ||
'A simple CLI tool to sign the given web bundle with the given private key.' | ||
); | ||
|
||
function readOptions() { | ||
return program | ||
.addOption( | ||
new Option('--version <version>').choices(['v1', 'v2']).default('v1') | ||
) | ||
.requiredOption( | ||
'-i, --input <file>', | ||
'input web bundle to be signed (required)' | ||
) | ||
.requiredOption( | ||
'-k, --privateKey <file>', | ||
'path to ed25519 private key (required)' | ||
'-k, --private-key <file...>', | ||
'paths to Ed25519 / ECDSA P-256 private key(s) (required)' | ||
) | ||
.option( | ||
'-o, --output <file>', | ||
'signed web bundle output file', | ||
/*defaultValue=*/ 'signed.swbn' | ||
) | ||
.parse(process.argv); | ||
.option('--web-bundle-id <web-bundle-id>', 'web bundle ID (only for v2)') | ||
.action((options) => { | ||
switch (options.version) { | ||
case 'v1': | ||
{ | ||
if (options.privateKey.length > 1) { | ||
throw new Error( | ||
`It's not allowed to specify more than one private key for v1 signing.` | ||
); | ||
} | ||
if (options.webBundleId) { | ||
throw new Error( | ||
`It's not allowed to specify --web-bundle-id for v1 signing.` | ||
); | ||
} | ||
} | ||
break; | ||
case 'v2': | ||
{ | ||
if (options.privateKey.length > 1 && !options.webBundleId) { | ||
throw new Error( | ||
`--web-bundle-id must be specified if there's more than 1 signing key involved.` | ||
); | ||
} | ||
} | ||
break; | ||
} | ||
}) | ||
.parse(process.argv) | ||
.opts(); | ||
} | ||
|
||
export async function main() { | ||
const options = readOptions(); | ||
const webBundle = fs.readFileSync(options.input); | ||
const parsedPrivateKey: KeyObject = await parseMaybeEncryptedKey( | ||
fs.readFileSync(options.privateKey) | ||
); | ||
|
||
const privateKeys = new Array<KeyObject>(); | ||
for (const privateKey of options.privateKey) { | ||
privateKeys.push(await parseMaybeEncryptedKeyFromFile(privateKey)); | ||
} | ||
|
||
const webBundleId = options.webBundleId | ||
? options.webBundleId | ||
: new WebBundleId(privateKeys[0]).serialize(); | ||
const signer = new IntegrityBlockSigner( | ||
/*is_v2=*/ options.version === 'v2', | ||
webBundle, | ||
new NodeCryptoSigningStrategy(parsedPrivateKey) | ||
webBundleId, | ||
privateKeys.map((privateKey) => new NodeCryptoSigningStrategy(privateKey)) | ||
); | ||
const { signedWebBundle } = await signer.sign(); | ||
greenConsoleLog(`${new WebBundleId(parsedPrivateKey)}`); | ||
greenConsoleLog(`${webBundleId}`); | ||
fs.writeFileSync(options.output, signedWebBundle); | ||
} |
Oops, something went wrong.