Skip to content

Commit

Permalink
add admin native auth token generation
Browse files Browse the repository at this point in the history
  • Loading branch information
michavie committed Feb 24, 2024
1 parent 65735f9 commit 50b2152
Show file tree
Hide file tree
Showing 5 changed files with 671 additions and 517 deletions.
7 changes: 6 additions & 1 deletion apps/api/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const config = () => ({
app: {
name: process.env.APP_NAME || 'aggregator',
env: process.env.APP_ENV || 'mainnet',
url: process.env.APP_DOMAIN || 'https://itheum.io',
},
features: {
publicApi: {
Expand All @@ -22,8 +23,12 @@ export const config = () => ({
},
chain: {
apiUrl: process.env.CHAIN_API_URL || 'https://api.multiversx.com',
wallet: {
admin: process.env.WALLET_ADMIN_PEM || '####',
},
nativeAuth: {
acceptedOrigins: ['https://utils.multiversx.com'],
maxExpirySeconds: 3600,
acceptedOrigins: ['https://itheum.io', 'https://utils.multiversx.com'],
},
},
redis: {
Expand Down
6 changes: 6 additions & 0 deletions apps/cache-warmer/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export const config = () => ({
aggregator: process.env.CONTRACT_AGGREGATOR || '####',
},
services: {
chain: {
apiUrl: process.env.CHAIN_API_URL || 'https://api.multiversx.com',
nativeAuth: {
acceptedOrigins: ['https://utils.multiversx.com'],
},
},
redis: {
host: process.env.REDIS_HOST || '127.0.0.1',
port: parseInt(process.env.REDIS_PORT || '6379'),
Expand Down
22 changes: 20 additions & 2 deletions libs/common/src/admin/admin.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { config } from 'apps/api/config'
import { Injectable } from '@nestjs/common'
import { AppConfigService } from '../config'
import { UserSigner } from '@multiversx/sdk-wallet'
import { Account, Transaction } from '@multiversx/sdk-core'
import { Account, SignableMessage, Transaction } from '@multiversx/sdk-core'
import { NativeAuthClient } from '@multiversx/sdk-native-auth-client'

@Injectable()
export class AdminService {
public readonly signer: UserSigner
public readonly account: Account

constructor(private readonly appConfigService: AppConfigService) {
this.signer = UserSigner.fromPem('####')
this.signer = UserSigner.fromPem(config().services.chain.wallet.admin)
this.account = new Account(this.signer.getAddress())
}

Expand Down Expand Up @@ -37,4 +39,20 @@ export class AdminService {

return tx
}

async generateNativeAuthToken() {
const client = new NativeAuthClient({
apiUrl: config().services.chain.apiUrl,
expirySeconds: config().services.chain.nativeAuth.maxExpirySeconds,
origin: config().app.url,
})

const init = await client.initialize()
const address = this.account.address.bech32()
const signable = new SignableMessage({ message: Buffer.from(`${address}${init}`) })
const signature = await this.signer.sign(signable.serializeForSigning())
const signatureHex = signature.toString('hex')

return client.getToken(address, init, signatureHex)
}
}
Loading

0 comments on commit 50b2152

Please sign in to comment.