@steeveproject/ngx-steem-keychain
Version:
Your Angular interface to Steem Keychain
369 lines (358 loc) • 9.81 kB
JavaScript
/**
* @license ngx-steem-keychain
* MIT license
*/
import { Injectable, NgZone, NgModule, defineInjectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
class SteemKeychainError extends Error {
/**
* @param {?} response
*/
constructor(response) {
super(response.message);
this.response = response;
Object.setPrototypeOf(this, SteemKeychainError.prototype);
}
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
class SteemKeychainService {
/**
* @param {?} ngZone
*/
constructor(ngZone) {
this.ngZone = ngZone;
}
/**
* @return {?}
*/
get steemKeychainAvailable() {
return ((/** @type {?} */ (window))).steem_keychain !== undefined;
}
/**
* @return {?}
*/
requestHandshake() {
return this.call('requestHandshake', []);
}
/**
* @param {?} account
* @param {?} encryptedMessage
* @param {?} keyType
* @return {?}
*/
requestVerifyKey(account, encryptedMessage, keyType) {
return this.call('requestVerifyKey', [
account,
encryptedMessage,
keyType
]);
}
/**
* @param {?} account
* @param {?} message
* @param {?} keyType
* @return {?}
*/
requestSignBuffer(account, message, keyType) {
return this.call('requestSignBuffer', [
account,
message,
keyType
]);
}
/**
* @param {?} account
* @param {?} operations
* @param {?} keyType
* @return {?}
*/
requestBroadcast(account, operations, keyType) {
return this.call('requestBroadcast', [
account,
operations.map(op => op.toArray()),
keyType
]);
}
/**
* @param {?} account
* @param {?} method
* @param {?} params
* @param {?} keyType
* @return {?}
*/
requestSignedCall(account, method, params, keyType) {
return this.call('requestSignedCall', [
account,
method,
params,
keyType
]);
}
/**
* @param {?} parentAuthor
* @param {?} parentPermlink
* @param {?} author
* @param {?} permlink
* @param {?} title
* @param {?} body
* @param {?=} jsonMetadata
* @param {?=} options
* @return {?}
*/
requestPost(parentAuthor, parentPermlink, author, permlink, title, body, jsonMetadata = '', options = '') {
return this.call('requestPost', [
author,
title,
body,
parentPermlink,
parentAuthor,
jsonMetadata,
permlink,
options
]);
}
/**
* @param {?} voter
* @param {?} author
* @param {?} permlink
* @param {?} weight
* @return {?}
*/
requestVote(voter, author, permlink, weight) {
return this.call('requestVote', [
voter,
permlink,
author,
weight
]);
}
/**
* @param {?} account
* @param {?} displayMessage
* @param {?} id
* @param {?} customJson
* @param {?=} key
* @return {?}
*/
requestCustomJson(account, displayMessage, id, customJson, key = 'Posting') {
return this.call('requestCustomJson', [
account,
id,
((/** @type {?} */ (key))).toLowerCase(),
customJson,
displayMessage
]);
}
/**
* @param {?} from
* @param {?} to
* @param {?} amount
* @param {?} memo
* @param {?} currency
* @param {?=} enforce
* @return {?}
*/
requestTransfer(from, to, amount, memo, currency, enforce = false) {
return this.call('requestTransfer', [
from,
to,
amount,
memo,
currency,
enforce,
], 4);
}
/**
* @param {?} delegator
* @param {?} delegatee
* @param {?} amount
* @param {?} unit
* @return {?}
*/
requestDelegation(delegator, delegatee, amount, unit) {
return this.call('requestDelegation', [
delegator,
delegatee,
amount,
unit
]);
}
/*
* call is invoked by all other methods to call Steem Keychain.
* It should not be used directly since it is not type-safe,
* but it can be bandy in case there is a method not implemented yet.
*/
/**
* @param {?} method
* @param {?} args
* @param {?=} callbackIndex
* @return {?}
*/
call(method, args, callbackIndex) {
return Observable.create((observer) => {
// Make sure the extension is available.
if (!this.steemKeychainAvailable) {
observer.error(new Error('Steem Keychain not available'));
observer.complete();
return;
}
// Push the callback to the argument list.
/** @type {?} */
const cb = (res) => this.ngZone.run(() => {
if (res.success) {
observer.next(res);
observer.complete();
}
else {
observer.error(new SteemKeychainError(res));
}
});
if (callbackIndex) {
args.splice(callbackIndex, 0, cb);
}
else {
args.push(cb);
}
// Send the request.
((/** @type {?} */ (window))).steem_keychain[method](...args);
});
}
}
SteemKeychainService.decorators = [
{ type: Injectable, args: [{
providedIn: 'root'
},] }
];
/** @nocollapse */
SteemKeychainService.ctorParameters = () => [
{ type: NgZone }
];
/** @nocollapse */ SteemKeychainService.ngInjectableDef = defineInjectable({ factory: function SteemKeychainService_Factory() { return new SteemKeychainService(inject(NgZone)); }, token: SteemKeychainService, providedIn: "root" });
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
// Consider registering providers using a forRoot() method
// when the module exports components, directives or pipes that require sharing the same providers instances.
// Consider registering providers also using a forChild() method
// when they requires new providers instances or different providers in child modules.
class SteemKeychainModule {
/**
* Use in AppModule: new instance of SteemKeychainService.
* @return {?}
*/
static forRoot() {
return {
ngModule: SteemKeychainModule,
providers: [SteemKeychainService]
};
}
/**
* Use in features modules with lazy loading: new instance of SteemKeychainService.
* @return {?}
*/
static forChild() {
return {
ngModule: SteemKeychainModule,
providers: [SteemKeychainService]
};
}
}
SteemKeychainModule.decorators = [
{ type: NgModule }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
class InvalidArgumentsError extends Error {
/**
* @param {?} message
*/
constructor(message) {
super(message);
this.message = message;
Object.setPrototypeOf(this, InvalidArgumentsError.prototype);
}
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/*
* Operation represents a blockchain operation,
* which has a name and some parameters.
*/
class Operation {
/**
* @param {?} name
* @param {?} params
*/
constructor(name, params) {
this.name = name;
this.params = params;
}
/*
* fromArray can be used to convert [operationName, operationParams] into an Operation.
*/
/**
* @param {?} operation
* @return {?}
*/
static fromArray(operation) {
if (!operation) {
throw new InvalidArgumentsError('operation array is unset');
}
if (operation.length !== 2) {
throw new InvalidArgumentsError('operation array length invalid (must be 2)');
}
const [name, params] = operation;
if (typeof name !== 'string') {
throw new InvalidArgumentsError('operation[0] (operation name) must be a string');
}
if (typeof params !== 'object') {
throw new InvalidArgumentsError('operation[1] (operation params) must be an object');
}
return new Operation(name, params);
}
/*
* toArray turns [operationName, operationParams].
*/
/**
* @return {?}
*/
toArray() {
return [this.name, this.params];
}
/*
* toJSON implements custom JSON serialization.
* It simply calls toArray() and returns the result.
*/
/**
* @return {?}
*/
toJSON() {
return this.toArray();
}
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
export { SteemKeychainModule, SteemKeychainError, InvalidArgumentsError, Operation, SteemKeychainService };
//# sourceMappingURL=ngx-steem-keychain.js.map