diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..bb6215b --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,37 @@ +name: Build package + +on: + push: + branches: [ main ] + paths-ignore: + - '*.md' # ignore changes to readmes + - '*.js' # ignore js files + pull_request: + branches: [ main ] + +permissions: + contents: read + pages: write + id-token: write + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: oven-sh/setup-bun@v2 + + - name: Install dependencies + run: bun i + + - name: Build package + run: bun run build + + - name: Commit JS files + run: | + set +e + git add dist/ + git config user.name "$(git --no-pager log --format=format:'%an' -n 1)" + git config user.email "$(git --no-pager log --format=format:'%ae' -n 1)" + git commit -m "build: build package" + git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY" \ No newline at end of file diff --git a/.gitignore b/.gitignore index f8900d8..b274f59 100644 --- a/.gitignore +++ b/.gitignore @@ -174,4 +174,6 @@ dist # Finder (MacOS) folder config .DS_Store -docs \ No newline at end of file +docs + +dist/ \ No newline at end of file diff --git a/package.json b/package.json index cf46b90..d903787 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@li0ard/ecdsa_icao", "main": "./dist/index.js", "types": "./dist/index.d.ts", - "version": "0.1.3", + "version": "0.1.4", "type": "module", "repository": { "type": "git", diff --git a/src/index.ts b/src/index.ts index 68709a1..f32df87 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,38 +1,17 @@ import { createCurve } from "@noble/curves/_shortw_utils"; import { Field } from "@noble/curves/abstract/modular"; +import { type CurveFn } from "@noble/curves/abstract/weierstrass"; import { sha1 } from "@noble/hashes/sha1"; import { sha224, sha256 } from "@noble/hashes/sha256"; import { sha384, sha512 } from "@noble/hashes/sha512"; -import { ECParameters } from "@peculiar/asn1-ecc"; +import { type ECParameters } from "@peculiar/asn1-ecc"; import TLV from "node-tlv"; -import { p256 } from "@noble/curves/p256" -import { p384 } from "@noble/curves/p384" -import { p521 } from "@noble/curves/p521" -import { secp256k1 } from "@noble/curves/secp256k1" -import { ed25519 } from "@noble/curves/ed25519" -import { ed448 } from "@noble/curves/ed448" - -/** - * Identify curve by `p` field - * @param params Public key parameters - */ -export const identifyCurveByP = (params: ECParameters) => { - let curves = [p256, p384, p521, secp256k1, ed25519, ed448] - let curvesObj: {[key: string]: any} = {} - - for(let i of curves) { - curvesObj[i.CURVE.p.toString()] = i - } - return curvesObj[BigInt(`0x${TLV.parse(Buffer.from(params.specifiedCurve?.fieldID.parameters as ArrayBuffer)).value}`).toString()] -} - - /** * Convert buffer to BigInt * @param data Input buffer */ -const bufToBigInt = (data: Buffer) => { +const bufToBigInt = (data: Buffer): bigint => { return BigInt(`0x${data.toString("hex")}`) } @@ -41,12 +20,12 @@ const bufToBigInt = (data: Buffer) => { * @param params Public key parameters * @param lowS Low order */ -export const curveFromECParams = (params: ECParameters, lowS: boolean = false) => { +export const curveFromECParams = (params: ECParameters, lowS: boolean = false): CurveFn => { if(!params.specifiedCurve) throw new Error("Only explicit ECC parameters supported"); if(params.specifiedCurve.fieldID.fieldType != "1.2.840.10045.1.1") throw new Error("Only explicit [X9.62] schema supported"); let base = Buffer.from(params.specifiedCurve.base.buffer).subarray(1) - + return createCurve({ a: bufToBigInt(Buffer.from(params.specifiedCurve.curve.a)), b: bufToBigInt(Buffer.from(params.specifiedCurve.curve.b)), @@ -74,4 +53,3 @@ export const hashFromECDSAOID = (oid: string): typeof sha1 | typeof sha256 | typ } return algorithms[oid] } -