Skip to content

Commit 636bd7c

Browse files
Exulansismnzaki
authored andcommitted
feat(encoding utils): added utils to work with hex. JWT signatures are now base64 encoded
We had an inconsistency, where JWT signatures were encoded as hex. This commit fixes that, ensuring all future signatures will be correctly encoded. This commit also adds a few helper methods (stripHexPrefix, addHexPrefix, parseHexOrBase64) which make it easier and safer to work with hex strings.
1 parent 46c0898 commit 636bd7c

File tree

6 files changed

+34
-10
lines changed

6 files changed

+34
-10
lines changed

ts/didMethods/jolo/registrar.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
ServiceEndpointsSection,
66
PublicKeySection,
77
} from '../../identity/didDocument/sections'
8-
import { fuelKeyWithEther } from '../../utils/helper'
8+
import { fuelKeyWithEther, stripHexPrefix } from '../../utils/helper'
99
import { SignedCredential } from '../../credentials/signedCredential/signedCredential'
1010
import { IRegistrar } from '../types'
1111
import { claimsMetadata } from '@jolocom/protocol-ts'
@@ -18,6 +18,7 @@ import {
1818
import { validateDigestable } from '../../utils/validation'
1919
import { KEY_REFS } from './constants'
2020
import { publicKeyToJoloDID } from './utils'
21+
import { addHexPrefix } from 'ethereumjs-util'
2122

2223
const { SIGNING_KEY_REF, ANCHOR_KEY_REF, ENCRYPTION_KEY_REF } = KEY_REFS
2324

@@ -239,7 +240,7 @@ export class JolocomRegistrar implements IRegistrar {
239240
}
240241

241242
const unsignedTx = await this.registrarFns.publishDidDocument(
242-
Buffer.from(anchoringKey.publicKeyHex.slice(2), 'hex'),
243+
Buffer.from(stripHexPrefix(anchoringKey.publicKeyHex), 'hex'),
243244
//@ts-ignore
244245
didDocument.toJSON(),
245246
)
@@ -251,13 +252,13 @@ export class JolocomRegistrar implements IRegistrar {
251252
keyRef: anchoringKey.controller[0],
252253
encryptionPass: password,
253254
},
254-
Buffer.from(unsignedTx.slice(2), 'hex'),
255+
Buffer.from(stripHexPrefix(unsignedTx), 'hex'),
255256
)
256257

257258
return this.registrarFns
258259
.broadcastTransaction(unsignedTx, {
259-
r: '0x' + signature.slice(0, 32).toString('hex'),
260-
s: '0x' + signature.slice(32, 64).toString('hex'),
260+
r: addHexPrefix(signature.slice(0, 32).toString('hex')),
261+
s: addHexPrefix(signature.slice(32, 64).toString('hex')),
261262
recoveryParam: signature[64],
262263
})
263264
.catch(console.log)

ts/identityWallet/identityWallet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ export class IdentityWallet {
319319
await jwt.asBytes(),
320320
) // TODO Also, are the signatures hex or b64?
321321

322-
jwt.signature = signature.toString('hex')
322+
jwt.signature = signature.toString('base64')
323323

324324
return jwt
325325
}

ts/linkedData/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ILinkedDataSignatureAttrs } from '../linkedDataSignature/types'
2-
import { keyIdToDid } from '../utils/helper'
2+
import { keyIdToDid, parseHexOrBase64 } from '../utils/helper'
33
import { sha256 } from '../utils/crypto'
44
import { canonize } from 'jsonld'
55
import { JsonLdObject, SignedJsonLdObject, JsonLdContext } from './types'
@@ -81,7 +81,7 @@ export const validateJsonLd = async (
8181

8282
return verifySignatureWithIdentity(
8383
await normalizeSignedLdObject(json, json['@context']),
84-
Buffer.from(json.proof.signatureValue, 'hex'),
84+
parseHexOrBase64(json.proof.signatureValue),
8585
json.proof.creator,
8686
issuerIdentity,
8787
)

ts/parse/parseAndValidate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { verifySignatureWithIdentity } from '../utils/validation'
99
import { ISignedCredentialAttrs } from '@jolocom/protocol-ts/dist/lib/signedCredential'
1010
import { SignedCredential } from '../credentials/signedCredential/signedCredential'
1111
import { parse } from './parse'
12+
import { parseHexOrBase64 } from '../utils/helper'
1213

1314
const parseAndValidateDidDoc = async (
1415
didDocument: IDidDocumentAttrs,
@@ -53,7 +54,7 @@ export const parseAndValidateInteractionToken = async (
5354

5455
const isValid = await verifySignatureWithIdentity(
5556
Buffer.from(Buffer.from([body, payload].join('.'))),
56-
Buffer.from(signature, 'hex'),
57+
parseHexOrBase64(signature),
5758
interactionToken.signer.keyId,
5859
signer,
5960
)

ts/utils/helper.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@ import { DidDocument } from '../identity/didDocument/didDocument'
44
import { KeyTypes, PublicKeyInfo } from '@jolocom/vaulted-key-provider'
55
import { IKeyMetadata } from '../identityWallet/types'
66
import { ErrorCodes } from '../errors'
7+
import { isHexString } from 'ethers/lib/utils'
8+
9+
/**
10+
* Helper which will strip the 0x prefix from a hex string
11+
* If no hex prefix is present, the unmodified string is returned
12+
*/
13+
14+
export const stripHexPrefix = (hexPrefixedString: string) => {
15+
return addHexPrefix(hexPrefixedString).slice(2)
16+
}
17+
18+
/**
19+
* Helper which will attempt to parse a string as hex first, and then,
20+
* in case of failure, as base64. Returns the decoded buffer
21+
*/
22+
23+
export const parseHexOrBase64 = (hexOrB64: string) => {
24+
return isHexString(addHexPrefix(hexOrB64))
25+
? Buffer.from(stripHexPrefix(hexOrB64), 'hex')
26+
: Buffer.from(hexOrB64, 'base64')
27+
}
728

829
/**
930
* Helper function to convert a key identifier to the owner did

ts/utils/validation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { KeyTypes, getCryptoProvider } from '@jolocom/vaulted-key-provider'
44
import { cryptoUtils } from '@jolocom/native-core'
55
import { Identity } from '../identity/identity'
66
import { IResolver } from '../didMethods/types'
7+
import { parseHexOrBase64 } from './helper'
78

89
export type IdentityOrResolver = Identity | IResolver
910

@@ -76,7 +77,7 @@ export const validateDigestable = async (
7677

7778
return verifySignatureWithIdentity(
7879
await toValidate.asBytes(),
79-
Buffer.from(toValidate.signature, 'hex'),
80+
parseHexOrBase64(toValidate.signature),
8081
toValidate.signer.keyId,
8182
issuerIdentity,
8283
)

0 commit comments

Comments
 (0)