Skip to content

Commit

Permalink
fix: compressed key does not contain private key
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Dec 5, 2019
1 parent 979b201 commit 2cdace9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 11 deletions.
Expand Up @@ -8,6 +8,6 @@ export async function getMyProveBio(whoAmI: PersonIdentifier): Promise<string |
const myIdentity = await queryMyIdentityAtDB(whoAmI)
if (!myIdentity) return null
const pub = await crypto.subtle.exportKey('jwk', myIdentity.publicKey)
const compressed = compressSecp256k1Key(pub)
const compressed = compressSecp256k1Key(pub, 'public')
return getNetworkWorker(whoAmI.network).publicKeyEncoder(compressed)
}
Expand Up @@ -8,7 +8,7 @@ import { import_ECDH_256k1_Key } from '../../../utils/crypto.subtle'
export async function verifyOthersProve(bio: string, others: PersonIdentifier): Promise<boolean> {
const compressedX = getNetworkWorker(others.network).publicKeyDecoder(bio)
if (!compressedX) return false
const key = decompressSecp256k1Key(compressedX)
const key = decompressSecp256k1Key(compressedX, 'public')
let publicKey: CryptoKey
try {
publicKey = await import_ECDH_256k1_Key(key)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/mnemonic-code/index.ts
Expand Up @@ -41,7 +41,7 @@ export async function recover_ECDH_256k1_KeyPair_ByMnemonicWord(mnemonicWord: st
}

function HDKeyToJwk(hdk: wallet.HDKey): JsonWebKey {
const jwk = decompressSecp256k1Key(encodeArrayBuffer(hdk.publicKey))
const jwk = decompressSecp256k1Key(encodeArrayBuffer(hdk.publicKey), 'public')
jwk.d = hdk.privateKey ? Convert.ToBase64Url(hdk.privateKey) : undefined
return jwk
}
8 changes: 4 additions & 4 deletions src/utils/type-transform/BackupFileShortRepresentation.ts
Expand Up @@ -25,8 +25,8 @@ export function compressBackupFile(file: BackupJSONFileLatest): string {
userId,
nickname,
localKey.k,
compressSecp256k1Key(publicKey),
compressSecp256k1Key(privateKey),
compressSecp256k1Key(publicKey, 'public'),
compressSecp256k1Key(privateKey, 'private'),
grantedHostPermissions.join(';'),
].join('🤔')
}
Expand Down Expand Up @@ -60,8 +60,8 @@ export function decompressBackupFile(short: string): BackupJSONFileLatest {
},
network,
nickname,
privateKey: decompressSecp256k1Key(privateKey),
publicKey: decompressSecp256k1Key(publicKey),
privateKey: decompressSecp256k1Key(privateKey, 'private'),
publicKey: decompressSecp256k1Key(publicKey, 'public'),
userId,
},
],
Expand Down
12 changes: 8 additions & 4 deletions src/utils/type-transform/SECP256k1-Compression.ts
Expand Up @@ -27,12 +27,15 @@ function decompressSecp256k1Point(point: ArrayBuffer): { x: string; y: string }
return { x: Convert.ToBase64Url(x), y: Convert.ToBase64Url(y) }
}

export function compressSecp256k1Key(key: JsonWebKey): string {
export function compressSecp256k1Key(key: JsonWebKey, type: 'public' | 'private'): string {
if (type === 'private' && !key.d) throw new Error('Private key does not contain secret')
const arr = compressSecp256k1Point(key.x!, key.y!)
return encodeArrayBuffer(arr)
return encodeArrayBuffer(arr) + (type === 'private' ? '🙈' + key.d! : '')
}
export function decompressSecp256k1Key(compressed: string): JsonWebKey {
const arr = decodeArrayBuffer(compressed)
export function decompressSecp256k1Key(compressed: string, type: 'public' | 'private'): JsonWebKey {
const [compressedPublic, privateKey] = compressed.split('🙈')
if (type === 'private' && privateKey.length < 1) throw new Error('Private key does not contain secret')
const arr = decodeArrayBuffer(compressedPublic)
const key = decompressSecp256k1Point(arr)
return {
crv: 'K-256',
Expand All @@ -41,5 +44,6 @@ export function decompressSecp256k1Key(compressed: string): JsonWebKey {
y: key.y,
key_ops: ['deriveKey'],
kty: 'EC',
d: type === 'private' ? privateKey : undefined,
}
}

0 comments on commit 2cdace9

Please sign in to comment.