Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add staticCanMessage method #158

Merged
merged 19 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ import { Client } from '@xmtp/react-native-sdk'
import { ConnectWallet, useSigner } from "@thirdweb-dev/react-native";

// Create the client with your wallet. This will connect to the XMTP development network by default
const xmtp = await XMTP.Client.create(useSigner());
const xmtp = await Client.create(useSigner());
// Start a conversation with XMTP
const conversation = await xmtp.conversations.newConversation(
'0x3F11b27F323b62B159D2642964fa27C46C841897'
Expand Down Expand Up @@ -275,8 +275,8 @@ If you would like to check and see if a blockchain address is registered on the
```tsx
import { Client } from '@xmtp/react-native-sdk'

const isOnDevNetwork = await Client.canMessage(
'0x3F11b27F323b62B159D2642964fa27C46C841897'
const isOnProdNetwork = await Client.canMessage(
'0x3F11b27F323b62B159D2642964fa27C46C841897', { env: 'production' }
)
```

Expand All @@ -298,15 +298,15 @@ const { Client } = require('@xmtp/react-native-sdk')
async function main() {
//Create a random wallet for example purposes. On the frontend you should replace it with the user's wallet (metamask, rainbow, etc)
//Initialize the xmtp client
const xmtp = await XMTP.Client.createRandom({ env: "dev" });
const xmtp = await Client.createRandom({ env: "dev" });

//In this example we are going to broadcast to the GM_BOT wallet (already activated) and a random wallet (not activated)
const GM_BOT = '0x937C0d4a6294cdfa575de17382c7076b579DC176'
const test = ethers.Wallet.createRandom()
const broadcasts_array = [GM_BOT, test.address]

//Querying the activation status of the wallets
const broadcasts_canMessage = await Client.canMessage(broadcasts_array)
const broadcasts_canMessage = await xmtp.canMessage(broadcasts_array)
for (let i = 0; i < broadcasts_array.length; i++) {
//Checking the activation status of each wallet
const wallet = broadcasts_array[i]
Expand All @@ -331,7 +331,7 @@ To learn more about content types, see [Content types with XMTP](https://xmtp.or
Support for other types of content can be added by registering additional `ContentCodecs` with the `Client`. Every codec is associated with a content type identifier, `ContentTypeId`, which is used to signal to the client which codec should be used to process the content that is being sent or received.
For example, see the [Native Codecs](https://github.com/xmtp/xmtp-react-native/tree/main/src/lib/NativeCodecs) available in `xmtp-react-native`.

```ts
```tsx
// Assuming we've loaded a fictional NumberCodec that can be used to encode numbers,
// and is identified with ContentTypeNumber, we can use it as follows.

Expand All @@ -343,7 +343,7 @@ conversation.send(3.14, {

Additional codecs can be configured through the `ClientOptions` parameter of `Client.create`. The `codecs` option is a list of codec instances that should be added to the default set of codecs (currently only the `TextCodec`). If a codec is added for a content type that is already in the default set, it will replace the original codec.

```ts
```tsx
// Adding support for `xmtp.org/reaction` content type
import { ReactionCodec } from '@xmtp/react-native-sdk'
const xmtp = Client.create(wallet, { codecs: [new ReactionCodec()] })
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ repositories {
dependencies {
implementation project(':expo-modules-core')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
implementation "org.xmtp:android:0.6.14"
implementation "org.xmtp:android:0.6.17"
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'com.facebook.react:react-native:0.71.3'
implementation "com.daveanthonythomas.moshipack:moshipack:1.0.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ class XMTPModule : Module() {
client.canMessage(peerAddress)
}

AsyncFunction("staticCanMessage") { peerAddress: String, environment: String, appVersion: String? ->
try {
logV("staticCanMessage")
val options = ClientOptions(api = apiEnvironments(environment, appVersion))
Client.canMessage(peerAddress = peerAddress, options = options)
} catch (e: Exception) {
throw XMTPException("Failed to create client: ${e.message}")
}
}

AsyncFunction("encryptAttachment") { clientAddress: String, fileJson: String ->
logV("encryptAttachment")
val client = clients[clientAddress] ?: throw XMTPException("No client")
Expand Down
38 changes: 19 additions & 19 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ PODS:
- ExpoModulesCore
- EXFont (11.1.1):
- ExpoModulesCore
- EXImageLoader (4.4.0):
- EXImageLoader (4.3.0):
- ExpoModulesCore
- React-Core
- Expo (48.0.21):
- Expo (48.0.20):
- ExpoModulesCore
- ExpoDocumentPicker (11.7.0):
- ExpoDocumentPicker (11.5.4):
- ExpoModulesCore
- ExpoImagePicker (14.5.0):
- ExpoImagePicker (14.3.2):
- ExpoModulesCore
- ExpoKeepAwake (12.0.1):
- ExpoModulesCore
Expand Down Expand Up @@ -302,11 +302,11 @@ PODS:
- React-jsinspector (0.71.8)
- React-logger (0.71.8):
- glog
- react-native-blob-util (0.19.4):
- react-native-blob-util (0.19.0):
- React-Core
- react-native-encrypted-storage (4.0.3):
- React-Core
- react-native-mmkv (2.11.0):
- react-native-mmkv (2.8.0):
- MMKV (>= 1.2.13)
- React-Core
- react-native-safe-area-context (4.5.0):
Expand Down Expand Up @@ -402,7 +402,7 @@ PODS:
- RNScreens (3.20.0):
- React-Core
- React-RCTImage
- RNSVG (13.14.0):
- RNSVG (13.9.0):
- React-Core
- secp256k1.swift (0.1.4)
- SwiftProtobuf (1.20.3)
Expand All @@ -411,15 +411,15 @@ PODS:
- GenericJSON (~> 2.0)
- Logging (~> 1.0.0)
- secp256k1.swift (~> 0.1)
- XMTP (0.6.12-alpha0):
- XMTP (0.6.14-alpha0):
- Connect-Swift (= 0.3.0)
- GzipSwift
- web3.swift
- XMTPRust (= 0.3.6-beta0)
- XMTPReactNative (0.1.0):
- ExpoModulesCore
- MessagePacker
- XMTP (= 0.6.12-alpha0)
- XMTP (= 0.6.14-alpha0)
- XMTPRust (0.3.6-beta0)
- Yoga (1.14.0)

Expand Down Expand Up @@ -613,10 +613,10 @@ SPEC CHECKSUMS:
EXConstants: f348da07e21b23d2b085e270d7b74f282df1a7d9
EXFileSystem: 844e86ca9b5375486ecc4ef06d3838d5597d895d
EXFont: 6ea3800df746be7233208d80fe379b8ed74f4272
EXImageLoader: 03063370bc06ea1825713d3f55fe0455f7c88d04
Expo: 0d9f112757acc6bf32103eabccf91267780bd580
ExpoDocumentPicker: d3b6b0ed2dbbc2f05158e0294dd3f4673f386e5f
ExpoImagePicker: 9e5c745cb3e56ee00e1cfe5d6af59caab66ecf1a
EXImageLoader: 34b214f9387e98f3c73989f15d8d5b399c9ab3f7
Expo: b7d2843b0a0027d0ce76121a63085764355a16ed
ExpoDocumentPicker: 5cb7389ff935b4addefdd466a606de51a512e922
ExpoImagePicker: c58fdf28be551681a8edc550bcec76c397a8dfcd
ExpoKeepAwake: 69f5f627670d62318410392d03e0b5db0f85759a
ExpoModulesCore: 653958063a301098b541ae4dfed1ac0b98db607b
EXSplashScreen: 0e0a9ba0cf7553094e93213099bd7b42e6e237e9
Expand Down Expand Up @@ -646,9 +646,9 @@ SPEC CHECKSUMS:
React-jsiexecutor: 747911ab5921641b4ed7e4900065896597142125
React-jsinspector: c712f9e3bb9ba4122d6b82b4f906448b8a281580
React-logger: 342f358b8decfbf8f272367f4eacf4b6154061be
react-native-blob-util: 30a6c9fd067aadf9177e61a998f2c7efb670598d
react-native-blob-util: 2b6627b288e3bd9874704ea9a153f0a179e4f3f5
react-native-encrypted-storage: db300a3f2f0aba1e818417c1c0a6be549038deb7
react-native-mmkv: e97c0c79403fb94577e5d902ab1ebd42b0715b43
react-native-mmkv: 7da5e18e55c04a9af9a7e0ab9792a1e8d33765a1
react-native-safe-area-context: 39c2d8be3328df5d437ac1700f4f3a4f75716acc
React-perflogger: d21f182895de9d1b077f8a3cd00011095c8c9100
React-RCTActionSheet: 0151f83ef92d2a7139bba7dfdbc8066632a6d47b
Expand All @@ -664,15 +664,15 @@ SPEC CHECKSUMS:
React-runtimeexecutor: 7c51ae9d4b3e9608a2366e39ccaa606aa551b9ed
ReactCommon: 85c98ab0a509e70bf5ee5d9715cf68dbf495b84c
RNScreens: 218801c16a2782546d30bd2026bb625c0302d70f
RNSVG: d00c8f91c3cbf6d476451313a18f04d220d4f396
RNSVG: 53c661b76829783cdaf9b7a57258f3d3b4c28315
secp256k1.swift: a7e7a214f6db6ce5db32cc6b2b45e5c4dd633634
SwiftProtobuf: b02b5075dcf60c9f5f403000b3b0c202a11b6ae1
web3.swift: 2263d1e12e121b2c42ffb63a5a7beb1acaf33959
XMTP: a1c4aaae8e0b05c95223a75c055dc120962760fb
XMTPReactNative: c1cb2c303284405b3a09c0fd9b21972d03762b22
XMTP: 5314a5151dbfad2e24bb5910e679052916a9faff
XMTPReactNative: 5e393ab43d402bae404f675245cfc66a078a33ff
XMTPRust: 3c958736a4f4ee798e425b5644551f1c948da4b0
Yoga: 065f0b74dba4832d6e328238de46eb72c5de9556

PODFILE CHECKSUM: 522d88edc2d5fac4825e60a121c24abc18983367

COCOAPODS: 1.14.2
COCOAPODS: 1.13.0
2 changes: 1 addition & 1 deletion example/src/ConversationScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
} from 'xmtp-react-native-sdk'

import { NavigationParamList } from './Navigation'
import { useXmtp } from './XmtpContext'
import {
useConversation,
useMessage,
Expand All @@ -42,7 +43,6 @@ import {
useLoadRemoteAttachment,
usePrepareRemoteAttachment,
} from './hooks'
import { useXmtp } from './XmtpContext'

type Attachment = {
file?: DocumentPickerAsset
Expand Down
22 changes: 22 additions & 0 deletions example/src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,28 @@ test('canManagePreferences', async () => {
return true
})

test('is address on the XMTP network', async () => {
const alix = await Client.createRandom({ env: 'local' })
const notOnNetwork = '0x0000000000000000000000000000000000000000'

const isAlixAddressAvailable = await Client.canMessage(alix.address, {
env: 'local',
})
const isAddressAvailable = await Client.canMessage(notOnNetwork, {
env: 'local',
})

if (!isAlixAddressAvailable) {
throw new Error('alix address should be available')
}

if (isAddressAvailable) {
throw new Error('address not on network should not be available')
}

return true
})

test('register and use custom content types', async () => {
const bob = await Client.createRandom({
env: 'local',
Expand Down
9 changes: 9 additions & 0 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ public class XMTPModule: Module {
return try await client.canMessage(peerAddress)
}

AsyncFunction("staticCanMessage") { (peerAddress: String, environment: String, appVersion: String?) -> Bool in
do {
let options = createClientConfig(env: environment, appVersion: appVersion)
return try await XMTP.Client.canMessage(peerAddress, options: options)
} catch {
throw Error.noClient
}
}

AsyncFunction("encryptAttachment") { (clientAddress: String, fileJson: String) -> String in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
Expand Down
2 changes: 1 addition & 1 deletion ios/XMTPReactNative.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ Pod::Spec.new do |s|

s.source_files = "**/*.{h,m,swift}"
s.dependency "MessagePacker"
s.dependency "XMTP", "= 0.6.12-alpha0"
s.dependency "XMTP", "= 0.6.14-alpha0"
end
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ export async function canMessage(
return await XMTPModule.canMessage(clientAddress, peerAddress)
}

export async function staticCanMessage(
peerAddress: string,
environment: 'local' | 'dev' | 'production',
appVersion?: string | undefined
): Promise<boolean> {
return await XMTPModule.staticCanMessage(peerAddress, environment, appVersion)
}

export async function encryptAttachment(
clientAddress: string,
file: DecryptedLocalAttachment
Expand Down
21 changes: 21 additions & 0 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,27 @@ export class Client<ContentTypes> {
return await XMTPModule.canMessage(this.address, peerAddress)
}

/**
* Static method to determine if the address is currently in our network.
*
* This method checks if the specified peer has signed up for XMTP.
*
* @param {string} peerAddress - The address of the peer to check for messaging eligibility.
* @param {Partial<ClientOptions>} opts - Optional configuration options for the Client.
* @returns {Promise<boolean>}
*/
static async canMessage(
peerAddress: string,
opts?: Partial<ClientOptions>
): Promise<boolean> {
const options = defaultOptions(opts)
return await XMTPModule.staticCanMessage(
peerAddress,
options.env,
options.appVersion
)
}

constructor(
address: string,
codecs: XMTPModule.ContentCodec<ContentTypes>[] = []
Expand Down
Loading