Skip to content

Commit

Permalink
Unit test case for custom public key prefix #32
Browse files Browse the repository at this point in the history
  • Loading branch information
james committed Aug 22, 2018
1 parent 5818b8f commit 904225d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/api_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ ecc.randomKey().then(privateKey => {
@example ecc.privateToPublic(wif) === pubkey
*/
privateToPublic: (wif, pubkey_prefix = 'EOS') => PrivateKey(wif).toPublic().toString(pubkey_prefix),
privateToPublic: (wif, pubkey_prefix = 'EOS') =>
PrivateKey(wif).toPublic().toString(pubkey_prefix),

/**
@arg {pubkey} pubkey - like EOSKey..
Expand All @@ -81,7 +82,8 @@ ecc.randomKey().then(privateKey => {
@example ecc.isValidPublic(pubkey) === true
*/
isValidPublic: (pubkey, pubkey_prefix = 'EOS') => PublicKey.isValid(pubkey,pubkey_prefix),
isValidPublic: (pubkey, pubkey_prefix = 'EOS') =>
PublicKey.isValid(pubkey, pubkey_prefix),

/**
@arg {wif} wif
Expand Down
7 changes: 5 additions & 2 deletions src/common.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ describe('Common API', () => {
[true, 'PUB_K1_859gxfnXyUriMgUeThh1fWv3oqcpLFyHa3TfFYC4PK2Ht7beeX'],
[true, 'EOS859gxfnXyUriMgUeThh1fWv3oqcpLFyHa3TfFYC4PK2HqhToVM'],
[false, 'MMM859gxfnXyUriMgUeThh1fWv3oqcpLFyHa3TfFYC4PK2HqhToVM'],
[false, 'EOS859gxfnXyUriMgUeThh1fWv3oqcpLFyHa3TfFYC4PK2HqhToVm'],
[false, 'EOS859gxfnXyUriMgUeThh1fWv3oqcpLFyHa3TfFYC4PK2HqhToVm', 'EOS'],
[true, 'PUB859gxfnXyUriMgUeThh1fWv3oqcpLFyHa3TfFYC4PK2HqhToVM', 'PUB'],
[false, 'PUB859gxfnXyUriMgUeThh1fWv3oqcpLFyHa3TfFYC4PK2HqhToVm', 'PUB'],
]
for(const key of keys) {
assert.equal(key[0], ecc.isValidPublic(key[1]), key[1])
const [valid, pubkey, prefix] = key
assert.equal(valid, ecc.isValidPublic(pubkey, prefix), pubkey)
}
})

Expand Down
7 changes: 3 additions & 4 deletions src/key_public.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ function PublicKey(Q, pubkey_prefix = 'EOS') {
}

/**
@param {string|Buffer|PublicKey|ecurve.Point} public key
@param {string|Buffer|PublicKey|ecurve.Point} pubkey - public key
@param {string} [pubkey_prefix = 'EOS']
*/
PublicKey.isValid = function(text, pubkey_prefix = 'EOS') {
PublicKey.isValid = function(pubkey, pubkey_prefix = 'EOS') {
try {
PublicKey(text,pubkey_prefix)
PublicKey(pubkey, pubkey_prefix)
return true
} catch(e) {
return false
Expand Down Expand Up @@ -151,7 +151,6 @@ PublicKey.fromStringOrThrow = function(public_key, pubkey_prefix = 'EOS') {
const match = public_key.match(/^PUB_([A-Za-z0-9]+)_([A-Za-z0-9]+)$/)
if(match === null) {
// legacy
// TELOS addition: support for variable public_key prefixes
var prefix_match = new RegExp("^" + pubkey_prefix);
if(prefix_match.test(public_key)) {
public_key = public_key.substring(pubkey_prefix.length)
Expand Down

0 comments on commit 904225d

Please sign in to comment.