Skip to content
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

Fix/paymail signatures tests passing #11

Merged
merged 6 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
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
38 changes: 22 additions & 16 deletions docs/paymailClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default class PaymailClient {
const schema = Joi.object({
name: Joi.string().required(),
avatar: Joi.string().uri().required()
});
}).options({ stripUnknown: true });
const { error, value } = schema.validate(response);
if (error) {
throw new PaymailServerResponseError(`Validation error: ${error.message}`);
Expand All @@ -108,7 +108,7 @@ export default class PaymailClient {
bsvalias: Joi.string().optional().allow("1.0"),
handle: Joi.string().required(),
pubkey: Joi.string().required()
});
}).options({ stripUnknown: true });
const { error, value } = schema.validate(response);
if (error) {
throw new PaymailServerResponseError(`Validation error: ${error.message}`);
Expand All @@ -125,7 +125,7 @@ export default class PaymailClient {
satoshis: Joi.number().required()
}).required().min(1)),
reference: Joi.string().required()
});
}).options({ stripUnknown: true });
const { error } = schema.validate(response);
if (error) {
throw new PaymailServerResponseError(`Validation error: ${error.message}`);
Expand All @@ -149,15 +149,18 @@ export default class PaymailClient {
const schema = Joi.object({
txid: Joi.string().required(),
note: Joi.string().optional().allow("")
});
}).options({ stripUnknown: true });
const { error, value } = schema.validate(response);
if (error) {
throw new PaymailServerResponseError(`Validation error: ${error.message}`);
}
return value;
};
public createP2PSignature = (txid: string, privKey: PrivateKey): string => {
return privKey.sign(txid).toString("hex") as string;
public createP2PSignature = (msg: string, privKey: PrivateKey): string => {
const msgHash = new BigNumber(sha256(msg, "hex"), 16);
const sig = ECDSA.sign(msgHash, privKey, true);
const recovery = sig.CalculateRecoveryFactor(privKey.toPublicKey(), msgHash);
return sig.toCompact(recovery, true, "base64") as string;
};
public verifyPublicKey = async (paymail, pubkey) => {
const [name, domain] = paymail.split("@");
Expand All @@ -170,7 +173,7 @@ export default class PaymailClient {
handle: Joi.string().required(),
pubkey: Joi.string().required(),
match: Joi.boolean().required()
});
}).options({ stripUnknown: true });
const { error } = schema.validate(responseBody);
if (error) {
throw new PaymailServerResponseError(`Validation error: ${error.message}`);
Expand All @@ -191,7 +194,7 @@ export default class PaymailClient {
const schema = Joi.object({
txid: Joi.string().required(),
note: Joi.string().optional().allow("")
});
}).options({ stripUnknown: true });
const { error, value } = schema.validate(response);
if (error) {
throw new PaymailServerResponseError(`Validation error: ${error.message}`);
Expand Down Expand Up @@ -227,8 +230,11 @@ Argument Details
Creates a digital signature for a P2P transaction using a given private key.

```ts
public createP2PSignature = (txid: string, privKey: PrivateKey): string => {
return privKey.sign(txid).toString("hex") as string;
public createP2PSignature = (msg: string, privKey: PrivateKey): string => {
const msgHash = new BigNumber(sha256(msg, "hex"), 16);
const sig = ECDSA.sign(msgHash, privKey, true);
const recovery = sig.CalculateRecoveryFactor(privKey.toPublicKey(), msgHash);
return sig.toCompact(recovery, true, "base64") as string;
}
```

Expand Down Expand Up @@ -261,7 +267,7 @@ public getP2pPaymentDestination = async (paymail: string, satoshis: number): Pro
satoshis: Joi.number().required()
}).required().min(1)),
reference: Joi.string().required()
});
}).options({ stripUnknown: true });
const { error } = schema.validate(response);
if (error) {
throw new PaymailServerResponseError(`Validation error: ${error.message}`);
Expand All @@ -284,7 +290,7 @@ public getPki = async (paymail) => {
bsvalias: Joi.string().optional().allow("1.0"),
handle: Joi.string().required(),
pubkey: Joi.string().required()
});
}).options({ stripUnknown: true });
const { error, value } = schema.validate(response);
if (error) {
throw new PaymailServerResponseError(`Validation error: ${error.message}`);
Expand All @@ -303,7 +309,7 @@ public getPublicProfile = async (paymail) => {
const schema = Joi.object({
name: Joi.string().required(),
avatar: Joi.string().uri().required()
});
}).options({ stripUnknown: true });
const { error, value } = schema.validate(response);
if (error) {
throw new PaymailServerResponseError(`Validation error: ${error.message}`);
Expand Down Expand Up @@ -349,7 +355,7 @@ public sendBeefTransactionP2P = async (paymail: string, beef: string, reference:
const schema = Joi.object({
txid: Joi.string().required(),
note: Joi.string().optional().allow("")
});
}).options({ stripUnknown: true });
const { error, value } = schema.validate(response);
if (error) {
throw new PaymailServerResponseError(`Validation error: ${error.message}`);
Expand Down Expand Up @@ -378,7 +384,7 @@ public sendTransactionP2P = async (paymail: string, hex: string, reference: stri
const schema = Joi.object({
txid: Joi.string().required(),
note: Joi.string().optional().allow("")
});
}).options({ stripUnknown: true });
const { error, value } = schema.validate(response);
if (error) {
throw new PaymailServerResponseError(`Validation error: ${error.message}`);
Expand All @@ -403,7 +409,7 @@ public verifyPublicKey = async (paymail, pubkey) => {
handle: Joi.string().required(),
pubkey: Joi.string().required(),
match: Joi.boolean().required()
});
}).options({ stripUnknown: true });
const { error } = schema.validate(responseBody);
if (error) {
throw new PaymailServerResponseError(`Validation error: ${error.message}`);
Expand Down
8 changes: 7 additions & 1 deletion docs/paymailRouter.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export default class PaymailRoute {
public getCode(): string
public getEndpoint(): string
public getMethod(): "GET" | "POST"
static getNameAndDomain(params: any): {
name: string;
domain: string;
pubkey?: string;
}
}
```

Expand All @@ -40,6 +45,7 @@ It sets up the necessary routes and handlers based on the given configuration.
```ts
export default class PaymailRouter {
public baseUrl: string;
public basePath: string;
public routes: PaymailRoute[];
public requestSenderValidation: boolean;
constructor(config: PaymailRouterConfig)
Expand Down Expand Up @@ -86,7 +92,7 @@ Links: [API](#api), [Classes](#classes), [Types](#types)
## Type: DomainLogicHandler

```ts
export type DomainLogicHandler = (name: string, domain: string, body?: any) => Promise<any>
export type DomainLogicHandler = (name: string, domain: string, body?: any, pubkey?: string) => Promise<any>
```

Links: [API](#api), [Classes](#classes), [Types](#types)
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@
"test": "npm run build && jest --testTimeout=15000",
"test:watch": "npm run build && jest --watch",
"test:coverage": "npm run build && jest --coverage",
"lint": "ts-standard --fix src/**/*.ts",
"ex-lint": "ts-standard --fix examples/**/*.ts",
"docs-lint": "ts-standard --fix docs/**/*.ts",
"lint": "ts-standard --fix 'src/**/*.ts'",
"ex-lint": "ts-standard --fix 'docs/examples/**/*.ts'",
"docs-lint": "ts-standard --fix 'docs/**/*.ts'",
"build": "tsc -b && tsconfig-to-dual-package tsconfig.cjs.json",
"dev": "tsc -b -w",
"prepublish": "npm run build",
Expand Down
7 changes: 2 additions & 5 deletions src/paymailClient/paymailClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,8 @@ export default class PaymailClient {
public createP2PSignature = (msg: string, privKey: PrivateKey): string => {
const msgHash = new BigNumber(sha256(msg, 'hex'), 16)
const sig = ECDSA.sign(msgHash, privKey, true)

return 'mock signature';
// FIXME - This is a temporary workaround until the SDK is fixed.
// const recovery = sig.CalculateRecoveryFactor(privKey.toPublicKey(), msgHash)
// return sig.toCompact(recovery, true, 'base64') as string
const recovery = sig.CalculateRecoveryFactor(privKey.toPublicKey(), msgHash)
return sig.toCompact(recovery, true, 'base64') as string
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/paymailRouter/__tests/p2p-payment-destinations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ describe('#Paymail Server - P2P Payment Destinations', () => {
let client: PaymailClient

beforeAll(() => {
app = express();
const baseUrl = 'http://localhost:3000';
client = new PaymailClient(); // Assuming the client is needed for route config
app = express()
const baseUrl = 'http://localhost:3000'
client = new PaymailClient(null, { dns: null }, null)
const routes = [
new P2pPaymentDestinationRoute({
domainLogicHandler: (_, body) => {
Expand All @@ -23,13 +23,13 @@ describe('#Paymail Server - P2P Payment Destinations', () => {
}
],
reference: 'someref'
};
}
}
})
];
const paymailRouter = new PaymailRouter({ baseUrl, routes });
app.use(paymailRouter.getRouter());
});
]
const paymailRouter = new PaymailRouter({ baseUrl, routes })
app.use(paymailRouter.getRouter())
})

it('should get public profile for user paymail', async () => {
const response = await request(app).post('/p2p-payment-destination/satoshi@bsv.org').send({
Expand Down
21 changes: 10 additions & 11 deletions src/paymailRouter/__tests/pki.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,24 @@ describe('#Paymail Server - PKI', () => {
const userIdentityKey = PrivateKey.fromRandom()

beforeAll(() => {
app = express();
const baseUrl = 'http://localhost:3000';
app = express()
const baseUrl = 'http://localhost:3000'

const routes = [
new PublicKeyInfrastructureRoute({
domainLogicHandler: (params) => {
const { name, domain } = PublicKeyInfrastructureRoute.getNameAndDomain(params);
const { name, domain } = PublicKeyInfrastructureRoute.getNameAndDomain(params)
return {
handle: `${name}@${domain}`,
pubkey: userIdentityKey.toPublicKey().toString()
};
}
}
})
];

const paymailRouter = new PaymailRouter({ baseUrl, routes });
app.use(paymailRouter.getRouter());
});

]

const paymailRouter = new PaymailRouter({ baseUrl, routes })
app.use(paymailRouter.getRouter())
})

it('should get identity key for user', async () => {
const response = await request(app).get('/id/satoshi@bsv.org').send()
Expand Down
25 changes: 12 additions & 13 deletions src/paymailRouter/__tests/public-profile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,25 @@ describe('#Paymail Server - Get Public Profile', () => {
let app

beforeAll(() => {
app = express();
const baseUrl = 'http://localhost:3000';
app = express()
const baseUrl = 'http://localhost:3000'

const routes = [
new PublicProfileRoute({
domainLogicHandler: (params) => {
const { name, domain } = PublicProfileRoute.getNameAndDomain(params);
const { name, domain } = PublicProfileRoute.getNameAndDomain(params)
return {
name: name,
domain: domain,
name,
domain,
avatar: `https://avatar.com/${name}@${domain}`
};
}
}
})
];

const paymailRouter = new PaymailRouter({ baseUrl, routes });
app.use(paymailRouter.getRouter());
});

]

const paymailRouter = new PaymailRouter({ baseUrl, routes })
app.use(paymailRouter.getRouter())
})

it('should get public profile for user paymail', async () => {
const response = await request(app).get('/public-profile/satoshi@bsv.org')
Expand Down
Loading
Loading