freelii-passkey-kit
Version:
A helper library for creating and using smart wallet accounts on the Stellar blockchain.
171 lines (156 loc) • 9.62 kB
text/typescript
import { Buffer } from "buffer";
import {
AssembledTransaction,
Client as ContractClient,
ClientOptions as ContractClientOptions,
MethodOptions,
Spec as ContractSpec,
} from '@stellar/stellar-sdk/minimal/contract';
import type {
u32,
Option,
} from '@stellar/stellar-sdk/minimal/contract';
if (typeof window !== 'undefined') {
//@ts-ignore Buffer exists
window.Buffer = window.Buffer || Buffer;
}
export const Errors = {
1: { message: "NotFound" },
2: { message: "AlreadyExists" },
3: { message: "MissingContext" },
4: { message: "SignerExpired" },
5: { message: "FailedSignerLimits" },
6: { message: "FailedPolicySignerLimits" },
7: { message: "SignatureKeyValueMismatch" },
8: { message: "ClientDataJsonChallengeIncorrect" },
9: { message: "JsonParseError" }
}
export type SignerExpiration = readonly [Option<u32>];
export type SignerLimits = readonly [Option<Map<string, Option<Array<SignerKey>>>>];
export type SignerStorage = { tag: "Persistent", values: void } | { tag: "Temporary", values: void };
export type Signer = { tag: "Policy", values: readonly [string, SignerExpiration, SignerLimits, SignerStorage] } | { tag: "Ed25519", values: readonly [Buffer, SignerExpiration, SignerLimits, SignerStorage] } | { tag: "Secp256r1", values: readonly [Buffer, Buffer, SignerExpiration, SignerLimits, SignerStorage] };
export type SignerKey = { tag: "Policy", values: readonly [string] } | { tag: "Ed25519", values: readonly [Buffer] } | { tag: "Secp256r1", values: readonly [Buffer] };
export type SignerVal = { tag: "Policy", values: readonly [SignerExpiration, SignerLimits] } | { tag: "Ed25519", values: readonly [SignerExpiration, SignerLimits] } | { tag: "Secp256r1", values: readonly [Buffer, SignerExpiration, SignerLimits] };
export interface Secp256r1Signature {
authenticator_data: Buffer;
client_data_json: Buffer;
signature: Buffer;
}
export type Signature = { tag: "Policy", values: void } | { tag: "Ed25519", values: readonly [Buffer] } | { tag: "Secp256r1", values: readonly [Secp256r1Signature] };
export type Signatures = readonly [Map<SignerKey, Signature>];
export interface Client {
/**
* Construct and simulate a add_signer transaction. Returns an `AssembledTransaction` object which will have a `result` field containing the result of the simulation. If this transaction changes contract state, you will need to call `signAndSend()` on the returned object.
*/
add_signer: ({ signer }: { signer: Signer }, options?: {
/**
* The fee to pay for the transaction. Default: BASE_FEE
*/
fee?: number;
/**
* The maximum amount of time to wait for the transaction to complete. Default: DEFAULT_TIMEOUT
*/
timeoutInSeconds?: number;
/**
* Whether to automatically simulate the transaction when constructing the AssembledTransaction. Default: true
*/
simulate?: boolean;
}) => Promise<AssembledTransaction<null>>
/**
* Construct and simulate a update_signer transaction. Returns an `AssembledTransaction` object which will have a `result` field containing the result of the simulation. If this transaction changes contract state, you will need to call `signAndSend()` on the returned object.
*/
update_signer: ({ signer }: { signer: Signer }, options?: {
/**
* The fee to pay for the transaction. Default: BASE_FEE
*/
fee?: number;
/**
* The maximum amount of time to wait for the transaction to complete. Default: DEFAULT_TIMEOUT
*/
timeoutInSeconds?: number;
/**
* Whether to automatically simulate the transaction when constructing the AssembledTransaction. Default: true
*/
simulate?: boolean;
}) => Promise<AssembledTransaction<null>>
/**
* Construct and simulate a remove_signer transaction. Returns an `AssembledTransaction` object which will have a `result` field containing the result of the simulation. If this transaction changes contract state, you will need to call `signAndSend()` on the returned object.
*/
remove_signer: ({ signer_key }: { signer_key: SignerKey }, options?: {
/**
* The fee to pay for the transaction. Default: BASE_FEE
*/
fee?: number;
/**
* The maximum amount of time to wait for the transaction to complete. Default: DEFAULT_TIMEOUT
*/
timeoutInSeconds?: number;
/**
* Whether to automatically simulate the transaction when constructing the AssembledTransaction. Default: true
*/
simulate?: boolean;
}) => Promise<AssembledTransaction<null>>
/**
* Construct and simulate a update_contract_code transaction. Returns an `AssembledTransaction` object which will have a `result` field containing the result of the simulation. If this transaction changes contract state, you will need to call `signAndSend()` on the returned object.
*/
update_contract_code: ({ hash }: { hash: Buffer }, options?: {
/**
* The fee to pay for the transaction. Default: BASE_FEE
*/
fee?: number;
/**
* The maximum amount of time to wait for the transaction to complete. Default: DEFAULT_TIMEOUT
*/
timeoutInSeconds?: number;
/**
* Whether to automatically simulate the transaction when constructing the AssembledTransaction. Default: true
*/
simulate?: boolean;
}) => Promise<AssembledTransaction<null>>
}
export class Client extends ContractClient {
static async deploy<T = Client>(
/** Constructor/Initialization Args for the contract's `__constructor` method */
{ signer }: { signer: Signer },
/** Options for initalizing a Client as well as for calling a method, with extras specific to deploying. */
options: MethodOptions &
Omit<ContractClientOptions, "contractId"> & {
/** The hash of the Wasm blob, which must already be installed on-chain. */
wasmHash: Buffer | string;
/** Salt used to generate the contract's ID. Passed through to {@link Operation.createCustomContract}. Default: random. */
salt?: Buffer | Uint8Array;
/** The format used to decode `wasmHash`, if it's provided as a string. */
format?: "hex" | "base64";
address?: string;
}
): Promise<AssembledTransaction<T>> {
return ContractClient.deploy({ signer }, options)
}
constructor(public readonly options: ContractClientOptions) {
super(
new ContractSpec(["AAAABAAAAAAAAAAAAAAABUVycm9yAAAAAAAACQAAAAAAAAAITm90Rm91bmQAAAABAAAAAAAAAA1BbHJlYWR5RXhpc3RzAAAAAAAAAgAAAAAAAAAOTWlzc2luZ0NvbnRleHQAAAAAAAMAAAAAAAAADVNpZ25lckV4cGlyZWQAAAAAAAAEAAAAAAAAABJGYWlsZWRTaWduZXJMaW1pdHMAAAAAAAUAAAAAAAAAGEZhaWxlZFBvbGljeVNpZ25lckxpbWl0cwAAAAYAAAAAAAAAGVNpZ25hdHVyZUtleVZhbHVlTWlzbWF0Y2gAAAAAAAAHAAAAAAAAACBDbGllbnREYXRhSnNvbkNoYWxsZW5nZUluY29ycmVjdAAAAAgAAAAAAAAADkpzb25QYXJzZUVycm9yAAAAAAAJ",
"AAAAAQAAAAAAAAAAAAAAEFNpZ25lckV4cGlyYXRpb24AAAABAAAAAAAAAAEwAAAAAAAD6AAAAAQ=",
"AAAAAQAAAAAAAAAAAAAADFNpZ25lckxpbWl0cwAAAAEAAAAAAAAAATAAAAAAAAPoAAAD7AAAABMAAAPoAAAD6gAAB9AAAAAJU2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IAAAAAAAAAAKc2lnbmF0dXJlcwAAAAAH0AAAAApTaWduYXR1cmVzAAAAAAAAAAAADWF1dGhfY29udGV4dHMAAAAAAAPqAAAH0AAAAAdDb250ZXh0AAAAAAEAAAPpAAAD7QAAAAAAAAAD"]),
options
)
}
public readonly fromJSON = {
add_signer: this.txFromJSON<null>,
update_signer: this.txFromJSON<null>,
remove_signer: this.txFromJSON<null>,
update_contract_code: this.txFromJSON<null>
}
}