-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from starkbank/feature/transfer-brcodepayment-…
…rules Add Transfer.rules and BrcodePayment.rules sub-resources
- Loading branch information
Showing
20 changed files
with
252 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const rule = require('./rule.js'); | ||
|
||
exports.Rule = rule.Rule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const SubResource = require('../../utils/subResource').SubResource | ||
|
||
|
||
class Rule extends SubResource { | ||
/** | ||
* | ||
* BrcodePayment.Rule object | ||
* | ||
* @description The BrcodePayment.Rule object modifies the behavior of BrcodePayment objects when passed as an argument upon their creation. | ||
* | ||
* Parameters (required): | ||
* @param key [string]: Rule to be customized, describes what BrcodePayment behavior will be altered. ex: "resendingLimit" | ||
* @param value [integer]: Value of the rule. ex: 5 | ||
* | ||
*/ | ||
constructor({key, value}) { | ||
super(); | ||
this.key = key; | ||
this.value = value; | ||
} | ||
} | ||
|
||
exports.Rule = Rule; | ||
exports.subResource = {'class': exports.Rule, 'name': 'Rule'}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const rule = require('./rule.js'); | ||
|
||
exports.Rule = rule.Rule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const SubResource = require('../../utils/subResource').SubResource | ||
|
||
|
||
class Rule extends SubResource { | ||
/** | ||
* | ||
* Transfer.Rule object | ||
* | ||
* @description The Transfer.Rule object modifies the behavior of Transfer objects when passed as an argument upon their creation. | ||
* | ||
* Parameters (required): | ||
* @param key [string]: Rule to be customized, describes what Transfer behavior will be altered. ex: "resendingLimit" | ||
* @param value [integer]: Value of the rule. ex: 5 | ||
* | ||
*/ | ||
constructor({key, value}) { | ||
super(); | ||
this.key = key; | ||
this.value = value; | ||
} | ||
} | ||
|
||
exports.Rule = Rule; | ||
exports.subResource = {'class': exports.Rule, 'name': 'Rule'}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const starkbank = require('../../index.js'); | ||
const Ellipticcurve = require('starkbank-ecdsa'); | ||
const rest = require('../utils/rest.js'); | ||
const error = require('../error.js'); | ||
const api = require('./api'); | ||
|
||
|
||
exports.parseObjects = function (objects, resource, resourceClass) { | ||
if (objects == null) | ||
return null; | ||
|
||
let parsedObjects = []; | ||
for (let object of objects) { | ||
if (object instanceof resourceClass) { | ||
parsedObjects.push(object); | ||
continue; | ||
} | ||
object = Object.assign(new resource['class'](object), object); | ||
parsedObjects.push(object); | ||
} | ||
return parsedObjects; | ||
} | ||
|
||
exports.parseAndVerify = async function (resource, content, signature, user = null) { | ||
content = await this.verify(content, signature, user); | ||
|
||
let object = Object.assign(new resource['class'](api.lastName(resource['name'])), JSON.parse(content)) | ||
if (resource['name'] === 'Event'){ | ||
object = Object.assign(new resource['class'](), JSON.parse(content)['event']); | ||
} | ||
|
||
return object; | ||
} | ||
|
||
exports.verify = async function (content, signature, user = null) { | ||
try { | ||
signature = Ellipticcurve.Signature.fromBase64(signature); | ||
} catch (e) { | ||
throw new error.InvalidSignatureError('The provided signature is not valid'); | ||
} | ||
|
||
if (await verifySignature(content, signature, user)) { | ||
return content; | ||
} | ||
if (await verifySignature(content, signature, user, true)) { | ||
return content; | ||
} | ||
throw new error.InvalidSignatureError('Provided signature and content do not match Stark public key'); | ||
} | ||
|
||
async function verifySignature(content, signature, user = null, refresh = false) { | ||
let publicKey = starkbank.cache['stark-public-key']; | ||
if (!publicKey || refresh) { | ||
let pem = await rest.getPublicKey(user); | ||
publicKey = Ellipticcurve.PublicKey.fromPem(pem); | ||
starkbank.cache['stark-public-key'] = publicKey; | ||
} | ||
return Ellipticcurve.Ecdsa.verify(content, signature, publicKey); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.