Created
May 21, 2022 14:15
-
-
Save rsoury/72e0a79210ea156b2110fd3c19b2bb58 to your computer and use it in GitHub Desktop.
CAIP10 Auth Provider for Arweave Wallets
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
import { | |
AuthProvider, | |
asOldCaipString, | |
getConsentMessage, | |
LinkProof | |
} from "@ceramicnetwork/blockchain-utils-linking"; | |
import { AccountId } from "caip"; | |
import * as uint8arrays from "uint8arrays"; | |
export class ArweaveAuthProvider implements AuthProvider { | |
readonly isAuthProvider = true; | |
readonly algorithm = { | |
name: "RSA-PSS", | |
saltLength: 0 | |
}; | |
constructor( | |
private readonly provider: typeof window.arweaveWallet, | |
private readonly address: string | |
) {} | |
async accountId(): Promise<AccountId> { | |
const chainId = `ar:1`; | |
return new AccountId({ address: this.address, chainId }); | |
} | |
async authenticate(message: string): Promise<string> { | |
const arr = uint8arrays.fromString(message); | |
const sig = await this.provider.signature(arr, this.algorithm); | |
const response = uint8arrays.toString(sig); | |
return response; | |
} | |
async createLink(did: string): Promise<LinkProof> { | |
const { message, timestamp } = getConsentMessage(did, true); | |
const arr = uint8arrays.fromString(message); | |
const sig = await this.provider.signature(arr, this.algorithm); | |
const accountId = await this.accountId(); | |
const signature = uint8arrays.toString(sig); | |
return { | |
version: 2, | |
message, | |
signature, | |
account: asOldCaipString(accountId), | |
timestamp | |
}; | |
} | |
withAddress(address: string): AuthProvider { | |
return new ArweaveAuthProvider(this.provider, address); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment