Skip to content

Added address Verification #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
cf5561b
Added address verification check
Nov 24, 2021
8fe3bf5
Version bump
gavinharris-dev Nov 24, 2021
6173f04
Fixed some bugs in verify
Nov 24, 2021
053964d
Merge branch 'main' of github.com:gavinharris-dev/web3-cardano-token
Nov 24, 2021
072ae8f
Merge branch 'pyropy:main' into main
gavinharris-dev Jan 22, 2022
33303ca
Updated for Review Comments
gavinharris-dev Jan 22, 2022
b457038
CIP30 changes
Feb 22, 2022
d209c8c
Merge branch 'main' into feature/cip30
gavinharris-dev Feb 22, 2022
6e686e6
Merge pull request #1 from gavinharris-dev/feature/cip30
gavinharris-dev Feb 22, 2022
f78a184
Bumpped version
Feb 23, 2022
83282d4
Merge branch 'feature/cip30' of github.com:gavinharris-dev/web3-carda…
Feb 23, 2022
19bb462
Merge pull request #2 from gavinharris-dev/feature/cip30
gavinharris-dev Feb 23, 2022
da1dc9b
RewardAddress is not a standalone Object!
Mar 8, 2022
8d3f6be
Merge pull request #3 from gavinharris-dev/feature/cip30
gavinharris-dev Mar 8, 2022
ed9b2a1
Check this out.
Mar 15, 2022
271a125
Merge pull request #4 from gavinharris-dev/feature/cip30
gavinharris-dev Mar 15, 2022
2c9bb35
Fix issue with Verification. There are still lots to fix; if only I c…
Mar 15, 2022
a619850
Fix issue with Verification. There are still lots to fix; if only I c…
Mar 15, 2022
f6e276c
wsdfg
Aug 24, 2022
2cc72b2
sdfds
Aug 24, 2022
f39848c
sdfds
Aug 24, 2022
daebc6a
Validate Stake Address validation
gavinharris-dev Jan 11, 2023
3e68824
Update package.json
gavinharris-dev Jan 11, 2023
f613fcc
Validate Stake Address validation
gavinharris-dev Jan 11, 2023
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
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignore artifacts:
dist
1 change: 1 addition & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Binary file added dist/01d712ed3b6fe8f23933.module.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions dist/228.browser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/351.browser.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/405.browser.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/603.browser.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/66.browser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/891.browser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/997.browser.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions dist/browser.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions dist/browser.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*!
* The buffer module from node.js, for the browser.
*
* @author Feross Aboukhadijeh <https://feross.org>
* @license MIT
*/

/*! https://mths.be/base64 v1.0.0 by @mathias | MIT license */

/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
Binary file added dist/ef0ca63316357c3c262f.module.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/node.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "web3-cardano-token",
"version": "0.0.12",
"version": "0.2.1",
"private": false,
"description": "Web3 Token is a new way to authenticate users in hybrid dApps using signed messages.",
"author": "pyropy",
"scripts": {
"build": "webpack --mode=production --progress"
},
"types": "src/lib.d.ts",
"module": "src/lib.js",
"main": "dist/web3-cardano-token.js",
"dependencies": {
Expand Down
25 changes: 25 additions & 0 deletions src/browser.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
type COSESign1 = {
signature: string;
key: string;
}

type Signer = (msg: string) => PromiseLike<COSESign1>;

export function sign(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we stick with arrow function types? Also we should make first letter of the function uppercase ⏫

signer: Signer,
expires_in?: string | number,
body?: Object
): PromiseLike<string>;

export function verify(token: string): {
address: string;
body: Object;
};

declare const Web3Token: {
sign: typeof sign;
verify: typeof verify;
};

export default Web3Token;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Types ❤️

32 changes: 18 additions & 14 deletions src/lib.d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
type Signer = (msg: string) => PromiseLike<string>
type COSESign1 = {
signature: string;
key: string;
}

type Signer = (msg: string) => PromiseLike<COSESign1>;

export function sign(
signer: Signer,
expires_in?: string | number,
body?: Object): PromiseLike<string>
signer: Signer,
expires_in?: string | number,
body?: Object
): PromiseLike<string>;

export function verify(
token: string
): {
address: string,
body: Object
}
export function verify(token: string): {
address: string;
body: Object;
};

declare const Web3Token: {
sign: typeof sign,
verify: typeof verify
}
sign: typeof sign;
verify: typeof verify;
};

export default Web3Token
export default Web3Token;
73 changes: 41 additions & 32 deletions src/lib/sign.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,80 @@
import Base64 from 'base-64';
import { timeSpan } from './timespan';
import Base64 from "base-64";
import { timeSpan } from "./timespan";

/**
*
*
* @param {function} signer - The signer function, must return Promise<string>
* @param {any} body - Body to add to the sign
*/
export const sign = async (signer, expires_in = '1d', body = {}) => {

export const sign = async (signer, expires_in = "1d", body = {}) => {
const expires_in_date = timeSpan(expires_in);

validateInput(body);

const data = {
'Web3-Token-Version': 1,
'Expire-Date': expires_in_date,
"Web3-Token-Version": 1,
"Expire-Date": expires_in_date,
...body,
};

const msg = buildMessage(data);
if(typeof signer === 'function') {
var signature = await signer(msg);

if (typeof signer === "function") {
var COSESign1Message = await signer(msg);
} else {
throw new Error('"signer" argument should be a function that returns a signature eg: "msg => web3.eth.personal.sign(msg, <YOUR_ADDRESS>)"')
throw new Error(
'"signer" argument should be a function that returns a signature eg: "msg => web3.eth.personal.sign(msg, <YOUR_ADDRESS>)"'
);
}

if (typeof(signature) === "object") {
signature = signature.signature

let signature, key;

if (typeof COSESign1Message === "string") {
signature = COSESign1Message;
} else {
signature = COSESign1Message.signature;
key = COSESign1Message.key;
}

if(typeof signature !== 'string') {
throw new Error('"signer" argument should be a function that returns a signature string (Promise<string>)')
if (typeof signature !== "string") {
throw new Error(
'"signature" argument should be a function that returns a signature string (Promise<string>)'
);
}

const token = Base64.encode(JSON.stringify({
signature,
body: msg,
}))
const token = Base64.encode(
JSON.stringify({
signature,
key,
body: msg,
})
);

return token;
}

};

const validateInput = body => {
const validateInput = (body) => {
for (const key in body) {
const field = body[key];

const field = body[key]

if(key === 'Expire-Date') {
if (key === "Expire-Date") {
throw new Error('Please do not rewrite "Expire-Date" field');
}

if(key === 'Web3-Token-Version') {
if (key === "Web3-Token-Version") {
throw new Error('Please do not rewrite "Web3-Token-Version" field');
}

if(typeof field !== 'string') {
throw new Error('Body can only contain string values');
if (typeof field !== "string") {
throw new Error("Body can only contain string values");
}
}
};

const buildMessage = data => {
const buildMessage = (data) => {
const message = [];
for (const key in data) {
message.push(`${key}: ${data[key]}`)
message.push(`${key}: ${data[key]}`);
}
return message.join('\n');
return message.join("\n");
};
Loading