UNPKG

@ceramicnetwork/blockchain-utils-linking

Version:

Blockchain utils for linking blockchain accounts to DID

51 lines 2.11 kB
import { AccountId } from 'caip'; import { asOldCaipString, getConsentMessage } from './util.js'; import * as uint8arrays from 'uint8arrays'; import * as sha256 from '@stablelib/sha256'; export const SOLANA_TESTNET_CHAIN_REF = '4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z'; export const SOLANA_DEVNET_CHAIN_REF = 'EtWTRABZaYq6iMfeYKouRu166VU2xqa1'; export const SOLANA_MAINNET_CHAIN_REF = '5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'; export class SolanaAuthProvider { constructor(provider, address, chainRef) { this.provider = provider; this.address = address; this.chainRef = chainRef; this.isAuthProvider = true; console.warn('WARN: SolanaAuthProvider is not fully supported. You may encounter issues using this.'); } async accountId() { return new AccountId({ address: this.address, chainId: `solana:${this.chainRef}`, }); } async authenticate(message) { if (!this.provider.signMessage) { throw new Error(`Unsupported provider; provider must implement signMessage`); } const signatureBytes = await this.provider.signMessage(uint8arrays.fromString(message)); const digest = sha256.hash(signatureBytes); return `0x${uint8arrays.toString(digest, 'base16')}`; } async createLink(did) { if (!this.provider.signMessage) { throw new Error(`Unsupported provider; provider must implement signMessage`); } const { message, timestamp } = getConsentMessage(did, true); const accountID = await this.accountId(); const signatureBytes = await this.provider.signMessage(uint8arrays.fromString(message)); const signature = uint8arrays.toString(signatureBytes, 'base64'); return { version: 2, type: 'solana', message, signature, account: asOldCaipString(accountID), timestamp, }; } withAddress(address) { return new SolanaAuthProvider(this.provider, address, this.chainRef); } } //# sourceMappingURL=solana.js.map