Skip to content

Commit 04cbb07

Browse files
committed
feat: use utility for ns conversion
1 parent 77640ad commit 04cbb07

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

src/ApiClient.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { messageApi } from '@xmtp/proto'
22
import { NotifyStreamEntityArrival } from '@xmtp/proto/ts/dist/types/fetch.pb'
3-
import { retry, sleep } from './utils'
3+
import { dateToNs, retry, sleep } from './utils'
44
import Long from 'long'
55
import AuthCache from './authn/AuthCache'
66
import { Authenticator } from './authn'
@@ -48,7 +48,7 @@ export type SubscribeCallback = NotifyStreamEntityArrival<messageApi.Envelope>
4848
export type UnsubscribeFn = () => Promise<void>
4949

5050
const toNanoString = (d: Date | undefined): undefined | string => {
51-
return d && Long.fromNumber(d.valueOf()).multiply(1_000_000).toString()
51+
return d && dateToNs(d).toString()
5252
}
5353

5454
const isAbortError = (err?: Error): boolean => {

src/Invitation.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { invitation } from '@xmtp/proto'
44
import Ciphertext from './crypto/Ciphertext'
55
import { decrypt, encrypt } from './crypto'
66
import { PrivateKeyBundleV2 } from './crypto/PrivateKeyBundle'
7+
import { dateToNs } from './utils'
78

89
/**
910
* InvitationV1 is a protobuf message to be encrypted and used as the ciphertext in a SealedInvitationV1 message
@@ -184,7 +185,7 @@ export class SealedInvitation implements invitation.SealedInvitation {
184185
const headerBytes = new SealedInvitationHeaderV1({
185186
sender: sender.getPublicKeyBundle(),
186187
recipient,
187-
createdNs: Long.fromNumber(created.valueOf()).multiply(1_000_000),
188+
createdNs: dateToNs(created),
188189
}).toBytes()
189190

190191
const secret = await sender.sharedSecret(

src/authn/AuthData.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { authn as authnProto } from '@xmtp/proto'
22
import Long from 'long'
3+
import { dateToNs } from '../utils'
34

45
export default class AuthData implements authnProto.AuthData {
56
walletAddr: string
@@ -13,8 +14,8 @@ export default class AuthData implements authnProto.AuthData {
1314
static create(walletAddr: string, timestamp?: Date): AuthData {
1415
timestamp = timestamp || new Date()
1516
return new AuthData({
16-
walletAddr: walletAddr,
17-
createdNs: Long.fromNumber(timestamp.getTime()).multiply(1_000_000),
17+
walletAddr,
18+
createdNs: dateToNs(timestamp),
1819
})
1920
}
2021

src/utils.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Long from 'long'
2+
13
export type IsRetryable = (err?: Error) => boolean
24

35
export const buildContentTopic = (name: string): string =>
@@ -70,3 +72,7 @@ export async function retry<T extends (...arg0: any[]) => any>(
7072
return retry(fn, args, maxRetries, sleepTime, isRetryableFn, currRetry + 1)
7173
}
7274
}
75+
76+
export function dateToNs(date: Date): Long {
77+
return Long.fromNumber(date.valueOf()).multiply(1_000_000)
78+
}

test/ApiClient.test.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { sleep } from './helpers'
66
import { Authenticator } from '../src/authn'
77
import { PrivateKey } from '../src'
88
import { version } from '../package.json'
9+
import { dateToNs } from '../src/utils'
910
const { MessageApi } = messageApi
1011

1112
const PATH_PREFIX = 'http://fake:5050'
@@ -165,9 +166,7 @@ describe('Publish', () => {
165166
{
166167
message: msg.message,
167168
contentTopic: msg.contentTopic,
168-
timestampNs: Long.fromNumber(now.valueOf())
169-
.multiply(1_000_000)
170-
.toString(),
169+
timestampNs: dateToNs(now).toString(),
171170
},
172171
],
173172
}

0 commit comments

Comments
 (0)