diff --git a/src/Client.ts b/src/Client.ts index 22854fda3..047d8e2d8 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -25,7 +25,6 @@ import { import { decompress, compress } from './Compression' import { Compression } from './proto/messaging' import * as proto from './proto/messaging' -import ContactBundle from './ContactBundle' /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ /* eslint-disable @typescript-eslint/no-explicit-any */ @@ -196,12 +195,10 @@ export default class Client { callback: (msgs: WakuMessage[]) => { for (const msg of msgs) { if (!msg.payload) continue - const bundle = ContactBundle.fromBytes(msg.payload as Uint8Array) - const keyBundle = bundle.keyBundle - - const address = keyBundle?.walletSignatureAddress() + const bundle = PublicKeyBundle.fromBytes(msg.payload as Uint8Array) + const address = bundle.walletSignatureAddress() if (address === peerAddress) { - recipientKey = keyBundle + recipientKey = bundle break } } diff --git a/src/ContactBundle.ts b/src/ContactBundle.ts deleted file mode 100644 index a6c06efee..000000000 --- a/src/ContactBundle.ts +++ /dev/null @@ -1,39 +0,0 @@ -import * as proto from '../src/proto/messaging' -import { PublicKeyBundle } from './crypto' -import PublicKey from './crypto/PublicKey' - -// ContactBundle packages all the infromation which a client uses to advertise on the network. -export default class ContactBundle implements proto.ContactBundleV1 { - keyBundle: PublicKeyBundle - - constructor(publicKeyBundle: PublicKeyBundle) { - if (!publicKeyBundle) { - throw new Error('missing keyBundle') - } - this.keyBundle = publicKeyBundle - } - - toBytes(): Uint8Array { - return proto.ContactBundle.encode({ - v1: { - keyBundle: this.keyBundle, - }, - }).finish() - } - - static fromBytes(bytes: Uint8Array): ContactBundle { - const decoded = proto.ContactBundle.decode(bytes) - if (!decoded.v1?.keyBundle?.identityKey) { - throw new Error('missing keyBundle') - } - if (!decoded.v1?.keyBundle?.preKey) { - throw new Error('missing pre-key') - } - return new ContactBundle( - new PublicKeyBundle( - new PublicKey(decoded.v1?.keyBundle?.identityKey), - new PublicKey(decoded.v1?.keyBundle?.preKey) - ) - ) - } -} diff --git a/src/crypto/PrivateKeyBundle.ts b/src/crypto/PrivateKeyBundle.ts index 5c1921b61..b93c3eedd 100644 --- a/src/crypto/PrivateKeyBundle.ts +++ b/src/crypto/PrivateKeyBundle.ts @@ -11,7 +11,7 @@ import { NoMatchingPreKeyError } from './errors' // PrivateKeyBundle bundles the private keys corresponding to a PublicKeyBundle for convenience. // This bundle must not be shared with anyone, although will have to be persisted // somehow so that older messages can be decrypted again. -export default class PrivateKeyBundle implements proto.PrivateKeyBundleV1 { +export default class PrivateKeyBundle implements proto.PrivateKeyBundle { identityKey: PrivateKey preKeys: PrivateKey[] @@ -121,10 +121,8 @@ export default class PrivateKeyBundle implements proto.PrivateKeyBundleV1 { throw new Error('missing identity key') } const bytes = proto.PrivateKeyBundle.encode({ - v1: { - identityKey: this.identityKey, - preKeys: this.preKeys, - }, + identityKey: this.identityKey, + preKeys: this.preKeys, }).finish() const wPreKey = getRandomValues(new Uint8Array(32)) const secret = hexToBytes( @@ -132,10 +130,8 @@ export default class PrivateKeyBundle implements proto.PrivateKeyBundleV1 { ) const ciphertext = await encrypt(bytes, secret) return proto.EncryptedPrivateKeyBundle.encode({ - v1: { - walletPreKey: wPreKey, - ciphertext, - }, + walletPreKey: wPreKey, + ciphertext, }).finish() } @@ -145,29 +141,29 @@ export default class PrivateKeyBundle implements proto.PrivateKeyBundleV1 { bytes: Uint8Array ): Promise { const encrypted = proto.EncryptedPrivateKeyBundle.decode(bytes) - if (!encrypted.v1?.walletPreKey) { + if (!encrypted.walletPreKey) { throw new Error('missing wallet pre-key') } const secret = hexToBytes( await wallet.signMessage( - PrivateKeyBundle.storageSigRequestText(encrypted.v1.walletPreKey) + PrivateKeyBundle.storageSigRequestText(encrypted.walletPreKey) ) ) - if (!encrypted.v1?.ciphertext?.aes256GcmHkdfSha256) { + if (!encrypted.ciphertext?.aes256GcmHkdfSha256) { throw new Error('missing bundle ciphertext') } - const ciphertext = new Ciphertext(encrypted.v1.ciphertext) + const ciphertext = new Ciphertext(encrypted.ciphertext) const decrypted = await decrypt(ciphertext, secret) const bundle = proto.PrivateKeyBundle.decode(decrypted) - if (!bundle.v1?.identityKey) { + if (!bundle.identityKey) { throw new Error('missing identity key') } - if (bundle.v1?.preKeys.length === 0) { + if (bundle.preKeys.length === 0) { throw new Error('missing pre-keys') } return new PrivateKeyBundle( - new PrivateKey(bundle.v1.identityKey), - bundle.v1.preKeys.map((protoKey) => new PrivateKey(protoKey)) + new PrivateKey(bundle.identityKey), + bundle.preKeys.map((protoKey) => new PrivateKey(protoKey)) ) } } diff --git a/src/proto/messaging.proto b/src/proto/messaging.proto index 900eba18d..74d70d56f 100644 --- a/src/proto/messaging.proto +++ b/src/proto/messaging.proto @@ -32,16 +32,6 @@ message PublicKeyBundle { PublicKey preKey = 2; } -message ContactBundleV1 { - PublicKeyBundle keyBundle = 1; -} - -message ContactBundle { - oneof version { - ContactBundleV1 v1 = 1; - } -} - // ContentTypeId is used to identify the type of content stored in a Message. message ContentTypeId { string authorityId = 1; // authority governing this content type diff --git a/src/proto/messaging.ts b/src/proto/messaging.ts index d00b406f7..94bf1b625 100644 --- a/src/proto/messaging.ts +++ b/src/proto/messaging.ts @@ -76,14 +76,6 @@ export interface PublicKeyBundle { preKey: PublicKey | undefined } -export interface ContactBundleV1 { - keyBundle: PublicKeyBundle | undefined -} - -export interface ContactBundle { - v1: ContactBundleV1 | undefined -} - /** ContentTypeId is used to identify the type of content stored in a Message. */ export interface ContentTypeId { /** authority governing this content type */ @@ -539,129 +531,6 @@ export const PublicKeyBundle = { }, } -function createBaseContactBundleV1(): ContactBundleV1 { - return { keyBundle: undefined } -} - -export const ContactBundleV1 = { - encode( - message: ContactBundleV1, - writer: _m0.Writer = _m0.Writer.create() - ): _m0.Writer { - if (message.keyBundle !== undefined) { - PublicKeyBundle.encode( - message.keyBundle, - writer.uint32(10).fork() - ).ldelim() - } - return writer - }, - - decode(input: _m0.Reader | Uint8Array, length?: number): ContactBundleV1 { - const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input) - let end = length === undefined ? reader.len : reader.pos + length - const message = createBaseContactBundleV1() - while (reader.pos < end) { - const tag = reader.uint32() - switch (tag >>> 3) { - case 1: - message.keyBundle = PublicKeyBundle.decode(reader, reader.uint32()) - break - default: - reader.skipType(tag & 7) - break - } - } - return message - }, - - fromJSON(object: any): ContactBundleV1 { - return { - keyBundle: isSet(object.keyBundle) - ? PublicKeyBundle.fromJSON(object.keyBundle) - : undefined, - } - }, - - toJSON(message: ContactBundleV1): unknown { - const obj: any = {} - message.keyBundle !== undefined && - (obj.keyBundle = message.keyBundle - ? PublicKeyBundle.toJSON(message.keyBundle) - : undefined) - return obj - }, - - fromPartial, I>>( - object: I - ): ContactBundleV1 { - const message = createBaseContactBundleV1() - message.keyBundle = - object.keyBundle !== undefined && object.keyBundle !== null - ? PublicKeyBundle.fromPartial(object.keyBundle) - : undefined - return message - }, -} - -function createBaseContactBundle(): ContactBundle { - return { v1: undefined } -} - -export const ContactBundle = { - encode( - message: ContactBundle, - writer: _m0.Writer = _m0.Writer.create() - ): _m0.Writer { - if (message.v1 !== undefined) { - ContactBundleV1.encode(message.v1, writer.uint32(10).fork()).ldelim() - } - return writer - }, - - decode(input: _m0.Reader | Uint8Array, length?: number): ContactBundle { - const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input) - let end = length === undefined ? reader.len : reader.pos + length - const message = createBaseContactBundle() - while (reader.pos < end) { - const tag = reader.uint32() - switch (tag >>> 3) { - case 1: - message.v1 = ContactBundleV1.decode(reader, reader.uint32()) - break - default: - reader.skipType(tag & 7) - break - } - } - return message - }, - - fromJSON(object: any): ContactBundle { - return { - v1: isSet(object.v1) ? ContactBundleV1.fromJSON(object.v1) : undefined, - } - }, - - toJSON(message: ContactBundle): unknown { - const obj: any = {} - message.v1 !== undefined && - (obj.v1 = message.v1 ? ContactBundleV1.toJSON(message.v1) : undefined) - return obj - }, - - fromPartial, I>>( - object: I - ): ContactBundle { - const message = createBaseContactBundle() - message.v1 = - object.v1 !== undefined && object.v1 !== null - ? ContactBundleV1.fromPartial(object.v1) - : undefined - return message - }, -} - function createBaseContentTypeId(): ContentTypeId { return { authorityId: '', typeId: '', versionMajor: 0, versionMinor: 0 } } diff --git a/src/proto/private_key.proto b/src/proto/private_key.proto index ab709f82a..4b3e21010 100644 --- a/src/proto/private_key.proto +++ b/src/proto/private_key.proto @@ -18,24 +18,12 @@ message PrivateKey { PublicKey publicKey = 3; } -message PrivateKeyBundleV1 { +message PrivateKeyBundle { PrivateKey identityKey = 1; repeated PrivateKey preKeys = 2; } -message PrivateKeyBundle { - oneof version { - PrivateKeyBundleV1 v1 = 1; - } -} - -message EncryptedPrivateKeyBundleV1 { +message EncryptedPrivateKeyBundle { bytes walletPreKey = 1; Ciphertext ciphertext = 2; } - -message EncryptedPrivateKeyBundle { - oneof version { - EncryptedPrivateKeyBundleV1 v1 = 1; - } -} diff --git a/src/proto/private_key.ts b/src/proto/private_key.ts index b0e767d11..b801a2eec 100644 --- a/src/proto/private_key.ts +++ b/src/proto/private_key.ts @@ -16,24 +16,16 @@ export interface PrivateKey_Secp256k1 { bytes: Uint8Array } -export interface PrivateKeyBundleV1 { +export interface PrivateKeyBundle { identityKey: PrivateKey | undefined preKeys: PrivateKey[] } -export interface PrivateKeyBundle { - v1: PrivateKeyBundleV1 | undefined -} - -export interface EncryptedPrivateKeyBundleV1 { +export interface EncryptedPrivateKeyBundle { walletPreKey: Uint8Array ciphertext: Ciphertext | undefined } -export interface EncryptedPrivateKeyBundle { - v1: EncryptedPrivateKeyBundleV1 | undefined -} - function createBasePrivateKey(): PrivateKey { return { timestamp: 0, secp256k1: undefined, publicKey: undefined } } @@ -191,13 +183,13 @@ export const PrivateKey_Secp256k1 = { }, } -function createBasePrivateKeyBundleV1(): PrivateKeyBundleV1 { +function createBasePrivateKeyBundle(): PrivateKeyBundle { return { identityKey: undefined, preKeys: [] } } -export const PrivateKeyBundleV1 = { +export const PrivateKeyBundle = { encode( - message: PrivateKeyBundleV1, + message: PrivateKeyBundle, writer: _m0.Writer = _m0.Writer.create() ): _m0.Writer { if (message.identityKey !== undefined) { @@ -209,10 +201,10 @@ export const PrivateKeyBundleV1 = { return writer }, - decode(input: _m0.Reader | Uint8Array, length?: number): PrivateKeyBundleV1 { + decode(input: _m0.Reader | Uint8Array, length?: number): PrivateKeyBundle { const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input) let end = length === undefined ? reader.len : reader.pos + length - const message = createBasePrivateKeyBundleV1() + const message = createBasePrivateKeyBundle() while (reader.pos < end) { const tag = reader.uint32() switch (tag >>> 3) { @@ -230,7 +222,7 @@ export const PrivateKeyBundleV1 = { return message }, - fromJSON(object: any): PrivateKeyBundleV1 { + fromJSON(object: any): PrivateKeyBundle { return { identityKey: isSet(object.identityKey) ? PrivateKey.fromJSON(object.identityKey) @@ -241,7 +233,7 @@ export const PrivateKeyBundleV1 = { } }, - toJSON(message: PrivateKeyBundleV1): unknown { + toJSON(message: PrivateKeyBundle): unknown { const obj: any = {} message.identityKey !== undefined && (obj.identityKey = message.identityKey @@ -257,10 +249,10 @@ export const PrivateKeyBundleV1 = { return obj }, - fromPartial, I>>( + fromPartial, I>>( object: I - ): PrivateKeyBundleV1 { - const message = createBasePrivateKeyBundleV1() + ): PrivateKeyBundle { + const message = createBasePrivateKeyBundle() message.identityKey = object.identityKey !== undefined && object.identityKey !== null ? PrivateKey.fromPartial(object.identityKey) @@ -271,71 +263,13 @@ export const PrivateKeyBundleV1 = { }, } -function createBasePrivateKeyBundle(): PrivateKeyBundle { - return { v1: undefined } -} - -export const PrivateKeyBundle = { - encode( - message: PrivateKeyBundle, - writer: _m0.Writer = _m0.Writer.create() - ): _m0.Writer { - if (message.v1 !== undefined) { - PrivateKeyBundleV1.encode(message.v1, writer.uint32(10).fork()).ldelim() - } - return writer - }, - - decode(input: _m0.Reader | Uint8Array, length?: number): PrivateKeyBundle { - const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input) - let end = length === undefined ? reader.len : reader.pos + length - const message = createBasePrivateKeyBundle() - while (reader.pos < end) { - const tag = reader.uint32() - switch (tag >>> 3) { - case 1: - message.v1 = PrivateKeyBundleV1.decode(reader, reader.uint32()) - break - default: - reader.skipType(tag & 7) - break - } - } - return message - }, - - fromJSON(object: any): PrivateKeyBundle { - return { - v1: isSet(object.v1) ? PrivateKeyBundleV1.fromJSON(object.v1) : undefined, - } - }, - - toJSON(message: PrivateKeyBundle): unknown { - const obj: any = {} - message.v1 !== undefined && - (obj.v1 = message.v1 ? PrivateKeyBundleV1.toJSON(message.v1) : undefined) - return obj - }, - - fromPartial, I>>( - object: I - ): PrivateKeyBundle { - const message = createBasePrivateKeyBundle() - message.v1 = - object.v1 !== undefined && object.v1 !== null - ? PrivateKeyBundleV1.fromPartial(object.v1) - : undefined - return message - }, -} - -function createBaseEncryptedPrivateKeyBundleV1(): EncryptedPrivateKeyBundleV1 { +function createBaseEncryptedPrivateKeyBundle(): EncryptedPrivateKeyBundle { return { walletPreKey: new Uint8Array(), ciphertext: undefined } } -export const EncryptedPrivateKeyBundleV1 = { +export const EncryptedPrivateKeyBundle = { encode( - message: EncryptedPrivateKeyBundleV1, + message: EncryptedPrivateKeyBundle, writer: _m0.Writer = _m0.Writer.create() ): _m0.Writer { if (message.walletPreKey.length !== 0) { @@ -350,10 +284,10 @@ export const EncryptedPrivateKeyBundleV1 = { decode( input: _m0.Reader | Uint8Array, length?: number - ): EncryptedPrivateKeyBundleV1 { + ): EncryptedPrivateKeyBundle { const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input) let end = length === undefined ? reader.len : reader.pos + length - const message = createBaseEncryptedPrivateKeyBundleV1() + const message = createBaseEncryptedPrivateKeyBundle() while (reader.pos < end) { const tag = reader.uint32() switch (tag >>> 3) { @@ -371,7 +305,7 @@ export const EncryptedPrivateKeyBundleV1 = { return message }, - fromJSON(object: any): EncryptedPrivateKeyBundleV1 { + fromJSON(object: any): EncryptedPrivateKeyBundle { return { walletPreKey: isSet(object.walletPreKey) ? bytesFromBase64(object.walletPreKey) @@ -382,7 +316,7 @@ export const EncryptedPrivateKeyBundleV1 = { } }, - toJSON(message: EncryptedPrivateKeyBundleV1): unknown { + toJSON(message: EncryptedPrivateKeyBundle): unknown { const obj: any = {} message.walletPreKey !== undefined && (obj.walletPreKey = base64FromBytes( @@ -397,10 +331,10 @@ export const EncryptedPrivateKeyBundleV1 = { return obj }, - fromPartial, I>>( + fromPartial, I>>( object: I - ): EncryptedPrivateKeyBundleV1 { - const message = createBaseEncryptedPrivateKeyBundleV1() + ): EncryptedPrivateKeyBundle { + const message = createBaseEncryptedPrivateKeyBundle() message.walletPreKey = object.walletPreKey ?? new Uint8Array() message.ciphertext = object.ciphertext !== undefined && object.ciphertext !== null @@ -410,77 +344,6 @@ export const EncryptedPrivateKeyBundleV1 = { }, } -function createBaseEncryptedPrivateKeyBundle(): EncryptedPrivateKeyBundle { - return { v1: undefined } -} - -export const EncryptedPrivateKeyBundle = { - encode( - message: EncryptedPrivateKeyBundle, - writer: _m0.Writer = _m0.Writer.create() - ): _m0.Writer { - if (message.v1 !== undefined) { - EncryptedPrivateKeyBundleV1.encode( - message.v1, - writer.uint32(10).fork() - ).ldelim() - } - return writer - }, - - decode( - input: _m0.Reader | Uint8Array, - length?: number - ): EncryptedPrivateKeyBundle { - const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input) - let end = length === undefined ? reader.len : reader.pos + length - const message = createBaseEncryptedPrivateKeyBundle() - while (reader.pos < end) { - const tag = reader.uint32() - switch (tag >>> 3) { - case 1: - message.v1 = EncryptedPrivateKeyBundleV1.decode( - reader, - reader.uint32() - ) - break - default: - reader.skipType(tag & 7) - break - } - } - return message - }, - - fromJSON(object: any): EncryptedPrivateKeyBundle { - return { - v1: isSet(object.v1) - ? EncryptedPrivateKeyBundleV1.fromJSON(object.v1) - : undefined, - } - }, - - toJSON(message: EncryptedPrivateKeyBundle): unknown { - const obj: any = {} - message.v1 !== undefined && - (obj.v1 = message.v1 - ? EncryptedPrivateKeyBundleV1.toJSON(message.v1) - : undefined) - return obj - }, - - fromPartial, I>>( - object: I - ): EncryptedPrivateKeyBundle { - const message = createBaseEncryptedPrivateKeyBundle() - message.v1 = - object.v1 !== undefined && object.v1 !== null - ? EncryptedPrivateKeyBundleV1.fromPartial(object.v1) - : undefined - return message - }, -} - declare var self: any | undefined declare var window: any | undefined declare var global: any | undefined diff --git a/src/utils.ts b/src/utils.ts index 97867960e..6cd7a5b1f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,5 @@ import { Waku, WakuMessage } from 'js-waku' import { PublicKeyBundle } from './crypto' -import ContactBundle from './ContactBundle' export const buildContentTopic = (name: string): string => `/xmtp/0/${name}/proto` @@ -53,11 +52,7 @@ export async function publishUserContact( keys: PublicKeyBundle, address: string ): Promise { - const contactBundle = new ContactBundle(keys) await waku.lightPush.push( - await WakuMessage.fromBytes( - contactBundle.toBytes(), - buildUserContactTopic(address) - ) + await WakuMessage.fromBytes(keys.toBytes(), buildUserContactTopic(address)) ) } diff --git a/test/ContactBundle.test.ts b/test/ContactBundle.test.ts deleted file mode 100644 index 5b813a85e..000000000 --- a/test/ContactBundle.test.ts +++ /dev/null @@ -1,18 +0,0 @@ -import * as assert from 'assert' -import ContactBundle from '../src/ContactBundle' -import * as ethers from 'ethers' -import { PrivateKeyBundle } from '../src' - -describe('ContactBundles', function () { - it('roundtrip', async function () { - const priv = PrivateKeyBundle.generate() - - const cb1 = new ContactBundle((await priv).getPublicKeyBundle()) - const bytes1 = cb1.toBytes() - - const cb2 = ContactBundle.fromBytes(bytes1) - const bytes2 = cb2.toBytes() - - assert.deepEqual(bytes1, bytes2) - }) -})