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

Algorand integration #18

Merged
merged 8 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
13 changes: 11 additions & 2 deletions frontend/claim_sdk/claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import * as anchor from '@coral-xyz/anchor'
import { PublicKey } from '@solana/web3.js'
import tokenDispenser from './idl/token_dispenser.json'
import { ethers } from 'ethers'
import { removeLeading0x } from './index'
import { HexString } from 'aptos'
import { base32decode, removeLeading0x } from './index'

// Must be kept in line with the database types and the on-chain program
export type Ecosystem =
| 'discord'
| 'solana'
| 'evm'
| 'sui'
| 'algorand'
| 'aptos'
| 'terra'
| 'osmosis'
Expand All @@ -20,6 +20,7 @@ export const Ecosystems: Ecosystem[] = [
'solana',
'evm',
'sui',
'algorand',
'aptos',
'terra',
'osmosis',
Expand Down Expand Up @@ -84,6 +85,14 @@ export class ClaimInfo {
}
break
}
case 'algorand': {
identityStruct = {
algorand: {
address: base32decode(this.identity),
},
}
break
}
default: {
// TODO: support the other ecosystems
throw new Error(`unknown ecosystem type: ${this.ecosystem}`)
Expand Down
3 changes: 3 additions & 0 deletions frontend/claim_sdk/ecosystems/algorand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function algorandGetFullMessage(payload: string): string {
return 'MX'.concat(payload)
}
16 changes: 15 additions & 1 deletion frontend/claim_sdk/ecosystems/signatures.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { removeLeading0x } from '../index'
import { base32decode, removeLeading0x } from '../index'
import {
evmGetFullMessage,
splitEvmSignature,
Expand All @@ -15,6 +15,7 @@ import { Hash } from '@keplr-wallet/crypto'
import { aptosGetFullMessage } from './aptos'
import { splitSignatureAndPubkey, suiGetFullMessage } from './sui'
import { blake2b } from '@noble/hashes/blake2b'
import { algorandGetFullMessage } from './algorand'

export type SignedMessage = {
publicKey: Uint8Array
Expand Down Expand Up @@ -103,3 +104,16 @@ export function suiBuildSignedMessage(
fullMessage: blake2b(suiGetFullMessage(payload), { dkLen: 32 }),
}
}

export function algorandBuildSignedMessage(
pubkey: string,
signature: string,
payload: string
): SignedMessage {
return {
publicKey: base32decode(pubkey).subarray(0, 32),
signature: Buffer.from(removeLeading0x(signature), 'hex'),
recoveryId: undefined,
fullMessage: Buffer.from(algorandGetFullMessage(payload), 'utf-8'),
}
}
7 changes: 7 additions & 0 deletions frontend/claim_sdk/eventSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@coral-xyz/anchor'
import { ConfirmedSignatureInfo, TransactionSignature } from '@solana/web3.js'
import { TokenDispenser } from './idl/token_dispenser'
import { base32encode } from 'claim_sdk'

export class TokenDispenserEventSubscriber {
eventParser: anchor.EventParser
Expand Down Expand Up @@ -269,6 +270,12 @@ function formatClaimInfo(
address: claimInfo.identity.injective.address,
amount: claimInfo.amount.toString(),
}
} else if (claimInfo.identity.algorand?.pubkey) {
return {
ecosystem: 'algorand',
address: base32encode(claimInfo.identity.algorand.pubkey),
amount: claimInfo.amount.toString(),
}
} else
throw new Error(
`unknown identity type. ${JSON.stringify(claimInfo.identity)}}`
Expand Down
26 changes: 26 additions & 0 deletions frontend/claim_sdk/idl/token_dispenser.json
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,17 @@
"type": "string"
}
]
},
{
"name": "Algorand",
"fields": [
{
"name": "pubkey",
"type": {
"array": ["u8", 32]
}
}
]
}
]
}
Expand Down Expand Up @@ -549,6 +560,21 @@
"type": "u8"
}
]
},
{
"name": "Algorand",
"fields": [
{
"name": "pubkey",
"type": {
"array": ["u8", 32]
}
},
{
"name": "verification_instruction_index",
"type": "u8"
}
]
}
]
}
Expand Down
52 changes: 52 additions & 0 deletions frontend/claim_sdk/idl/token_dispenser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,17 @@ export type TokenDispenser = {
type: 'string'
}
]
},
{
name: 'Algorand'
fields: [
{
name: 'pubkey'
type: {
array: ['u8', 32]
}
}
]
}
]
}
Expand Down Expand Up @@ -549,6 +560,21 @@ export type TokenDispenser = {
type: 'u8'
}
]
},
{
name: 'Algorand'
fields: [
{
name: 'pubkey'
type: {
array: ['u8', 32]
}
},
{
name: 'verification_instruction_index'
type: 'u8'
}
]
}
]
}
Expand Down Expand Up @@ -1064,6 +1090,17 @@ export const IDL: TokenDispenser = {
},
],
},
{
name: 'Algorand',
fields: [
{
name: 'pubkey',
type: {
array: ['u8', 32],
},
},
],
},
],
},
},
Expand Down Expand Up @@ -1177,6 +1214,21 @@ export const IDL: TokenDispenser = {
},
],
},
{
name: 'Algorand',
fields: [
{
name: 'pubkey',
type: {
array: ['u8', 32],
},
},
{
name: 'verification_instruction_index',
type: 'u8',
},
],
},
],
},
},
Expand Down
14 changes: 14 additions & 0 deletions frontend/claim_sdk/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import base32 from 'hi-base32'

export function removeLeading0x(s: string): string {
if (s.startsWith('0x')) {
return s.substring(2)
Expand All @@ -13,3 +15,15 @@ export function envOrErr(env: string): string {
}
return String(process.env[env])
}

export function base32decode(s: string): Buffer {
const address = base32.decode.asBytes(s)
if (address.length != 36) {
throw new Error(`Invalid Algorand address length`)
}
return Buffer.from(address)
}

export function base32encode(s: base32.Input): string {
return base32.encode(s)
}
6 changes: 4 additions & 2 deletions frontend/claim_sdk/solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export const ERROR_CRAFTING_TX = 'error: crafting transaction'
type bump = number
// NOTE: This must be kept in sync with the on-chain program
const AUTHORIZATION_PAYLOAD = [
'Pyth Grant PID:\n',
'W Airdrop PID:\n',
'\nI authorize Solana wallet\n',
'\nto claim my token grant.\n',
'\nto claim my W tokens.\n',
]

/**
Expand Down Expand Up @@ -368,6 +368,7 @@ export class TokenDispenserProvider {
case 'evm':
case 'aptos':
case 'sui':
case 'algorand':
case 'injective': {
return {
[claimInfo.ecosystem]: {
Expand Down Expand Up @@ -433,6 +434,7 @@ export class TokenDispenserProvider {
}
case 'discord':
case 'sui':
case 'algorand':
case 'aptos': {
return Ed25519Program.createInstructionWithPublicKey({
publicKey: signedMessage.publicKey,
Expand Down
28 changes: 28 additions & 0 deletions frontend/claim_sdk/testWallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { aptosGetFullMessage } from './ecosystems/aptos'
import { Ed25519Keypair } from '@mysten/sui.js/keypairs/ed25519'
import { hashDiscordUserId } from '../utils/hashDiscord'
import { getInjectiveAddress } from '../utils/getInjectiveAddress'
import { algorandGetFullMessage } from './ecosystems/algorand'

dotenv.config() // Load environment variables from .env file

Expand Down Expand Up @@ -67,6 +68,10 @@ export async function loadTestWallets(): Promise<
const cosmosPrivateKeyPath = path.resolve(KEY_DIR, 'cosmos_private_key.json')
const aptosPrivateKeyPath = path.resolve(KEY_DIR, 'aptos_private_key.json')
const suiPrivateKeyPath = path.resolve(KEY_DIR, 'sui_private_key.json')
const algorandPrivateKeyPath = path.resolve(
KEY_DIR,
'algorand_private_key.json'
)

const dispenserGuardKeyPath = path.resolve(
KEY_DIR,
Expand All @@ -83,6 +88,7 @@ export async function loadTestWallets(): Promise<
terra: [],
osmosis: [],
injective: [],
algorand: [],
}
result['discord'] = [
DiscordTestWallet.fromKeyfile(TEST_DISCORD_USERNAME, dispenserGuardKeyPath),
Expand All @@ -99,6 +105,7 @@ export async function loadTestWallets(): Promise<
]),
]
result['injective'] = [TestEvmWallet.fromKeyfile(cosmosPrivateKeyPath, true)]
result['algorand'] = [TestAlgorandWallet.fromKeyfile(algorandPrivateKeyPath)]

return result
}
Expand Down Expand Up @@ -288,3 +295,24 @@ export class TestSuiWallet implements TestWallet {
return suiBuildSignedMessage(response, payload)
}
}

export class TestAlgorandWallet implements TestWallet {
constructor(readonly wallet: Keypair) {}
static fromKeyfile(keyFile: string): TestSolanaWallet {
const keypair = Keypair.fromSecretKey(
new Uint8Array(JSON.parse(fs.readFileSync(keyFile, 'utf-8')))
)
return new TestAlgorandWallet(keypair)
}

async signMessage(payload: string): Promise<SignedMessage> {
return hardDriveSignMessage(
Buffer.from(algorandGetFullMessage(payload), 'utf-8'),
this.wallet
)
}

public address(): string {
return this.wallet.publicKey.toBase58()
}
}
Loading
Loading