Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import {SignedJsonRpcRequest, validate as validateSignature, VerifyMessage} from '@steemit/rpc-auth'
import * as assert from 'assert'
import {Client, ClientOptions, PublicKey, Signature} from 'dsteem'
import {Authority, Client, ClientOptions, PublicKey, Signature} from 'dsteem'
import {RequestOptions} from 'https'
import {parse as parseUrl} from 'url'

Expand All @@ -25,6 +25,8 @@ export type JsonRpcAuthMethod = (this: JsonRpcAuthMethodContext, ...params) => a
export class JsonRpcAuth extends JsonRpc {

private client: Client
private cacheExpiry = 2 * 60 * 1000 // ms
private authCache = new Map<string, {auth: Authority, timestamp: number}>()

/**
* @param rpcNode Address to steemd node used for signature verification.
Expand Down Expand Up @@ -66,19 +68,33 @@ export class JsonRpcAuth extends JsonRpc {
assert.equal(message.byteLength, 32, 'Invalid message')
assert(accountName.length >= 3 && accountName.length <= 16, 'Invalid account name')

const [account] = await this.client.database.getAccounts([accountName])
let postingAuth: Authority

if (!account) {
throw new Error('No such account')
const now = Date.now()
const cached = this.authCache.get(accountName)
if (cached && cached.timestamp + this.cacheExpiry > now) {
postingAuth = cached.auth
} else {
const [account] = await this.client.database.getAccounts([accountName])

if (!account) {
throw new Error('No such account')
}

postingAuth = account.posting

this.authCache.set(accountName, {
timestamp: now, auth: postingAuth
})
}

if (account.posting.key_auths.length !== 1) {
if (postingAuth.key_auths.length !== 1) {
throw new Error('Unsupported posting key configuration for account')
}

const [keyWif, keyWeight] = account.posting.key_auths[0]
const [keyWif, keyWeight] = postingAuth.key_auths[0]

if (account.posting.weight_threshold > keyWeight) {
if (postingAuth.weight_threshold > keyWeight) {
throw new Error('Signing key not above weight threshold')
}

Expand Down