Skip to content

Commit

Permalink
Fix address mapping|add influx token from secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
abhidtu2014 committed Apr 1, 2024
1 parent 9670387 commit 2f122dd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 37 deletions.
12 changes: 9 additions & 3 deletions backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
region: process.env.AWS_REGION ?? 'us-east-2'
},
tokenDispenserProgramId: () => process.env.TOKEN_DISPENSER_PROGRAM_ID,
keys: {
secrets: {
dispenserGuard: {
/** optional. mostly for local testing */
key: process.env.DISPENSER_WALLET_KEY,
Expand All @@ -21,13 +21,19 @@ export default {
secretName:
process.env.FUNDER_WALLET_KEY_SECRET_NAME ??
'xli-test-secret-funder-wallet'
},
influx: {
key: () => process.env.INFLUXDB_TOKEN,
/** required. with a default value */
secretName: process.env.IDB_SECRET_NAME ?? 'xl-ad-idb'
}
},
influx: {
url: () => process.env.INFLUXDB_URL ?? 'http://localhost:8086',
org: () => process.env.INFLUXDB_ORG ?? 'xl',
bucket: () => process.env.INFLUXDB_BUCKET ?? 'ad',
token: () => process.env.INFLUXDB_TOKEN ?? 'token',
timeout: () => parseInt(process.env.INFLUXDB_TIMEOUT_MS ?? '20500')
token: () => process.env.INFLUXDB_TOKEN,
timeout: () => parseInt(process.env.INFLUXDB_TIMEOUT_MS ?? '20500'),
isFlushEnabled: () => process.env.INFLUXDB_FLUSH_ENABLED === 'true' ?? false
}
}
53 changes: 27 additions & 26 deletions backend/src/utils/persistence.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import { InfluxDB, Point } from '@influxdata/influxdb-client'
import { InfluxDB, Point, WriteApi } from '@influxdata/influxdb-client'
import base32 from 'hi-base32'
import { rawSecp256k1PubkeyToRawAddress } from '@cosmjs/amino'
import { Secp256k1 } from '@cosmjs/crypto'
import { toBech32 } from '@cosmjs/encoding'
import config from '../config'
import { ClaimSignature } from '../types'
import { PublicKey } from '@solana/web3.js'
import { getInfluxToken } from './secrets'

const OSMOSIS_ADDRESS_PREFIX = 'osmo'
const EVM_WALLET_ADDRESS_PREFIX = '0x'
const TERRA_ADDRESS_PREFIX = 'terra'
const INJECTIVE_ADDRESS_PREFIX = 'inj'

let influxWriter: WriteApi

export async function saveSignedTransactions(
claimSignatures: ClaimSignature[]
) {
try {
const influxWriter = new InfluxDB({
url: config.influx.url(),
token: config.influx.token(),
timeout: config.influx.timeout()
}).getWriteApi(config.influx.org(), config.influx.bucket())

if (!influxWriter) {
influxWriter = await initInfluxWriter()
}
const points = claimSignatures.map((claimSignature) => {
const { ecosystem, subEcosystem, identity } = mapIdentity(claimSignature)
return new Point('ad_signatures')
Expand All @@ -32,9 +30,10 @@ export async function saveSignedTransactions(
.stringField('identity', identity)
.floatField('amount', claimSignature.instruction?.amount?.toNumber())
})

influxWriter.writePoints(points)
await influxWriter.close()
if (config.influx.isFlushEnabled()) {
await influxWriter.flush()
}
} catch (err) {
console.error('Error saving signed transactions', err)
}
Expand Down Expand Up @@ -66,26 +65,16 @@ function mapIdentity(claimSignature: ClaimSignature) {
).toString('hex')
break
}
case 'osmosis': {
case 'cosmwasm': {
const pubkey = claimSignature.instruction?.proofOfIdentity?.cosmwasm
?.pubkey as unknown as Uint8Array
const compressed = Secp256k1.compressPubkey(pubkey)

identity = toBech32(
OSMOSIS_ADDRESS_PREFIX,
rawSecp256k1PubkeyToRawAddress(compressed)
)
const chainId =
claimSignature.instruction?.proofOfIdentity?.cosmwasm?.chainId ?? 'osmo'
identity = toBech32(chainId, rawSecp256k1PubkeyToRawAddress(compressed))

subEcosystem = 'osmosis'
break
}
case 'terra': {
identity =
TERRA_ADDRESS_PREFIX +
Buffer.from(
claimSignature.instruction?.proofOfIdentity?.cosmwasm?.pubkey ?? []
).toString('hex')
subEcosystem = 'terra'
subEcosystem = chainId
break
}
case 'injective': {
Expand Down Expand Up @@ -125,3 +114,15 @@ function mapIdentity(claimSignature: ClaimSignature) {

return { ecosystem, subEcosystem, identity }
}

async function initInfluxWriter() {
const token = await getInfluxToken()
return new InfluxDB({
url: config.influx.url(),
token
}).getWriteApi(config.influx.org(), config.influx.bucket(), 'ms')
}

process.on('SIGTERM', async () => {
await influxWriter.close()
})
24 changes: 18 additions & 6 deletions backend/src/utils/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,40 @@ const client = new SecretsManagerClient({ region: config.aws.region })

export async function getDispenserKey() {
let key: string
if (config.keys.dispenserGuard.key) {
if (config.secrets.dispenserGuard.key) {
console.log('Using dispenser guard key from config')
key = config.keys.dispenserGuard.key
key = config.secrets.dispenserGuard.key
} else {
key = await getSecretKey(config.keys.dispenserGuard.secretName, 'key')
key = await getSecretKey(config.secrets.dispenserGuard.secretName, 'key')
}

return { key: JSON.parse(key) }
}

export async function getFundingKey(): Promise<{ key: Uint8Array }> {
let key: string
if (config.keys.funding.key()) {
if (config.secrets.funding.key()) {
console.log('Using funding key from config')
key = config.keys.funding.key()!
key = config.secrets.funding.key()!
} else {
key = await getSecretKey(config.keys.funding.secretName, 'key')
key = await getSecretKey(config.secrets.funding.secretName, 'key')
}

return { key: JSON.parse(key) }
}

export async function getInfluxToken(): Promise<string> {
let key: string
if (config.secrets.influx.key()) {
console.log('Using influx token from config')
key = config.secrets.influx.key()!
} else {
key = await getSecretKey(config.secrets.influx.secretName, 'key')
}

return key
}

export async function getSecretKey(secretName: string, keyName: string) {
const secret = await getSecret(secretName)
return secret[keyName]
Expand Down
5 changes: 3 additions & 2 deletions backend/test/handlers/fund-transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { AnchorProvider, IdlTypes, Program, BN } from '@coral-xyz/anchor'
import NodeWallet from '@coral-xyz/anchor/dist/cjs/nodewallet'
import { IDL, TokenDispenser } from '../../src/token-dispenser'
import { fundTransactions } from '../../src/handlers/fund-transactions'
import { GenericContainer, StartedTestContainer, Wait } from 'testcontainers'
import { GenericContainer, StartedTestContainer } from 'testcontainers'
import { InfluxDB } from '@influxdata/influxdb-client'

const RANDOM_BLOCKHASH = 'HXq5QPm883r7834LWwDpcmEM8G8uQ9Hqm1xakCHGxprV'
Expand Down Expand Up @@ -54,10 +54,11 @@ describe('fundTransactions integration test', () => {
process.env.INFLUXDB_URL = `http://${startedInflux.getHost()}:${startedInflux.getMappedPort(
8086
)}`
process.env.INFLUXDB_FLUSH_ENABLED = 'true'

process.env.FUNDING_WALLET_KEY = `[${FUNDER_KEY.secretKey}]`
process.env.TOKEN_DISPENSER_PROGRAM_ID = PROGRAM_ID.toString()
}, 15_000)
}, 20_000)

afterAll(async () => {
await startedInflux.stop()
Expand Down

0 comments on commit 2f122dd

Please sign in to comment.