-
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.
- Loading branch information
Showing
7 changed files
with
161 additions
and
8 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
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
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
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 |
---|---|---|
@@ -1,7 +1,17 @@ | ||
import { IdlTypes } from '@coral-xyz/anchor' | ||
import { TokenDispenser } from './token-dispenser' | ||
|
||
export type SignedMessage = { | ||
publicKey: Uint8Array | ||
signature: Uint8Array | ||
// recoveryId is undefined for ed25519 | ||
recoveryId: number | undefined | ||
fullMessage: Uint8Array | ||
} | ||
|
||
export type ClaimCertificate = IdlTypes<TokenDispenser>['ClaimCertificate'] | ||
|
||
export type ClaimSignature = { | ||
sig: string | ||
instruction: ClaimCertificate | null | ||
} |
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
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,123 @@ | ||
import { InfluxDB, Point, WriteApi } from '@influxdata/influxdb-client' | ||
import base32 from 'hi-base32' | ||
import config from '../config' | ||
import { ClaimSignature } from '../types' | ||
import { PublicKey } from '@solana/web3.js' | ||
|
||
export async function saveSignedTransactions( | ||
claimSignatures: ClaimSignature[] | ||
) { | ||
try { | ||
if (!config.influx.url) { | ||
console.log('InfluxDB not configured') | ||
return | ||
} | ||
|
||
const influxWriter = new InfluxDB({ | ||
url: config.influx.url, | ||
token: config.influx.token, | ||
timeout: config.influx.timeout, | ||
writeOptions: {} | ||
}).getWriteApi(config.influx.org, config.influx.bucket) | ||
|
||
const points = claimSignatures.map((claimSignature) => { | ||
const { ecosystem, identity } = mapIdentity(claimSignature) | ||
const point = new Point('ad_signatures') | ||
.stringField('type', 'transaction_signed') | ||
.stringField('sig', claimSignature.sig) | ||
.stringField('ecosystem', ecosystem) | ||
.stringField('identity', identity) | ||
.floatField('amount', claimSignature.instruction?.amount?.toNumber()) | ||
|
||
return point | ||
}) | ||
|
||
influxWriter.writePoints(points) | ||
await influxWriter.close() | ||
} catch (err) { | ||
console.error('Error saving signed transactions', err) | ||
} | ||
} | ||
|
||
function mapIdentity(claimSignature: ClaimSignature) { | ||
const ecosystem = Object.keys( | ||
claimSignature.instruction?.proofOfIdentity ?? { unknown: 'ecosystem' } | ||
)[0] | ||
let identity: any = undefined | ||
let subEcosystem = ecosystem | ||
|
||
switch (ecosystem) { | ||
case 'discord': { | ||
identity = claimSignature.instruction?.proofOfIdentity?.discord?.username | ||
break | ||
} | ||
case 'solana': { | ||
identity = new PublicKey( | ||
claimSignature.instruction?.proofOfIdentity?.solana?.pubkey ?? [] | ||
).toBase58() | ||
break | ||
} | ||
case 'evm': { | ||
identity = | ||
'0x' + | ||
Buffer.from( | ||
claimSignature.instruction?.proofOfIdentity?.evm?.pubkey ?? [] | ||
).toString('hex') | ||
break | ||
} | ||
// TODO: validate correct parsing | ||
case 'osmosis': { | ||
identity = | ||
'0x' + | ||
Buffer.from( | ||
claimSignature.instruction?.proofOfIdentity?.cosmwasm?.pubkey ?? [] | ||
).toString('hex') | ||
subEcosystem = 'osmosis' | ||
break | ||
} | ||
case 'terra': { | ||
identity = | ||
'0x' + | ||
Buffer.from( | ||
claimSignature.instruction?.proofOfIdentity?.cosmwasm?.pubkey ?? [] | ||
).toString('hex') | ||
subEcosystem = 'terra' | ||
break | ||
} | ||
case 'injective': { | ||
identity = | ||
'0x' + | ||
Buffer.from( | ||
claimSignature.instruction?.proofOfIdentity?.injective?.pubkey ?? [] | ||
).toString('hex') | ||
break | ||
} | ||
case 'aptos': { | ||
identity = | ||
'0x' + | ||
Buffer.from( | ||
claimSignature.instruction?.proofOfIdentity?.aptos?.pubkey ?? [] | ||
).toString('hex') | ||
break | ||
} | ||
case 'sui': { | ||
identity = | ||
'0x' + | ||
Buffer.from( | ||
claimSignature.instruction?.proofOfIdentity?.sui?.pubkey ?? [] | ||
).toString('hex') | ||
break | ||
} | ||
case 'algorand': { | ||
identity = base32.encode( | ||
claimSignature.instruction?.proofOfIdentity?.algorand?.pubkey ?? [] | ||
) | ||
break | ||
} | ||
default: { | ||
identity = 'unknown' | ||
} | ||
} | ||
|
||
return { ecosystem, identity } | ||
} |
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