Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | 77x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 2x 2x 1x 3x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 69x 69x 6x 6x 10x 10x 10x 15x 2x 2x | import {ReceiveOptions} from '@greymass/buoy' import { CallbackPayload, CallbackType, ChainDefinition, ChainId, LoginContext, PrivateKey, PublicKey, Signature, SigningRequest, UInt64, } from '@wharfkit/session' import {SealedMessage, sealedMessagePayload} from '@wharfkit/sealed-messages' import {generateReturnUrl, uuid} from './utils' import {BuoySession} from './buoy-types' import WebSocket from 'isomorphic-ws' export interface WalletPluginOptions { buoyUrl?: string buoyWs?: WebSocket } export interface IdentityRequestResponse { callback request: SigningRequest // Request for multi-device login sameDeviceRequest: SigningRequest // Request for same-device login requestKey: PublicKey privateKey: PrivateKey } /** * createIdentityRequest * * @param context LoginContext * @returns */ export async function createIdentityRequest( context: LoginContext, buoyUrl: string ): Promise<IdentityRequestResponse> { // Create a new private key and public key to act as the request key const privateKey = PrivateKey.generate('K1') const requestKey = privateKey.toPublic() // Create a new BuoySession struct to be used as the info field const createInfo = BuoySession.from({ session_name: context.appName, request_key: requestKey, user_agent: getUserAgent(), }) // Determine based on the options whether this is a multichain request const isMultiChain = !(context.chain || context.chains.length === 1) // Create the callback const callbackChannel = prepareCallbackChannel(buoyUrl) // Determine the chain id(s) to use const chainId: ChainId | null = isMultiChain ? null : context.chain ? ChainId.from(context.chain.id.array) : null const chainIds: ChainId[] = isMultiChain ? context.chains.map((c) => ChainId.from(c.id.array)) : [] // Create the request const request = SigningRequest.identity( { callback: prepareCallback(callbackChannel), scope: String(context.appName), chainId, chainIds, info: { link: createInfo, scope: String(context.appName), }, }, context.esrOptions ) const sameDeviceRequest = request.clone() Iif (typeof window !== 'undefined') { const returnUrl = generateReturnUrl() sameDeviceRequest.setInfoKey('same_device', true) Iif (returnUrl !== undefined) { sameDeviceRequest.setInfoKey('return_path', returnUrl) } } // Return the request and the callback data return { callback: callbackChannel, request, sameDeviceRequest, requestKey, privateKey, } } /** * prepareTransactionRequest * * @param resolved ResolvedSigningRequest * @returns */ export function setTransactionCallback(request: SigningRequest, buoyUrl) { const callback = prepareCallbackChannel(buoyUrl) request.setCallback(`${callback.service}/${callback.channel}`, true) return callback } export function getUserAgent(): string { const version = '__ver' let agent = `@wharfkit/protocol-esr ${version}` Iif (typeof navigator !== 'undefined') { agent += ' ' + navigator.userAgent } return agent } export function prepareCallback(callbackChannel: ReceiveOptions): CallbackType { const {service, channel} = callbackChannel return { url: `${service}/${channel}`, background: true, } } function prepareCallbackChannel(buoyUrl): ReceiveOptions { return { service: buoyUrl, channel: uuid(), } } export async function sealMessage( message: string, privateKey: PrivateKey, publicKey: PublicKey, nonce?: UInt64 ): Promise<SealedMessage> { return sealedMessagePayload(message, privateKey, publicKey, nonce) } export async function verifyLoginCallbackResponse(callbackResponse, context: LoginContext) { if (!callbackResponse.sig || callbackResponse.sig.length === 0) { throw new Error('Invalid response, must have at least one signature') } let chain: ChainDefinition Iif (!context.chain && context.chains.length > 1) { Iif (!callbackResponse.cid) { throw new Error('Multi chain response payload must specify resolved chain id (cid)') } } else { chain = context.chain || context.chains[0] if (callbackResponse.cid && String(chain.id) !== callbackResponse.cid) { throw new Error('Got response for wrong chain id') } } } export function extractSignaturesFromCallback(payload: CallbackPayload): Signature[] { const signatures: string[] = [] if (payload.sig) { signatures.push(String(payload.sig)) } // Tolerate zero-based (`sig0`) and one-based (`sig1`, swift-eosio) numbering, including gaps. const indexed = Object.keys(payload) .map((key) => /^sig(\d+)$/.exec(key)) .filter((match): match is RegExpExecArray => match !== null) .sort((a, b) => Number(a[1]) - Number(b[1])) for (const match of indexed) { const sig = payload[match[0]] if (sig) { signatures.push(String(sig)) } } // Deduplicate and make signatures return [...new Set(signatures)].map((s) => Signature.from(s)) } export function isCallback(object: any): object is CallbackPayload { return 'tx' in object } |