-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some example scripts for backend pass lookup
- Loading branch information
1 parent
7d8ff0a
commit eaabf83
Showing
8 changed files
with
269 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Get the state of a custom pass by calling the Civic Partner API with Oauth client credentials. | ||
// Usage: CLIENT_ID=<client-id> CLIENT_SECRET=<client-secret> bun run getPassStatusUsingAPI.ts <walletAddress> <chain> | ||
|
||
const clientId = process.env.CLIENT_ID; | ||
const clientSecret = process.env.CLIENT_SECRET; | ||
const walletAddress = process.argv[2]; | ||
const chain = process.argv[3]; | ||
|
||
// authentication credentials for the Civic API | ||
const basicAuth = Buffer.from(`${clientId}:${clientSecret}`).toString('base64'); | ||
|
||
const authUrl = "https://auth.civic.com/oauth/token"; | ||
const lookupUrl = `https://api.civic.com/partner/pass/${chain}/${walletAddress}`; | ||
|
||
(async () => { | ||
// fetch the oauth client credentials token | ||
const loginResponse = await fetch(authUrl, { | ||
headers: { | ||
accept: "application/json, text/plain, */*", | ||
authorization: `Basic ${basicAuth}`, | ||
"content-type": "application/x-www-form-urlencoded", | ||
}, | ||
body: "grant_type=client_credentials", | ||
method: "POST" | ||
}); | ||
const token = (await loginResponse.json()).access_token; | ||
|
||
const lookupResponse = await fetch(lookupUrl, { | ||
headers: { | ||
accept: "application/json", | ||
authorization: `Bearer ${token}`, | ||
}, | ||
}); | ||
|
||
const passStatus = await lookupResponse.json(); | ||
console.log(passStatus); | ||
})().catch((error) => console.error(error)); |
30 changes: 30 additions & 0 deletions
30
packages/evm/exampleScripts/getPassStatusUsingContractABI.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Get the state of a pass by calling the contract directly | ||
// Usage: RPC=<your rpc node> bun run getPassStatusUsingContractABI.ts <walletAddress> <gatekeeperNetwork> | ||
|
||
import {ethers} from "ethers"; | ||
import {base58ToBigInt, GATEWAY_CONTRACT_ADDRESS} from "./util"; | ||
|
||
const walletAddress = process.argv[2]; | ||
const gatekeeperNetwork = process.argv[3]; | ||
|
||
if (!walletAddress || !gatekeeperNetwork || !process.env.RPC) { | ||
console.log('Usage: RPC=<your rpc node> bun run getPassStatusUsingContractABI.ts <walletAddress> <gatekeeperNetwork>'); | ||
process.exit(1); | ||
} | ||
|
||
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC); | ||
const slotId = base58ToBigInt(gatekeeperNetwork); | ||
const abi = [ | ||
'function getToken(uint256) view returns (address,uint8,string,uint256,uint256)', | ||
'function getTokenIdsByOwnerAndNetwork(address,uint256,bool) view returns (uint[])', | ||
]; | ||
|
||
const contract = new ethers.Contract(GATEWAY_CONTRACT_ADDRESS, abi, provider); | ||
contract.getTokenIdsByOwnerAndNetwork(walletAddress, slotId, true).then((tokenIds: bigint[]) => { | ||
tokenIds.forEach(async tokenId => { | ||
const [owner, state, identity, expiration, bitmask] = await contract.getToken(tokenId); | ||
console.log(`Token ID: ${tokenId}`); | ||
console.log(`Pass status: ${state}`); | ||
console.log(`Expiration: ${expiration}`); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Get the state of a pass using the @civic/gateway-eth-ts NPM library | ||
// Usage: RPC=<your rpc node> bun run getPassStatusUsingLibrary.ts <walletAddress> <gatekeeperNetwork> | ||
|
||
import { GatewayTs } from '@civic/gateway-eth-ts'; | ||
import {ethers} from "ethers"; | ||
import {base58ToBigInt, GATEWAY_CONTRACT_ADDRESS} from "./util"; | ||
|
||
const walletAddress = process.argv[2]; | ||
const gatekeeperNetwork = process.argv[3]; | ||
|
||
if (!walletAddress || !gatekeeperNetwork || !process.env.RPC) { | ||
console.log('Usage: RPC=<your rpc node> bun run getPassStatusUsingLibrary.ts <walletAddress> <gatekeeperNetwork>'); | ||
process.exit(1); | ||
} | ||
|
||
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC); | ||
const gatewayTs = new GatewayTs(provider, GATEWAY_CONTRACT_ADDRESS) | ||
|
||
gatewayTs.getToken(walletAddress, base58ToBigInt(gatekeeperNetwork)).then(token => { | ||
if (!token) { | ||
console.log('Token not found'); | ||
return; | ||
} | ||
|
||
console.log(`Token ID: ${token.tokenId}`); | ||
console.log(`Pass status: ${token.state}`); | ||
console.log(`Expiration: ${token.expiration}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {decode} from "bs58"; | ||
|
||
const base58ToHex = (base58: string) => { | ||
try { | ||
return decode(base58).toString('hex'); | ||
} catch (error) { | ||
console.log('Error decoding base58 string:', error); | ||
throw new Error(`Invalid base58 string: ${base58}. Pass types created before July 2024 are mapped to a slot ID in the gateway contract - these are not supported here.`); | ||
} | ||
} | ||
const hexToBigInt = (hex: string) => BigInt(`0x${hex}`) | ||
export const base58ToBigInt = (base58: string) => hexToBigInt(base58ToHex(base58)) | ||
|
||
export const GATEWAY_CONTRACT_ADDRESS = '0xF65b6396dF6B7e2D8a6270E3AB6c7BB08BAEF22E'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/solana/exampleScripts/getPassStatusUsingLibrary.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Get the state of a pass using the @civic/solana-gateway-ts NPM library | ||
// Usage: RPC=<your rpc node> bun run getPassStatusUsingLibrary.ts <walletAddress> <gatekeeperNetwork> | ||
|
||
import {findGatewayTokensForOwnerAndNetwork} from '@civic/solana-gateway-ts'; | ||
import {Connection, PublicKey} from "@solana/web3.js"; | ||
|
||
const walletAddress = process.argv[2]; | ||
const gatekeeperNetwork = process.argv[3]; | ||
|
||
if (!walletAddress || !gatekeeperNetwork || !process.env.RPC) { | ||
console.log('Usage: RPC=<your rpc node> bun run getPassStatusUsingLibrary.ts <walletAddress> <gatekeeperNetwork>'); | ||
process.exit(1); | ||
} | ||
|
||
const connection = new Connection(process.env.RPC, 'confirmed'); | ||
|
||
findGatewayTokensForOwnerAndNetwork(connection, new PublicKey(walletAddress), new PublicKey(gatekeeperNetwork)).then((tokens) => { | ||
tokens.forEach((token) => { | ||
console.log(`Pass status: ${token.state}`); | ||
console.log(`Expiration: ${token.expiryTime}`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.