-
Notifications
You must be signed in to change notification settings - Fork 6
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
1 changed file
with
40 additions
and
6 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
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' | ||
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'), | ||
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) | ||
} | ||
} |