Skip to content

Commit

Permalink
Support string key types in memo api. #105
Browse files Browse the repository at this point in the history
  • Loading branch information
James Calfee committed Mar 15, 2017
1 parent 8425c62 commit c759543
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/auth/memo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import ByteBuffer from 'bytebuffer'
import assert from 'assert'
import base58 from 'bs58'
import {Aes} from './ecc'
import {Aes, PrivateKey, PublicKey} from './ecc'
import {ops} from './serializer'

const encMemo = ops.encrypted_memo
Expand All @@ -21,6 +21,8 @@ export function decode(private_key, memo) {

assert(private_key, 'private_key is required')

private_key = toPrivateObj(private_key)

memo = base58.decode(memo)
memo = encMemo.fromBuffer(new Buffer(memo, 'binary'))

Expand Down Expand Up @@ -59,6 +61,9 @@ export function encode(private_key, public_key, memo, testNonce) {
assert(private_key, 'private_key is required')
assert(public_key, 'public_key is required')

private_key = toPrivateObj(private_key)
public_key = toPublicObj(public_key)

const mbuf = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN)
mbuf.writeVString(memo)
memo = new Buffer(mbuf.copy(0, mbuf.offset).toBinary(), 'binary')
Expand All @@ -75,3 +80,6 @@ export function encode(private_key, public_key, memo, testNonce) {
memo = encMemo.toBuffer(memo)
return '#' + base58.encode(new Buffer(memo, 'binary'))
}

const toPrivateObj = o => (o ? o.d ? o : PrivateKey.fromWif(o) : o/*null or undefined*/)
const toPublicObj = o => (o ? o.Q ? o : PublicKey.fromString(o) : o/*null or undefined*/)
7 changes: 6 additions & 1 deletion test/memo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ const private_key = PrivateKey.fromSeed("")
const public_key = private_key.toPublicKey()

describe('memo', ()=> {
it('encryption', () => {
it('encryption obj params', () => {
const cypertext = encode(private_key, public_key, '#memo')
const plaintext = decode(private_key, cypertext)
assert.equal(plaintext, 'memo')
})
it('encryption string params', () => {
const cypertext = encode(private_key.toWif(), public_key.toPublicKeyString(), '#memo2')
const plaintext = decode(private_key.toWif(), cypertext)
assert.equal(plaintext, 'memo2')
})
it('known encryption', () => {
const base58 = '#HU6pdQ4Hh8cFrDVooekRPVZu4BdrhAe9RxrWrei2CwfAApAPdM4PT5mSV9cV3tTuWKotYQF6suyM4JHFBZz4pcwyezPzuZ2na7uwhRcLqFoqCam1VU3eCLjVNqcgUNbH3'
const nonce = '1462976530069648'
Expand Down

0 comments on commit c759543

Please sign in to comment.