Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
"name": "@arcana/auth",
"version": "1.0.8",
"description": "Arcana Auth",
"main": "dist/standalone/auth.esm.js",
"main": "dist/index.js",
"type": "module",
"module": "dist/standalone/auth.esm.js",
"module": "dist/index.js",
"jsdelivr": "dist/standalone/auth.umd.js",
"unpkg": "dist/standalone/auth.umd.js",
"exports": {
".": {
"types": "./types/index.d.ts",
"require": "./dist/standalone/auth.esm.js",
"import": "./dist/standalone/auth.esm.js"
"require": "./dist/index.js",
"import": "./dist/index.js"
}
},
"types": "types/index.d.ts",
"types": "dist/types/index.d.ts",
"files": [
"dist",
"types"
Expand Down Expand Up @@ -59,7 +59,7 @@
"@rollup/plugin-inject": "^4.0.4",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.3.0",
"@rollup/plugin-typescript": "^8.3.3",
"@rollup/plugin-typescript": "^11.1.5",
"@types/jest": "^27.4.1",
"@types/validator": "^13.11.1",
"@typescript-eslint/eslint-plugin": "^5.18.0",
Expand Down Expand Up @@ -90,6 +90,8 @@
},
"dependencies": {
"@metamask/safe-event-emitter": "^2.0.0",
"@solana/web3.js": "^1.87.3",
"bs58": "^5.0.0",
"eth-rpc-errors": "^4.0.3",
"penpal": "^6.0.1",
"validator": "^13.11.0"
Expand Down
13 changes: 11 additions & 2 deletions rollup.es.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@ import baseConfig from './rollup.base.config'
export default {
...baseConfig,
output: {
file: 'dist/standalone/auth.esm.js',
format: 'es',
dir: 'dist',
// file: 'dist/standalone/auth.esm.js',
format: 'esm',
compact: true,
chunkFileNames: '[name].chunk.js',
manualChunks: function (id) {
if (id.includes('@solana/web3.js/lib')) {
return 'solana'
} else if (id.includes('/node_modules/')) {
return 'vendor'
}
},
},
}
1 change: 1 addition & 0 deletions rollup.umd.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default {
file: 'dist/standalone/auth.umd.js',
format: 'umd',
name: 'arcana.auth',
inlineDynamicImports: true,
compact: true,
},
}
29 changes: 25 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
AppMode,
BearerAuthentication,
ChainConfigInput,
ChainType,
ConstructorParams,
EthereumProvider,
FirebaseBearer,
Expand All @@ -26,23 +27,28 @@ import {
ThemeConfig,
UserInfo,
} from './typings'
import isEmail from 'validator/es/lib/isEmail'
import { getAppInfo, getImageUrls } from './appInfo'
import { ArcanaAuthError, ErrorNotInitialized } from './errors'
import { LOG_LEVEL, setExceptionReporter, setLogLevel } from './logger'
import Popup from './popup'
import { ModalController } from './ui/modalController'
import { ArcanaSolanaAPI } from './solana'

import isEmail from 'validator/es/lib/isEmail'

class AuthProvider {
public appId: string
private params: ConstructorParams
private appConfig: AppConfig
private iframeWrapper: IframeWrapper
private networkConfig: NetworkConfig
private rpcConfig: ChainConfigInput | undefined
private readonly rpcConfig: ChainConfigInput | undefined
private initStatus: InitStatus = InitStatus.CREATED
private initPromises: ((value: AuthProvider) => void)[] = []
private _provider: ArcanaProvider

private readonly _provider: ArcanaProvider
private _solanaAPI: ArcanaSolanaAPI

private connectCtrl: ModalController
private _standaloneMode: {
mode: 1 | 2
Expand Down Expand Up @@ -109,6 +115,12 @@ class AuthProvider {
})
this.setProviders()

switch (this.appConfig.chainType) {
case ChainType.solana_cv25519: {
this._solanaAPI = await ArcanaSolanaAPI.create(this._provider)
}
}

this.initStatus = InitStatus.DONE
this.resolveInitPromises()

Expand Down Expand Up @@ -297,7 +309,7 @@ class AuthProvider {
}

/**
* A function to to be called before trying to .reconnect()
* A function to be called before trying to .reconnect()
*/
public async canReconnect() {
await this.init()
Expand Down Expand Up @@ -384,6 +396,8 @@ class AuthProvider {
appInfo.logo.dark_vertical || appInfo.logo.light_vertical
this.appConfig = {
name: appInfo.name,
// chainType: ChainType.solana_cv25519,
chainType: ChainType.evm_secp256k1,
themeConfig: {
assets: {
logo: {
Expand Down Expand Up @@ -422,6 +436,13 @@ class AuthProvider {
throw ErrorNotInitialized
}

get solana() {
if (this._solanaAPI) {
return this._solanaAPI
}
throw ErrorNotInitialized
}

get logo() {
if (this.initStatus === InitStatus.DONE) {
return this.appConfig.themeConfig.assets.logo
Expand Down
2 changes: 1 addition & 1 deletion src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getLogger, Logger } from './logger'
import IframeWrapper from './iframeWrapper'
import { getCurrentUrl, getHexFromNumber, getUniqueId } from './utils'

interface RequestArguments {
export interface RequestArguments {
method: string
params?: unknown[] | Record<string, unknown>
}
Expand Down
124 changes: 124 additions & 0 deletions src/solana.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import type { ArcanaProvider, RequestArguments } from './provider'
import type * as Web3Module from '@solana/web3.js'
import type * as BS58Module from 'bs58'

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

export class ArcanaSolanaAPI {
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 BS58Module
) {}

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

async request(_args: RequestArguments): Promise<unknown> {
const args = structuredClone(_args)

switch (args.method) {
case 'signMessage': {
const p = args.params as {
message: string | Uint8Array | Buffer
display: string
}
if (p.message instanceof Uint8Array) {
p.message = this.bs58Module.encode(p.message)
}
}
}

const response = await this.p.request(args)

switch (args.method) {
case 'signMessage': {
return this.parseSignatureResponse(response as SignatureResRaw)
}
case 'signTransaction': {
return this.web3Module.VersionedTransaction.deserialize(
this.bs58Module.decode(response as string)
)
}
case 'signAllTransactions': {
return (response as string[]).map((x) =>
this.web3Module.VersionedTransaction.deserialize(
this.bs58Module.decode(x)
)
)
}
default: {
return response
}
}
}

private parseSignatureResponse(input: SignatureResRaw): SignatureRes {
return {
signature: this.bs58Module.decode(input.signature),
publicKey: new this.web3Module.PublicKey(input.publicKey),
}
}

signMessage(data: Uint8Array, display: string): Promise<SignatureRes> {
const r = this.request({
method: 'signMessage',
params: {
message: this.bs58Module.encode(data),
display,
},
})
return r as Promise<SignatureRes>
}

signTransaction(
tx: Web3Module.VersionedTransaction | Web3Module.Transaction
): Promise<Web3Module.VersionedTransaction> {
const r = this.request({
method: 'signTransaction',
params: {
message: this.bs58Module.encode(tx.serialize()),
},
})
return r as Promise<Web3Module.VersionedTransaction>
}

signAndSendTransaction(
tx: Web3Module.VersionedTransaction | Web3Module.Transaction
): Promise<{ signature: string; publicKey: string }> {
const r = this.request({
method: 'signAndSendTransaction',
params: {
message: this.bs58Module.encode(tx.serialize()),
},
})
return r as Promise<{ signature: string; publicKey: string }>
}

signAllTransactions(
txes: (Web3Module.VersionedTransaction | Web3Module.Transaction)[]
): Promise<Web3Module.VersionedTransaction[]> {
const r = this.request({
method: 'signAllTransactions',
params: {
message: txes.map((x) => this.bs58Module.encode(x.serialize())),
},
})
return r as Promise<Web3Module.VersionedTransaction[]>
}
}
7 changes: 7 additions & 0 deletions src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ export enum InitStatus {
DONE,
}

export enum ChainType {
evm_secp256k1 = 'evm_secp256k1',
solana_cv25519 = 'solana_cv25519',
}

export interface IframeWrapperParams {
appId: string
iframeUrl: string
Expand All @@ -72,6 +77,7 @@ export interface ThemeConfig {

export interface AppInfo {
name: string
chain_type: ChainType
theme: Theme
logo: {
dark_horizontal?: string
Expand All @@ -83,6 +89,7 @@ export interface AppInfo {

export interface AppConfig {
name: string
chainType: ChainType
themeConfig: ThemeConfig
}

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"declarationDir": "types",
"declarationDir": "dist/types",
"outDir": "dist",
"strict": true,
"noImplicitAny": true,
Expand Down
Loading