Skip to content

Commit

Permalink
Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mmjee committed Nov 1, 2023
1 parent af41121 commit 8686b87
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions src/solana.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,62 @@
import type { ArcanaProvider, RequestArguments } from './provider'
import type * as Web3Module from '@solana/web3.js'

Check failure on line 2 in src/solana.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module '@solana/web3.js' or its corresponding type declarations.
import * as bs58 from 'bs58'

type SignatureRes = {
publicKey: Web3Module.PublicKey // can use new solanaWeb3.PublicKey(address),
signature: Uint8Array
}
type SignatureResRaw = {
signature: string
publicKey: string
}

export class ArcanaSolanaAPI {
constructor(private p: ArcanaProvider) {}
static async create(p: ArcanaProvider) {
const [w3, bs58] = await Promise.all([
import('@solana/web3.js'),

Check failure on line 17 in src/solana.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module '@solana/web3.js' or its corresponding type declarations.
import('bs58'),
])
return new ArcanaSolanaAPI(p, w3, bs58)
}
constructor(
private p: ArcanaProvider,
private web3Module: typeof Web3Module,
private bs58Module: typeof bs58
) {}

get isConnected() {
return this.p.connected
}

async request(args: RequestArguments): Promise<unknown> {
const response = await this.p.request(args)
switch (args.method) {
case 'signMessage': {
return bs58.decode(<string>response)
return this.parseSignatureResponse(response as SignatureResRaw)
}
default: {
return response
}
}
}

async signMessage(data: Uint8Array, display: string): Promise<Uint8Array> {
return (await this.p.request({
private parseSignatureResponse(input: SignatureResRaw): SignatureRes {
return {
signature: this.bs58Module.decode(input.signature),
publicKey: new this.web3Module.PublicKey(input.publicKey),
}
}

async signMessage(data: Uint8Array, display: string): Promise<SignatureRes> {
const response = (await this.p.request({
method: 'signMessage',
params: {
message: data,
message: this.bs58Module.encode(data),
display,
},
})) as Uint8Array
})) as SignatureResRaw

return this.parseSignatureResponse(response)
}
}

0 comments on commit 8686b87

Please sign in to comment.