Skip to content

Commit

Permalink
Merge branch 'master' into nearcore-1-37-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gtsonevv authored Mar 12, 2024
2 parents dca4592 + 0c4e41c commit d9b0657
Show file tree
Hide file tree
Showing 20 changed files with 107 additions and 86 deletions.
8 changes: 8 additions & 0 deletions packages/accounts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @near-js/accounts

## 1.0.4

### Patch Changes

- Updated dependencies [[`42dc7e2a`](https://github.com/near/near-api-js/commit/42dc7e2ac794e973987bed7b89da5ef2d3c6c8ac)]:
- @near-js/transactions@1.1.2
- @near-js/providers@0.1.1

## 1.0.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/accounts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@near-js/accounts",
"version": "1.0.3",
"version": "1.0.4",
"description": "Classes encapsulating account-specific functionality",
"main": "lib/index.js",
"scripts": {
Expand Down
6 changes: 6 additions & 0 deletions packages/biometric-ed25519/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @near-js/biometric-ed25519

## 1.1.2

### Patch Changes

- [#1309](https://github.com/near/near-api-js/pull/1309) [`4b5aae44`](https://github.com/near/near-api-js/commit/4b5aae441fc3c10a5a3bf4d559ed2867c5e9abf1) Thanks [@gtsonevv](https://github.com/gtsonevv)! - Fix recoverPublicKey(), createKey() and getKeys() methods.

## 1.1.1

### Patch Changes
Expand Down
7 changes: 3 additions & 4 deletions packages/biometric-ed25519/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@near-js/biometric-ed25519",
"description": "JavaScript library to handle webauthn and biometric keys",
"version": "1.1.1",
"version": "1.1.2",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
Expand All @@ -13,19 +13,18 @@
"author": "Pagoda",
"license": "ISC",
"dependencies": {
"@aws-crypto/sha256-js": "4.0.0",
"@hexagon/base64": "1.1.26",
"@near-js/crypto": "workspace:*",
"@near-js/utils": "workspace:*",
"@noble/curves": "1.2.0",
"@noble/hashes": "1.3.3",
"asn1-parser": "1.1.8",
"bn.js": "5.2.1",
"borsh": "1.0.0",
"buffer": "6.0.3",
"@noble/curves": "1.2.0",
"fido2-lib": "3.4.1"
},
"devDependencies": {
"@aws-sdk/types": "3.329.0",
"@types/node": "18.11.18"
}
}
36 changes: 15 additions & 21 deletions packages/biometric-ed25519/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import base64 from '@hexagon/base64';
import { ed25519 } from '@noble/curves/ed25519';
import { Sha256 } from '@aws-crypto/sha256-js';
import { sha256 } from '@noble/hashes/sha256';
import { Buffer } from 'buffer';
import asn1 from 'asn1-parser';
import { KeyPair } from '@near-js/crypto';
Expand All @@ -11,7 +11,8 @@ import {
get64BytePublicKeyFromPEM,
preformatGetAssertReq,
publicKeyCredentialToJSON,
recoverPublicKey
recoverPublicKey,
uint8ArrayToBigInt
} from './utils';
import { Fido2 } from './fido2';
import { AssertionResponse } from './index.d';
Expand Down Expand Up @@ -69,11 +70,9 @@ export const createKey = async (username: string): Promise<KeyPair> => {
});
const publicKey = result.authnrData.get('credentialPublicKeyPem');
const publicKeyBytes = get64BytePublicKeyFromPEM(publicKey);
const edSha256 = new Sha256();
edSha256.update(Buffer.from(publicKeyBytes));
const secretKey = await edSha256.digest();
const secretKey = sha256.create().update(Buffer.from(publicKeyBytes)).digest();
const pubKey = ed25519.getPublicKey(secretKey);
return KeyPair.fromString(baseEncode(new Uint8Array(Buffer.concat([secretKey, Buffer.from(pubKey)]))));
return KeyPair.fromString(baseEncode(new Uint8Array(Buffer.concat([Buffer.from(secretKey), Buffer.from(pubKey)]))));
});
};

Expand Down Expand Up @@ -101,27 +100,22 @@ export const getKeys = async (username: string): Promise<[KeyPair, KeyPair]> =>
//@ts-ignore
const parser = asn1?.ASN1?.parse || window?.ASN1?.parse;
const rAndS = parser(new Uint8Array(signature));
const clientDataSha256 = new Sha256();
clientDataSha256.update(
const clientDataJSONHash = sha256.create().update(
Buffer.from(new Uint8Array(base64.toArrayBuffer(getAssertionResponse.response.clientDataJSON, true)))
);
const clientDataJSONHash = await clientDataSha256.digest();
).digest();
const authenticatorDataJSONHash = Buffer.from(new Uint8Array(base64.toArrayBuffer(getAssertionResponse.response.authenticatorData, true)));
const authenticatorAndClientDataJSONHash = Buffer.concat([authenticatorDataJSONHash, clientDataJSONHash]);
const authenticatorAndClientDataJSONHash = Buffer.concat([Buffer.from(authenticatorDataJSONHash), Buffer.from(clientDataJSONHash)]);

const correctPKs = await recoverPublicKey(rAndS.children[0].value, rAndS.children[1].value, authenticatorAndClientDataJSONHash, 0);

const firstEdSha256 = new Sha256();
firstEdSha256.update(Buffer.from(correctPKs[0]));
const secondEdSha256 = new Sha256();
secondEdSha256.update(Buffer.from(correctPKs[1]));
const r = rAndS.children[0].value;
const s = rAndS.children[1].value;
const correctPKs = await recoverPublicKey(uint8ArrayToBigInt(r), uint8ArrayToBigInt(s), authenticatorAndClientDataJSONHash, 0);

const firstEDSecret = await firstEdSha256.digest();
const firstEDSecret = sha256.create().update(Buffer.from(correctPKs[0])).digest();
const firstEDPublic = ed25519.getPublicKey(firstEDSecret);
const secondEDSecret = await secondEdSha256.digest();
const secondEDSecret = sha256.create().update(Buffer.from(correctPKs[1])).digest();
const secondEDPublic = ed25519.getPublicKey(secondEDSecret);
const firstKeyPair = KeyPair.fromString(baseEncode(new Uint8Array(Buffer.concat([firstEDSecret, Buffer.from(firstEDPublic)]))));
const secondKeyPair = KeyPair.fromString(baseEncode(new Uint8Array(Buffer.concat([secondEDSecret, Buffer.from(secondEDPublic)]))));
const firstKeyPair = KeyPair.fromString(baseEncode(new Uint8Array(Buffer.concat([Buffer.from(firstEDSecret), Buffer.from(firstEDPublic)]))));
const secondKeyPair = KeyPair.fromString(baseEncode(new Uint8Array(Buffer.concat([Buffer.from(secondEDSecret), Buffer.from(secondEDPublic)]))));
return [firstKeyPair, secondKeyPair];
});
};
Expand Down
33 changes: 14 additions & 19 deletions packages/biometric-ed25519/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import base64 from '@hexagon/base64';
import { secp256k1 } from '@noble/curves/secp256k1';
import { Sha256 } from '@aws-crypto/sha256-js';
import { p256 } from '@noble/curves/p256';
import { sha256 } from '@noble/hashes/sha256';
import { PublicKey } from '@near-js/crypto';

export const preformatMakeCredReq = (makeCredReq) => {
Expand All @@ -25,7 +25,7 @@ export const preformatMakeCredReq = (makeCredReq) => {
export const get64BytePublicKeyFromPEM = (publicKey: PublicKey) => {
const prefix = '\n';
const publicKeyBase64 = publicKey.toString().split(prefix);
return base64.toArrayBuffer(`${publicKeyBase64[1]}${publicKeyBase64[2]}`).slice(27);
return base64.toArrayBuffer(`${publicKeyBase64[1]}${publicKeyBase64[2]}`).slice(27, 59);
};

export const validateUsername = (name: string): string => {
Expand Down Expand Up @@ -77,22 +77,17 @@ export const recoverPublicKey = async (r, s, message, recovery) => {
if (recovery !== 0 && recovery !== 1) {
throw new Error('Invalid recovery parameter');
}

const hash = new Sha256();
hash.update(message);

const sigObjQ = new secp256k1.Signature(r, s);
sigObjQ.addRecoveryBit(0);
const sigObjP = new secp256k1.Signature(r, s);
sigObjP.addRecoveryBit(1);
const sigObjQ = new p256.Signature(r, s).addRecoveryBit(0);
const sigObjP = new p256.Signature(r, s).addRecoveryBit(1);
const hash = sha256.create().update(message).digest();

const h = await hash.digest();

const Q = sigObjQ.recoverPublicKey(h);
const P = sigObjP.recoverPublicKey(h);

return [
Buffer.from(Q.toRawBytes()).subarray(1, 65),
Buffer.from(P.toRawBytes()).subarray(1, 65)
];
const Q = sigObjQ.recoverPublicKey(hash);
const P = sigObjP.recoverPublicKey(hash);
return [Q.toRawBytes().subarray(1, 33), P.toRawBytes().subarray(1, 33)];
};

export const uint8ArrayToBigInt = (uint8Array: Uint8Array) => {
const array = Array.from(uint8Array);
return BigInt('0x' + array.map(byte => byte.toString(16).padStart(2, '0')).join(''));
};
10 changes: 10 additions & 0 deletions packages/cookbook/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# @near-js/cookbook

## 1.0.16

### Patch Changes

- Updated dependencies [[`42dc7e2a`](https://github.com/near/near-api-js/commit/42dc7e2ac794e973987bed7b89da5ef2d3c6c8ac)]:
- @near-js/transactions@1.1.2
- @near-js/accounts@1.0.4
- [email protected]
- @near-js/providers@0.1.1

## 1.0.15

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/cookbook/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@near-js/cookbook",
"private": true,
"version": "1.0.15",
"version": "1.0.16",
"description": "",
"main": "main.js",
"author": "",
Expand Down
10 changes: 10 additions & 0 deletions packages/near-api-js/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# near-api-js

## 3.0.4

### Patch Changes

- Updated dependencies [[`42dc7e2a`](https://github.com/near/near-api-js/commit/42dc7e2ac794e973987bed7b89da5ef2d3c6c8ac)]:
- @near-js/transactions@1.1.2
- @near-js/accounts@1.0.4
- @near-js/providers@0.1.1
- @near-js/wallet-account@1.1.1

## 3.0.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/near-api-js/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "near-api-js",
"description": "JavaScript library to interact with NEAR Protocol via RPC API",
"version": "3.0.3",
"version": "3.0.4",
"repository": {
"type": "git",
"url": "git+https://github.com/near/near-api-js.git"
Expand Down
7 changes: 7 additions & 0 deletions packages/providers/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @near-js/providers

## 0.1.1

### Patch Changes

- Updated dependencies [[`42dc7e2a`](https://github.com/near/near-api-js/commit/42dc7e2ac794e973987bed7b89da5ef2d3c6c8ac)]:
- @near-js/transactions@1.1.2

## 0.1.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/providers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@near-js/providers",
"version": "0.1.0",
"version": "0.1.1",
"description": "Library of implementations for interfacing with the NEAR blockchain",
"main": "lib/index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/providers/test/providers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ describe('providers', () => {

// TODO: Use a near-workspaces Worker when time traveling is available
test('json rpc get next light client block', async () => {
const provider = new JsonRpcProvider({ url: 'https://rpc.ci-testnet.near.org' });
const provider = new JsonRpcProvider({ url: 'https://rpc.testnet.near.org' });
const stat = await provider.status();

// Get block in at least the last epoch (epoch duration 43,200 blocks on mainnet and testnet)
Expand Down
6 changes: 6 additions & 0 deletions packages/transactions/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @near-js/transactions

## 1.1.2

### Patch Changes

- [#1314](https://github.com/near/near-api-js/pull/1314) [`42dc7e2a`](https://github.com/near/near-api-js/commit/42dc7e2ac794e973987bed7b89da5ef2d3c6c8ac) Thanks [@gagdiez](https://github.com/gagdiez)! - Fixed delegateAction Schema

## 1.1.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/transactions/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@near-js/transactions",
"version": "1.1.1",
"version": "1.1.2",
"description": "Functions and data types for transactions on NEAR",
"main": "lib/index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/transactions/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ export const SCHEMA = new class BorshSchema {
struct: {
senderId: 'string',
receiverId: 'string',
nonce: 'u64',
actions: { array: { type: this.ClassicActions } },
nonce: 'u64',
maxBlockHeight: 'u64',
publicKey: this.PublicKey,
}
Expand Down
8 changes: 8 additions & 0 deletions packages/transactions/test/serialize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ test('serialize object', async () => {
expect(new_value.q).toEqual([1, 2, 3]);
});

test('deserialize delegate', async () => {
const serialized = [8, 16, 0, 0, 0, 116, 104, 101, 45, 117, 115, 101, 114, 46, 116, 101, 115, 116, 110, 101, 116, 27, 0, 0, 0, 104, 101, 108, 108, 111, 46, 110, 101, 97, 114, 45, 101, 120, 97, 109, 112, 108, 101, 115, 46, 116, 101, 115, 116, 110, 101, 116, 1, 0, 0, 0, 2, 12, 0, 0, 0, 115, 101, 116, 95, 103, 114, 101, 101, 116, 105, 110, 103, 20, 0, 0, 0, 123, 34, 103, 114, 101, 101, 116, 105, 110, 103, 34, 58, 34, 72, 101, 108, 108, 111, 34, 125, 0, 224, 87, 235, 72, 27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 9, 120, 166, 131, 144, 0, 0, 61, 158, 123, 9, 0, 0, 0, 0, 0, 154, 156, 80, 116, 108, 65, 42, 39, 47, 253, 146, 109, 67, 106, 83, 230, 57, 183, 195, 122, 150, 6, 246, 220, 173, 35, 120, 139, 167, 94, 183, 29, 0, 41, 98, 10, 45, 51, 177, 89, 159, 190, 247, 41, 255, 243, 17, 186, 140, 168, 139, 9, 81, 33, 8, 74, 73, 85, 254, 127, 62, 54, 193, 60, 50, 235, 49, 13, 37, 152, 94, 172, 24, 198, 220, 119, 148, 99, 89, 19, 187, 251, 80, 76, 230, 77, 28, 80, 140, 133, 81, 139, 159, 62, 245, 167, 4];
const { signedDelegate: { delegateAction } } = deserialize(SCHEMA.Action, serialized);
expect(delegateAction.senderId).toEqual('the-user.testnet');
expect(delegateAction.receiverId).toEqual('hello.near-examples.testnet');
expect(String(delegateAction.nonce)).toEqual('158895108000003');
});

test('serialize and sign multi-action tx', async () => {
const keyStore = new InMemoryKeyStore();
const keyPair = KeyPair.fromString('ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw');
Expand Down
8 changes: 8 additions & 0 deletions packages/wallet-account/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @near-js/wallet-account

## 1.1.1

### Patch Changes

- Updated dependencies [[`42dc7e2a`](https://github.com/near/near-api-js/commit/42dc7e2ac794e973987bed7b89da5ef2d3c6c8ac)]:
- @near-js/transactions@1.1.2
- @near-js/accounts@1.0.4

## 1.1.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet-account/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@near-js/wallet-account",
"version": "1.1.0",
"version": "1.1.1",
"description": "Dependencies for the NEAR API JavaScript client in the browser",
"main": "lib/index.js",
"scripts": {
Expand Down
Loading

0 comments on commit d9b0657

Please sign in to comment.