UNPKG

@ceramicnetwork/blockchain-utils-linking

Version:

Blockchain utils for linking blockchain accounts to DID

54 lines 1.94 kB
import { AccountId } from 'caip'; import { asOldCaipString, getConsentMessage } from './util.js'; import * as uint8arrays from 'uint8arrays'; export class FilecoinAuthProvider { constructor(provider, address) { this.provider = provider; this.address = address; this.isAuthProvider = true; console.warn('WARN: FilecoinAuthProvider is not fully supported. You may encounter issues using this.'); } async accountId() { const prefix = this.address[0]; const chainId = `fil:${prefix}`; return new AccountId({ address: this.address, chainId }); } async authenticate(message) { const payload = asTransaction(this.address, JSON.stringify(message)); const signatureResponse = await this.provider.sign(this.address, payload); return signatureResponse.Signature.Data; } async createLink(did) { const { message, timestamp } = getConsentMessage(did, true); const payload = asTransaction(this.address, message); const signatureResponse = await this.provider.sign(this.address, payload); const accountId = await this.accountId(); return { version: 2, type: 'eoa-tx', message: message, signature: signatureResponse.Signature.Data, account: asOldCaipString(accountId), timestamp: timestamp, }; } withAddress(address) { return new FilecoinAuthProvider(this.provider, address); } } export function asTransaction(address, message) { const messageParams = uint8arrays.toString(uint8arrays.fromString(message), 'base64'); return { From: address, To: address, Value: '0', Method: 0, GasPrice: '1', GasLimit: 1000, Nonce: 0, Params: messageParams, GasFeeCap: '1', GasPremium: '1', }; } //# sourceMappingURL=filecoin.js.map