Skip to content

Commit f3585b8

Browse files
committed
Fixed the function considering the new library
Old ASN library is replaced by the new one because it has high no. of downloads/week although the last commit is 2 years old.
1 parent d76a8cf commit f3585b8

File tree

4 files changed

+27
-89
lines changed

4 files changed

+27
-89
lines changed

packages/auto-id/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
"@autonomys/auto-utils": "workspace:*",
1313
"@peculiar/asn1-schema": "^2.3.8",
1414
"@peculiar/asn1-x509": "^2.3.8",
15-
"asn1-ts": "^8.0.2"
15+
"asn1js": "^3.0.5"
1616
},
1717
"files": [
1818
"dist",
1919
"README.md"
2020
],
2121
"devDependencies": {
22-
"@types/node": "^20.12.12",
2322
"@types/jest": "^29.5.12",
23+
"@types/node": "^20.12.12",
2424
"jest": "^29.7.0",
2525
"ts-jest": "^29.1.4",
2626
"ts-node": "^10.9.2",
2727
"typescript": "^5.4.5"
2828
}
29-
}
29+
}

packages/auto-id/src/utils.ts

Lines changed: 14 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,23 @@
1-
import {
2-
ASN1Construction,
3-
ASN1TagClass,
4-
ASN1UniversalType,
5-
DERElement,
6-
ObjectIdentifier,
7-
} from 'asn1-ts'
8-
9-
/**
10-
* Represents an ASN.1 AlgorithmIdentifier structure commonly used in cryptographic protocols.
11-
* This class handles the construction and DER encoding of an algorithm identifier, which typically
12-
* consists of an algorithm OID and optional parameters.
13-
*/
14-
class AlgorithmIdentifier {
15-
public algorithm: ObjectIdentifier
16-
public parameters: null
17-
18-
/**
19-
* Creates an instance of AlgorithmIdentifier.
20-
*
21-
* @param algorithm The ObjectIdentifier of the algorithm.
22-
* @param parameters The parameters of the algorithm, generally null in many cryptographic uses.
23-
*/
24-
constructor(algorithm: ObjectIdentifier, parameters: null = null) {
25-
this.algorithm = algorithm
26-
this.parameters = parameters
27-
}
28-
29-
/**
30-
* Encodes this AlgorithmIdentifier into its DER (Distinguished Encoding Rules) format.
31-
*
32-
* @returns Uint8Array containing the DER encoded bytes of the AlgorithmIdentifier.
33-
*/
34-
public toDER(): Uint8Array {
35-
const sequenceElement = new DERElement(
36-
ASN1TagClass.universal,
37-
ASN1Construction.constructed,
38-
ASN1UniversalType.sequence,
39-
)
40-
41-
const oidElement = new DERElement(
42-
ASN1TagClass.universal,
43-
ASN1Construction.primitive,
44-
ASN1UniversalType.objectIdentifier,
45-
)
46-
oidElement.objectIdentifier = this.algorithm
47-
48-
const nullElement = new DERElement(
49-
ASN1TagClass.universal,
50-
ASN1Construction.primitive,
51-
ASN1UniversalType.nill,
52-
)
53-
54-
sequenceElement.sequence = [oidElement, nullElement]
55-
56-
return sequenceElement.toBytes()
57-
}
58-
}
1+
import { ObjectIdentifier } from 'asn1js'
592

603
/**
614
* Encodes a given string representation of an OID into its DER format.
625
* This function is specifically used to encode signature algorithm OIDs.
636
*
647
* @param oid The string representation of the ObjectIdentifier to be encoded.
65-
* @returns Uint8Array containing the DER encoded OID.
66-
* @example
67-
* ```ts
68-
* const oid = '1.2.840.113549.1.1.11' // Example OID for SHA-256 with RSA Encryption
69-
* const derEncodedOID = derEncodeSignatureAlgorithmOID(oid)
70-
* console.log(new Uint8Array(derEncodedOID)) // Logs the DER encoded bytes
71-
* ```
8+
* @returns Uint8Array containing the DER encoded OID along with NULL params of X.509 signature algorithm.
729
*/
7310
export function derEncodeSignatureAlgorithmOID(oid: string): Uint8Array {
74-
const numbers = oid.split('.').map((n) => parseInt(n, 10)) // Convert the string parts to numbers
75-
const algorithmIdentifier = new AlgorithmIdentifier(new ObjectIdentifier(numbers))
76-
return algorithmIdentifier.toDER()
11+
const objectIdentifier = new ObjectIdentifier({ value: oid })
12+
const berArrayBuffer = objectIdentifier.toBER(false)
13+
14+
// Typically, in X.509, the algorithm identifier is followed by parameters; for many algorithms, this is just NULL.
15+
const nullParameter = [0x05, 0x00] // DER encoding for NULL
16+
17+
// Calculate the total length including OID and NULL parameter
18+
const totalLength = berArrayBuffer.byteLength + nullParameter.length
19+
20+
const sequenceHeader = [0x30, totalLength] // 0x30 is the DER tag for SEQUENCE
21+
22+
return new Uint8Array([...sequenceHeader, ...new Uint8Array(berArrayBuffer), ...nullParameter])
7723
}

packages/auto-id/tests/utils.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AsnParser } from '@peculiar/asn1-schema' // A library to parse ASN.1
2+
// TODO: See why X509Certificate (from crypto) is not compatible argument.
23
import { Certificate } from '@peculiar/asn1-x509' // Assuming X.509 certificate handling
34
import fs from 'fs'
45
import { derEncodeSignatureAlgorithmOID } from '../src/utils'
@@ -16,14 +17,14 @@ describe('Verify crypto functions', () => {
1617
const signatureAlgorithmOID = cert.signatureAlgorithm.algorithm
1718

1819
// DER encode the OID
19-
const derEncodedOID = derEncodeSignatureAlgorithmOID(signatureAlgorithmOID.toString())
20+
const derEncodedOID = derEncodeSignatureAlgorithmOID(signatureAlgorithmOID)
21+
// <Buffer 30 0d 06 09 2a 86 48 86 f7 0d 01 01 0b 05 00>
22+
// console.log(Buffer.from(derEncodedOID))
2023

21-
// Expected DER encoded OID from a known good implementation (example hex string)
22-
const fromRustImplementation = new Uint8Array(
23-
Buffer.from('300d06092a864886f70d01010b0500', 'hex'),
24-
)
24+
// Convert derEncodedOID to hex string for comparison
25+
const derEncodedOIDHex = Buffer.from(derEncodedOID).toString('hex')
2526

26-
// Compare the DER encoded OID with the expected result
27-
expect(new Uint8Array(derEncodedOID)).toEqual(fromRustImplementation)
27+
// Expected DER encoded OID from the result of tests in https://github.com/subspace/subspace/blob/d875a5aac35c1732eec61ce4359782eff58ff6fc/domains/pallets/auto-id/src/tests.rs#L127
28+
expect(derEncodedOIDHex).toEqual('300d06092a864886f70d01010b0500')
2829
})
2930
})

yarn.lock

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ __metadata:
3636
"@peculiar/asn1-x509": "npm:^2.3.8"
3737
"@types/jest": "npm:^29.5.12"
3838
"@types/node": "npm:^20.12.12"
39-
asn1-ts: "npm:^8.0.2"
39+
asn1js: "npm:^3.0.5"
4040
jest: "npm:^29.7.0"
4141
ts-jest: "npm:^29.1.4"
4242
ts-node: "npm:^10.9.2"
@@ -1825,15 +1825,6 @@ __metadata:
18251825
languageName: node
18261826
linkType: hard
18271827

1828-
"asn1-ts@npm:^8.0.2":
1829-
version: 8.0.2
1830-
resolution: "asn1-ts@npm:8.0.2"
1831-
dependencies:
1832-
tslib: "npm:^2.4.1"
1833-
checksum: 10c0/37ca66c319730ae7a2762642a3bb867b298a1ad721a11f859f900332b39117497fd337ba16e7d4c4f7dc9bd6f5e577d8d49c07edcc7fdebeff1d42d7a66a3112
1834-
languageName: node
1835-
linkType: hard
1836-
18371828
"asn1js@npm:^3.0.5":
18381829
version: 3.0.5
18391830
resolution: "asn1js@npm:3.0.5"
@@ -4866,7 +4857,7 @@ __metadata:
48664857
languageName: node
48674858
linkType: hard
48684859

4869-
"tslib@npm:^2.4.0, tslib@npm:^2.4.1, tslib@npm:^2.6.1":
4860+
"tslib@npm:^2.4.0, tslib@npm:^2.6.1":
48704861
version: 2.6.3
48714862
resolution: "tslib@npm:2.6.3"
48724863
checksum: 10c0/2598aef53d9dbe711af75522464b2104724d6467b26a60f2bdac8297d2b5f1f6b86a71f61717384aa8fd897240467aaa7bcc36a0700a0faf751293d1331db39a

0 commit comments

Comments
 (0)