Socket
Socket
Sign inDemoInstall

@near-js/transactions

Package Overview
Dependencies
Maintainers
2
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@near-js/transactions - npm Package Compare versions

Comparing version 1.2.2 to 1.3.0-next.0

58

lib/action_creators.js

@@ -1,5 +0,2 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.actionCreators = exports.stringifyJsonOrBytes = void 0;
const actions_1 = require("./actions");
import { AccessKey, AccessKeyPermission, Action, AddKey, CreateAccount, DeleteAccount, DeleteKey, DeployContract, FullAccessPermission, FunctionCall, FunctionCallPermission, SignedDelegate, Stake, Transfer, } from './actions';
/**

@@ -10,6 +7,6 @@ * Creates a full access key with full access permissions.

function fullAccessKey() {
return new actions_1.AccessKey({
nonce: 0,
permission: new actions_1.AccessKeyPermission({
fullAccess: new actions_1.FullAccessPermission({}),
return new AccessKey({
nonce: 0n,
permission: new AccessKeyPermission({
fullAccess: new FullAccessPermission(),
}),

@@ -26,6 +23,6 @@ });

function functionCallAccessKey(receiverId, methodNames, allowance) {
return new actions_1.AccessKey({
nonce: 0,
permission: new actions_1.AccessKeyPermission({
functionCall: new actions_1.FunctionCallPermission({
return new AccessKey({
nonce: 0n,
permission: new AccessKeyPermission({
functionCall: new FunctionCallPermission({
receiverId,

@@ -43,3 +40,3 @@ allowance,

function createAccount() {
return new actions_1.Action({ createAccount: new actions_1.CreateAccount({}) });
return new Action({ createAccount: new CreateAccount() });
}

@@ -52,3 +49,3 @@ /**

function deployContract(code) {
return new actions_1.Action({ deployContract: new actions_1.DeployContract({ code }) });
return new Action({ deployContract: new DeployContract({ code }) });
}

@@ -60,7 +57,6 @@ /**

*/
function stringifyJsonOrBytes(args) {
export function stringifyJsonOrBytes(args) {
const isUint8Array = args.byteLength !== undefined && args.byteLength === args.length;
return isUint8Array ? args : Buffer.from(JSON.stringify(args));
}
exports.stringifyJsonOrBytes = stringifyJsonOrBytes;
/**

@@ -77,10 +73,10 @@ * Constructs {@link Action} instance representing contract method call.

*/
function functionCall(methodName, args, gas = BigInt(0), deposit = BigInt(0), stringify = stringifyJsonOrBytes, jsContract = false) {
function functionCall(methodName, args, gas = 0n, deposit = 0n, stringify = stringifyJsonOrBytes, jsContract = false) {
if (jsContract) {
return new actions_1.Action({
functionCall: new actions_1.FunctionCall({ methodName, args, gas, deposit }),
return new Action({
functionCall: new FunctionCall({ methodName, args: args, gas, deposit }),
});
}
return new actions_1.Action({
functionCall: new actions_1.FunctionCall({
return new Action({
functionCall: new FunctionCall({
methodName,

@@ -98,4 +94,4 @@ args: stringify(args),

*/
function transfer(deposit = BigInt(0)) {
return new actions_1.Action({ transfer: new actions_1.Transfer({ deposit }) });
function transfer(deposit = 0n) {
return new Action({ transfer: new Transfer({ deposit }) });
}

@@ -108,4 +104,4 @@ /**

*/
function stake(stake = BigInt(0), publicKey) {
return new actions_1.Action({ stake: new actions_1.Stake({ stake, publicKey }) });
function stake(stake = 0n, publicKey) {
return new Action({ stake: new Stake({ stake, publicKey }) });
}

@@ -119,3 +115,3 @@ /**

function addKey(publicKey, accessKey) {
return new actions_1.Action({ addKey: new actions_1.AddKey({ publicKey, accessKey }) });
return new Action({ addKey: new AddKey({ publicKey, accessKey }) });
}

@@ -128,3 +124,3 @@ /**

function deleteKey(publicKey) {
return new actions_1.Action({ deleteKey: new actions_1.DeleteKey({ publicKey }) });
return new Action({ deleteKey: new DeleteKey({ publicKey }) });
}

@@ -137,3 +133,3 @@ /**

function deleteAccount(beneficiaryId) {
return new actions_1.Action({ deleteAccount: new actions_1.DeleteAccount({ beneficiaryId }) });
return new Action({ deleteAccount: new DeleteAccount({ beneficiaryId }) });
}

@@ -147,7 +143,7 @@ /**

function signedDelegate({ delegateAction, signature, }) {
return new actions_1.Action({
signedDelegate: new actions_1.SignedDelegate({ delegateAction, signature }),
return new Action({
signedDelegate: new SignedDelegate({ delegateAction, signature }),
});
}
exports.actionCreators = {
export const actionCreators = {
addKey,

@@ -154,0 +150,0 @@ createAccount,

import { PublicKey } from '@near-js/crypto';
import { Assignable } from '@near-js/types';
import { Enum } from '@near-js/types';
import { DelegateAction } from './delegate';
import { Signature } from './signature';
declare abstract class Enum {
enum: string;
constructor(properties: any);
}
export declare class FunctionCallPermission extends Assignable {
export declare class FunctionCallPermission {
allowance?: bigint;
receiverId: string;
methodNames: string[];
constructor({ allowance, receiverId, methodNames }: {
allowance: bigint;
receiverId: string;
methodNames: string[];
});
}
export declare class FullAccessPermission extends Assignable {
export declare class FullAccessPermission {
}
export declare class AccessKeyPermission extends Enum {
enum: string;
functionCall?: FunctionCallPermission;
fullAccess?: FullAccessPermission;
constructor(props: any);
}
export declare class AccessKey extends Assignable {
export declare class AccessKey {
nonce: bigint;
permission: AccessKeyPermission;
constructor({ nonce, permission }: {
nonce: bigint;
permission: AccessKeyPermission;
});
}
export declare class IAction extends Assignable {
export declare class CreateAccount {
}
export declare class CreateAccount extends IAction {
}
export declare class DeployContract extends IAction {
export declare class DeployContract {
code: Uint8Array;
constructor({ code }: {
code: Uint8Array;
});
}
export declare class FunctionCall extends IAction {
export declare class FunctionCall {
methodName: string;

@@ -36,23 +44,50 @@ args: Uint8Array;

deposit: bigint;
constructor({ methodName, args, gas, deposit }: {
methodName: string;
args: Uint8Array;
gas: bigint;
deposit: bigint;
});
}
export declare class Transfer extends IAction {
export declare class Transfer {
deposit: bigint;
constructor({ deposit }: {
deposit: bigint;
});
}
export declare class Stake extends IAction {
export declare class Stake {
stake: bigint;
publicKey: PublicKey;
constructor({ stake, publicKey }: {
stake: bigint;
publicKey: PublicKey;
});
}
export declare class AddKey extends IAction {
export declare class AddKey {
publicKey: PublicKey;
accessKey: AccessKey;
constructor({ publicKey, accessKey }: {
publicKey: PublicKey;
accessKey: AccessKey;
});
}
export declare class DeleteKey extends IAction {
export declare class DeleteKey {
publicKey: PublicKey;
constructor({ publicKey }: {
publicKey: PublicKey;
});
}
export declare class DeleteAccount extends IAction {
export declare class DeleteAccount {
beneficiaryId: string;
constructor({ beneficiaryId }: {
beneficiaryId: string;
});
}
export declare class SignedDelegate extends IAction {
export declare class SignedDelegate {
delegateAction: DelegateAction;
signature: Signature;
constructor({ delegateAction, signature }: {
delegateAction: DelegateAction;
signature: Signature;
});
}

@@ -64,2 +99,3 @@ /**

export declare class Action extends Enum {
enum: string;
createAccount?: CreateAccount;

@@ -74,4 +110,4 @@ deployContract?: DeployContract;

signedDelegate?: SignedDelegate;
constructor(props: any);
}
export {};
//# sourceMappingURL=actions.d.ts.map

@@ -1,58 +0,96 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Action = exports.SignedDelegate = exports.DeleteAccount = exports.DeleteKey = exports.AddKey = exports.Stake = exports.Transfer = exports.FunctionCall = exports.DeployContract = exports.CreateAccount = exports.IAction = exports.AccessKey = exports.AccessKeyPermission = exports.FullAccessPermission = exports.FunctionCallPermission = void 0;
const types_1 = require("@near-js/types");
class Enum {
constructor(properties) {
if (Object.keys(properties).length !== 1) {
throw new Error('Enum can only take single value');
}
Object.keys(properties).map((key) => {
this[key] = properties[key];
this.enum = key;
});
import { Enum } from '@near-js/types';
export class FunctionCallPermission {
allowance;
receiverId;
methodNames;
constructor({ allowance, receiverId, methodNames }) {
this.allowance = allowance;
this.receiverId = receiverId;
this.methodNames = methodNames;
}
}
class FunctionCallPermission extends types_1.Assignable {
export class FullAccessPermission {
}
exports.FunctionCallPermission = FunctionCallPermission;
class FullAccessPermission extends types_1.Assignable {
export class AccessKeyPermission extends Enum {
enum;
functionCall;
fullAccess;
constructor(props) {
super(props);
for (const [k, v] of Object.entries(props || {})) {
this[k] = v;
this.enum = k;
}
}
}
exports.FullAccessPermission = FullAccessPermission;
class AccessKeyPermission extends Enum {
export class AccessKey {
nonce;
permission;
constructor({ nonce, permission }) {
this.nonce = nonce;
this.permission = permission;
}
}
exports.AccessKeyPermission = AccessKeyPermission;
class AccessKey extends types_1.Assignable {
export class CreateAccount {
}
exports.AccessKey = AccessKey;
class IAction extends types_1.Assignable {
export class DeployContract {
code;
constructor({ code }) {
this.code = code;
}
}
exports.IAction = IAction;
class CreateAccount extends IAction {
export class FunctionCall {
methodName;
args;
gas;
deposit;
constructor({ methodName, args, gas, deposit }) {
this.methodName = methodName;
this.args = args;
this.gas = gas;
this.deposit = deposit;
}
}
exports.CreateAccount = CreateAccount;
class DeployContract extends IAction {
export class Transfer {
deposit;
constructor({ deposit }) {
this.deposit = deposit;
}
}
exports.DeployContract = DeployContract;
class FunctionCall extends IAction {
export class Stake {
stake;
publicKey;
constructor({ stake, publicKey }) {
this.stake = stake;
this.publicKey = publicKey;
}
}
exports.FunctionCall = FunctionCall;
class Transfer extends IAction {
export class AddKey {
publicKey;
accessKey;
constructor({ publicKey, accessKey }) {
this.publicKey = publicKey;
this.accessKey = accessKey;
}
}
exports.Transfer = Transfer;
class Stake extends IAction {
export class DeleteKey {
publicKey;
constructor({ publicKey }) {
this.publicKey = publicKey;
}
}
exports.Stake = Stake;
class AddKey extends IAction {
export class DeleteAccount {
beneficiaryId;
constructor({ beneficiaryId }) {
this.beneficiaryId = beneficiaryId;
}
}
exports.AddKey = AddKey;
class DeleteKey extends IAction {
export class SignedDelegate {
delegateAction;
signature;
constructor({ delegateAction, signature }) {
this.delegateAction = delegateAction;
this.signature = signature;
}
}
exports.DeleteKey = DeleteKey;
class DeleteAccount extends IAction {
}
exports.DeleteAccount = DeleteAccount;
class SignedDelegate extends IAction {
}
exports.SignedDelegate = SignedDelegate;
/**

@@ -62,4 +100,20 @@ * Contains a list of the valid transaction Actions available with this API

*/
class Action extends Enum {
export class Action extends Enum {
enum;
createAccount;
deployContract;
functionCall;
transfer;
stake;
addKey;
deleteKey;
deleteAccount;
signedDelegate;
constructor(props) {
super(props);
for (const [k, v] of Object.entries(props || {})) {
this[k] = v;
this.enum = k;
}
}
}
exports.Action = Action;

@@ -1,5 +0,2 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTransaction = void 0;
const schema_1 = require("./schema");
import { Transaction } from './schema';
/**

@@ -15,5 +12,5 @@ * Creates a new transaction object with the provided parameters.

*/
function createTransaction(signerId, publicKey, receiverId, nonce, actions, blockHash) {
export function createTransaction(signerId, publicKey, receiverId, nonce, actions, blockHash) {
const txNonce = typeof nonce === 'bigint' ? nonce : BigInt(nonce);
return new schema_1.Transaction({
return new Transaction({
signerId,

@@ -27,2 +24,1 @@ publicKey,

}
exports.createTransaction = createTransaction;
import { PublicKey } from '@near-js/crypto';
import { Assignable } from '@near-js/types';
import { Action } from './actions';
export declare class DelegateAction extends Assignable {
export declare class DelegateAction {
senderId: string;

@@ -11,2 +10,10 @@ receiverId: string;

publicKey: PublicKey;
constructor({ senderId, receiverId, actions, nonce, maxBlockHeight, publicKey }: {
senderId: string;
receiverId: string;
actions: Action[];
nonce: bigint;
maxBlockHeight: bigint;
publicKey: PublicKey;
});
}

@@ -13,0 +20,0 @@ /**

@@ -1,10 +0,19 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildDelegateAction = exports.DelegateAction = void 0;
const types_1 = require("@near-js/types");
const action_creators_1 = require("./action_creators");
const { addKey, createAccount, deleteAccount, deleteKey, deployContract, functionCall, stake, transfer, } = action_creators_1.actionCreators;
class DelegateAction extends types_1.Assignable {
import { actionCreators } from './action_creators';
const { addKey, createAccount, deleteAccount, deleteKey, deployContract, functionCall, stake, transfer, } = actionCreators;
export class DelegateAction {
senderId;
receiverId;
actions;
nonce;
maxBlockHeight;
publicKey;
constructor({ senderId, receiverId, actions, nonce, maxBlockHeight, publicKey }) {
this.senderId = senderId;
this.receiverId = receiverId;
this.actions = actions;
this.nonce = nonce;
this.maxBlockHeight = maxBlockHeight;
this.publicKey = publicKey;
}
}
exports.DelegateAction = DelegateAction;
/**

@@ -19,3 +28,3 @@ * Compose a delegate action for inclusion with a meta transaction signed on the sender's behalf

*/
function buildDelegateAction({ actions, maxBlockHeight, nonce, publicKey, receiverId, senderId, }) {
export function buildDelegateAction({ actions, maxBlockHeight, nonce, publicKey, receiverId, senderId, }) {
return new DelegateAction({

@@ -74,2 +83,1 @@ senderId,

}
exports.buildDelegateAction = buildDelegateAction;

@@ -1,23 +0,7 @@

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./action_creators"), exports);
__exportStar(require("./actions"), exports);
__exportStar(require("./create_transaction"), exports);
__exportStar(require("./delegate"), exports);
__exportStar(require("./schema"), exports);
__exportStar(require("./sign"), exports);
__exportStar(require("./signature"), exports);
export * from './action_creators';
export * from './actions';
export * from './create_transaction';
export * from './delegate';
export * from './schema';
export * from './sign';
export * from './signature';

@@ -1,5 +0,7 @@

import { Assignable } from '@near-js/types';
/** Base class for NEP message prefixes **/
declare abstract class NEPPrefix extends Assignable {
declare abstract class NEPPrefix {
prefix: number;
constructor({ prefix }: {
prefix: number;
});
}

@@ -6,0 +8,0 @@ /** Class for constructing prefixes on actionable (on-chain) messages **/

@@ -1,5 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DelegateActionPrefix = void 0;
const types_1 = require("@near-js/types");
const ACTIONABLE_MESSAGE_BASE = Math.pow(2, 30);

@@ -12,3 +8,7 @@ // const NON_ACTIONABLE_MESSAGE_BASE = Math.pow(2, 31);

/** Base class for NEP message prefixes **/
class NEPPrefix extends types_1.Assignable {
class NEPPrefix {
prefix;
constructor({ prefix }) {
this.prefix = prefix;
}
}

@@ -33,5 +33,4 @@ /** Class for constructing prefixes on actionable (on-chain) messages **/

/** Prefix for delegate actions whose signatures must always be distinguishable from valid transaction signatures **/
class DelegateActionPrefix extends ActionableMessagePrefix {
export class DelegateActionPrefix extends ActionableMessagePrefix {
constructor() { super(NEP.MetaTransactions); }
}
exports.DelegateActionPrefix = DelegateActionPrefix;
import { PublicKey } from '@near-js/crypto';
import { Assignable } from '@near-js/types';
import { Schema } from 'borsh';

@@ -20,6 +19,6 @@ import { Action, SignedDelegate } from './actions';

/**
* Borsh-encode a transaction or signed transaction into a serialized form.
* @param transaction The transaction or signed transaction object to be encoded.
* @returns A serialized representation of the input transaction.
*/
* Borsh-encode a transaction or signed transaction into a serialized form.
* @param transaction The transaction or signed transaction object to be encoded.
* @returns A serialized representation of the input transaction.
*/
export declare function encodeTransaction(transaction: Transaction | SignedTransaction): Uint8Array;

@@ -36,3 +35,3 @@ /**

export declare function decodeSignedTransaction(bytes: Uint8Array): SignedTransaction;
export declare class Transaction extends Assignable {
export declare class Transaction {
signerId: string;

@@ -44,8 +43,20 @@ publicKey: PublicKey;

blockHash: Uint8Array;
constructor({ signerId, publicKey, nonce, receiverId, actions, blockHash }: {
signerId: string;
publicKey: PublicKey;
nonce: bigint;
receiverId: string;
actions: Action[];
blockHash: Uint8Array;
});
encode(): Uint8Array;
static decode(bytes: Uint8Array): Transaction;
}
export declare class SignedTransaction extends Assignable {
export declare class SignedTransaction {
transaction: Transaction;
signature: Signature;
constructor({ transaction, signature }: {
transaction: Transaction;
signature: Signature;
});
encode(): Uint8Array;

@@ -55,3 +66,7 @@ static decode(bytes: Uint8Array): SignedTransaction;

export declare const SCHEMA: {
Ed25519Signature: Schema;
Secp256k1Signature: Schema;
Signature: Schema;
Ed25519Data: Schema;
Secp256k1Data: Schema;
PublicKey: Schema;

@@ -58,0 +73,0 @@ FunctionCallPermission: Schema;

@@ -1,7 +0,3 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SCHEMA = exports.SignedTransaction = exports.Transaction = exports.decodeSignedTransaction = exports.decodeTransaction = exports.encodeTransaction = exports.encodeSignedDelegate = exports.encodeDelegateAction = void 0;
const types_1 = require("@near-js/types");
const borsh_1 = require("borsh");
const prefix_1 = require("./prefix");
import { deserialize, serialize } from 'borsh';
import { DelegateActionPrefix } from './prefix';
/**

@@ -13,9 +9,8 @@ * Borsh-encode a delegate action for inclusion as an action within a meta transaction

*/
function encodeDelegateAction(delegateAction) {
export function encodeDelegateAction(delegateAction) {
return new Uint8Array([
...(0, borsh_1.serialize)(exports.SCHEMA.DelegateActionPrefix, new prefix_1.DelegateActionPrefix()),
...(0, borsh_1.serialize)(exports.SCHEMA.DelegateAction, delegateAction),
...serialize(SCHEMA.DelegateActionPrefix, new DelegateActionPrefix()),
...serialize(SCHEMA.DelegateAction, delegateAction),
]);
}
exports.encodeDelegateAction = encodeDelegateAction;
/**

@@ -25,16 +20,14 @@ * Borsh-encode a signed delegate for validation and execution by a relayer

*/
function encodeSignedDelegate(signedDelegate) {
return (0, borsh_1.serialize)(exports.SCHEMA.SignedDelegate, signedDelegate);
export function encodeSignedDelegate(signedDelegate) {
return serialize(SCHEMA.SignedDelegate, signedDelegate);
}
exports.encodeSignedDelegate = encodeSignedDelegate;
/**
* Borsh-encode a transaction or signed transaction into a serialized form.
* @param transaction The transaction or signed transaction object to be encoded.
* @returns A serialized representation of the input transaction.
*/
function encodeTransaction(transaction) {
const schema = transaction instanceof SignedTransaction ? exports.SCHEMA.SignedTransaction : exports.SCHEMA.Transaction;
return (0, borsh_1.serialize)(schema, transaction);
* Borsh-encode a transaction or signed transaction into a serialized form.
* @param transaction The transaction or signed transaction object to be encoded.
* @returns A serialized representation of the input transaction.
*/
export function encodeTransaction(transaction) {
const schema = transaction instanceof SignedTransaction ? SCHEMA.SignedTransaction : SCHEMA.Transaction;
return serialize(schema, transaction);
}
exports.encodeTransaction = encodeTransaction;
/**

@@ -44,6 +37,5 @@ * Borsh-decode a Transaction instance from a buffer

*/
function decodeTransaction(bytes) {
return new Transaction((0, borsh_1.deserialize)(exports.SCHEMA.Transaction, bytes));
export function decodeTransaction(bytes) {
return new Transaction(deserialize(SCHEMA.Transaction, bytes));
}
exports.decodeTransaction = decodeTransaction;
/**

@@ -53,7 +45,20 @@ * Borsh-decode a SignedTransaction instance from a buffer

*/
function decodeSignedTransaction(bytes) {
return new SignedTransaction((0, borsh_1.deserialize)(exports.SCHEMA.SignedTransaction, bytes));
export function decodeSignedTransaction(bytes) {
return new SignedTransaction(deserialize(SCHEMA.SignedTransaction, bytes));
}
exports.decodeSignedTransaction = decodeSignedTransaction;
class Transaction extends types_1.Assignable {
export class Transaction {
signerId;
publicKey;
nonce;
receiverId;
actions;
blockHash;
constructor({ signerId, publicKey, nonce, receiverId, actions, blockHash }) {
this.signerId = signerId;
this.publicKey = publicKey;
this.nonce = nonce;
this.receiverId = receiverId;
this.actions = actions;
this.blockHash = blockHash;
}
encode() {

@@ -66,4 +71,9 @@ return encodeTransaction(this);

}
exports.Transaction = Transaction;
class SignedTransaction extends types_1.Assignable {
export class SignedTransaction {
transaction;
signature;
constructor({ transaction, signature }) {
this.transaction = transaction;
this.signature = signature;
}
encode() {

@@ -76,145 +86,162 @@ return encodeTransaction(this);

}
exports.SignedTransaction = SignedTransaction;
exports.SCHEMA = new class BorshSchema {
constructor() {
this.Signature = {
struct: {
keyType: 'u8',
data: { array: { type: 'u8', len: 64 } },
}
};
this.PublicKey = {
struct: {
keyType: 'u8',
data: { array: { type: 'u8', len: 32 } },
}
};
this.FunctionCallPermission = {
struct: {
allowance: { option: 'u128' },
receiverId: 'string',
methodNames: { array: { type: 'string' } },
}
};
this.FullAccessPermission = {
struct: {}
};
this.AccessKeyPermission = {
enum: [
{ struct: { functionCall: this.FunctionCallPermission } },
{ struct: { fullAccess: this.FullAccessPermission } },
]
};
this.AccessKey = {
struct: {
nonce: 'u64',
permission: this.AccessKeyPermission,
}
};
this.CreateAccount = {
struct: {}
};
this.DeployContract = {
struct: {
code: { array: { type: 'u8' } },
}
};
this.FunctionCall = {
struct: {
methodName: 'string',
args: { array: { type: 'u8' } },
gas: 'u64',
deposit: 'u128',
}
};
this.Transfer = {
struct: {
deposit: 'u128',
}
};
this.Stake = {
struct: {
stake: 'u128',
publicKey: this.PublicKey,
}
};
this.AddKey = {
struct: {
publicKey: this.PublicKey,
accessKey: this.AccessKey,
}
};
this.DeleteKey = {
struct: {
publicKey: this.PublicKey,
}
};
this.DeleteAccount = {
struct: {
beneficiaryId: 'string',
}
};
this.DelegateActionPrefix = {
struct: {
prefix: 'u32',
}
};
this.ClassicActions = {
enum: [
{ struct: { createAccount: this.CreateAccount } },
{ struct: { deployContract: this.DeployContract } },
{ struct: { functionCall: this.FunctionCall } },
{ struct: { transfer: this.Transfer } },
{ struct: { stake: this.Stake } },
{ struct: { addKey: this.AddKey } },
{ struct: { deleteKey: this.DeleteKey } },
{ struct: { deleteAccount: this.DeleteAccount } },
]
};
this.DelegateAction = {
struct: {
senderId: 'string',
receiverId: 'string',
actions: { array: { type: this.ClassicActions } },
nonce: 'u64',
maxBlockHeight: 'u64',
publicKey: this.PublicKey,
}
};
this.SignedDelegate = {
struct: {
delegateAction: this.DelegateAction,
signature: this.Signature,
}
};
this.Action = {
enum: [
{ struct: { createAccount: this.CreateAccount } },
{ struct: { deployContract: this.DeployContract } },
{ struct: { functionCall: this.FunctionCall } },
{ struct: { transfer: this.Transfer } },
{ struct: { stake: this.Stake } },
{ struct: { addKey: this.AddKey } },
{ struct: { deleteKey: this.DeleteKey } },
{ struct: { deleteAccount: this.DeleteAccount } },
{ struct: { signedDelegate: this.SignedDelegate } },
]
};
this.Transaction = {
struct: {
signerId: 'string',
publicKey: this.PublicKey,
nonce: 'u64',
receiverId: 'string',
blockHash: { array: { type: 'u8', len: 32 } },
actions: { array: { type: this.Action } },
}
};
this.SignedTransaction = {
struct: {
transaction: this.Transaction,
signature: this.Signature,
}
};
}
export const SCHEMA = new class BorshSchema {
Ed25519Signature = {
struct: {
data: { array: { type: 'u8', len: 64 } },
}
};
Secp256k1Signature = {
struct: {
data: { array: { type: 'u8', len: 65 } },
}
};
Signature = {
enum: [
{ struct: { ed25519Signature: this.Ed25519Signature } },
{ struct: { secp256k1Signature: this.Secp256k1Signature } },
]
};
Ed25519Data = {
struct: {
data: { array: { type: 'u8', len: 32 } },
}
};
Secp256k1Data = {
struct: {
data: { array: { type: 'u8', len: 64 } },
}
};
PublicKey = {
enum: [
{ struct: { ed25519Key: this.Ed25519Data } },
{ struct: { secp256k1Key: this.Secp256k1Data } },
]
};
FunctionCallPermission = {
struct: {
allowance: { option: 'u128' },
receiverId: 'string',
methodNames: { array: { type: 'string' } },
}
};
FullAccessPermission = {
struct: {}
};
AccessKeyPermission = {
enum: [
{ struct: { functionCall: this.FunctionCallPermission } },
{ struct: { fullAccess: this.FullAccessPermission } },
]
};
AccessKey = {
struct: {
nonce: 'u64',
permission: this.AccessKeyPermission,
}
};
CreateAccount = {
struct: {}
};
DeployContract = {
struct: {
code: { array: { type: 'u8' } },
}
};
FunctionCall = {
struct: {
methodName: 'string',
args: { array: { type: 'u8' } },
gas: 'u64',
deposit: 'u128',
}
};
Transfer = {
struct: {
deposit: 'u128',
}
};
Stake = {
struct: {
stake: 'u128',
publicKey: this.PublicKey,
}
};
AddKey = {
struct: {
publicKey: this.PublicKey,
accessKey: this.AccessKey,
}
};
DeleteKey = {
struct: {
publicKey: this.PublicKey,
}
};
DeleteAccount = {
struct: {
beneficiaryId: 'string',
}
};
DelegateActionPrefix = {
struct: {
prefix: 'u32',
}
};
ClassicActions = {
enum: [
{ struct: { createAccount: this.CreateAccount } },
{ struct: { deployContract: this.DeployContract } },
{ struct: { functionCall: this.FunctionCall } },
{ struct: { transfer: this.Transfer } },
{ struct: { stake: this.Stake } },
{ struct: { addKey: this.AddKey } },
{ struct: { deleteKey: this.DeleteKey } },
{ struct: { deleteAccount: this.DeleteAccount } },
]
};
DelegateAction = {
struct: {
senderId: 'string',
receiverId: 'string',
actions: { array: { type: this.ClassicActions } },
nonce: 'u64',
maxBlockHeight: 'u64',
publicKey: this.PublicKey,
}
};
SignedDelegate = {
struct: {
delegateAction: this.DelegateAction,
signature: this.Signature,
}
};
Action = {
enum: [
{ struct: { createAccount: this.CreateAccount } },
{ struct: { deployContract: this.DeployContract } },
{ struct: { functionCall: this.FunctionCall } },
{ struct: { transfer: this.Transfer } },
{ struct: { stake: this.Stake } },
{ struct: { addKey: this.AddKey } },
{ struct: { deleteKey: this.DeleteKey } },
{ struct: { deleteAccount: this.DeleteAccount } },
{ struct: { signedDelegate: this.SignedDelegate } },
]
};
Transaction = {
struct: {
signerId: 'string',
publicKey: this.PublicKey,
nonce: 'u64',
receiverId: 'string',
blockHash: { array: { type: 'u8', len: 32 } },
actions: { array: { type: this.Action } },
}
};
SignedTransaction = {
struct: {
transaction: this.Transaction,
signature: this.Signature,
}
};
};

@@ -1,18 +0,7 @@

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.signDelegateAction = exports.signTransaction = void 0;
const sha256_1 = require("@noble/hashes/sha256");
const actions_1 = require("./actions");
const create_transaction_1 = require("./create_transaction");
const schema_1 = require("./schema");
const signature_1 = require("./signature");
import { sha256 } from '@noble/hashes/sha256';
import { SignedDelegate } from './actions';
import { createTransaction } from './create_transaction';
import { encodeDelegateAction, encodeTransaction, SignedTransaction, Transaction } from './schema';
import { Signature } from './signature';
import { KeyType } from '@near-js/crypto';
/**

@@ -25,29 +14,25 @@ * Signs a given transaction from an account with given keys, applied to the given network

*/
function signTransactionObject(transaction, signer, accountId, networkId) {
return __awaiter(this, void 0, void 0, function* () {
const message = (0, schema_1.encodeTransaction)(transaction);
const hash = new Uint8Array((0, sha256_1.sha256)(message));
const signature = yield signer.signMessage(message, accountId, networkId);
const signedTx = new schema_1.SignedTransaction({
transaction,
signature: new signature_1.Signature({ keyType: transaction.publicKey.keyType, data: signature.signature })
});
return [hash, signedTx];
async function signTransactionObject(transaction, signer, accountId, networkId) {
const message = encodeTransaction(transaction);
const hash = new Uint8Array(sha256(message));
const signature = await signer.signMessage(message, accountId, networkId);
const keyType = transaction.publicKey.ed25519Key ? KeyType.ED25519 : KeyType.SECP256K1;
const signedTx = new SignedTransaction({
transaction,
signature: new Signature({ keyType, data: signature.signature })
});
return [hash, signedTx];
}
function signTransaction(...args) {
return __awaiter(this, void 0, void 0, function* () {
if (args[0].constructor === schema_1.Transaction) {
const [transaction, signer, accountId, networkId] = args;
return signTransactionObject(transaction, signer, accountId, networkId);
}
else {
const [receiverId, nonce, actions, blockHash, signer, accountId, networkId] = args;
const publicKey = yield signer.getPublicKey(accountId, networkId);
const transaction = (0, create_transaction_1.createTransaction)(accountId, publicKey, receiverId, nonce, actions, blockHash);
return signTransactionObject(transaction, signer, accountId, networkId);
}
});
export async function signTransaction(...args) {
if (args[0].constructor === Transaction) {
const [transaction, signer, accountId, networkId] = args;
return signTransactionObject(transaction, signer, accountId, networkId);
}
else {
const [receiverId, nonce, actions, blockHash, signer, accountId, networkId] = args;
const publicKey = await signer.getPublicKey(accountId, networkId);
const transaction = createTransaction(accountId, publicKey, receiverId, nonce, actions, blockHash);
return signTransactionObject(transaction, signer, accountId, networkId);
}
}
exports.signTransaction = signTransaction;
/**

@@ -59,19 +44,17 @@ * Sign a delegate action

*/
function signDelegateAction({ delegateAction, signer }) {
return __awaiter(this, void 0, void 0, function* () {
const message = (0, schema_1.encodeDelegateAction)(delegateAction);
const signature = yield signer.sign(message);
const signedDelegateAction = new actions_1.SignedDelegate({
delegateAction,
signature: new signature_1.Signature({
keyType: delegateAction.publicKey.keyType,
data: signature,
}),
});
return {
hash: new Uint8Array((0, sha256_1.sha256)(message)),
signedDelegateAction,
};
export async function signDelegateAction({ delegateAction, signer }) {
const message = encodeDelegateAction(delegateAction);
const signature = await signer.sign(message);
const keyType = delegateAction.publicKey.ed25519Key ? KeyType.ED25519 : KeyType.SECP256K1;
const signedDelegateAction = new SignedDelegate({
delegateAction,
signature: new Signature({
keyType,
data: signature,
}),
});
return {
hash: new Uint8Array(sha256(message)),
signedDelegateAction,
};
}
exports.signDelegateAction = signDelegateAction;
import { KeyType } from '@near-js/crypto';
import { Assignable } from '@near-js/types';
export declare class Signature extends Assignable {
import { Enum } from '@near-js/types';
declare class ED25519Signature {
keyType: KeyType;
data: Uint8Array;
}
declare class SECP256K1Signature {
keyType: KeyType;
data: Uint8Array;
}
export declare class Signature extends Enum {
enum: string;
ed25519Signature?: ED25519Signature;
secp256k1Signature?: SECP256K1Signature;
constructor(signature: {
keyType: KeyType;
data: Uint8Array;
});
get signature(): ED25519Signature | SECP256K1Signature;
get signatureType(): KeyType;
get data(): Uint8Array;
}
export {};
//# sourceMappingURL=signature.d.ts.map

@@ -1,7 +0,43 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Signature = void 0;
const types_1 = require("@near-js/types");
class Signature extends types_1.Assignable {
import { KeyType } from '@near-js/crypto';
import { Enum } from '@near-js/types';
class ED25519Signature {
keyType = KeyType.ED25519;
data;
}
exports.Signature = Signature;
class SECP256K1Signature {
keyType = KeyType.SECP256K1;
data;
}
function resolveEnumKeyName(keyType) {
switch (keyType) {
case KeyType.ED25519: {
return 'ed25519Signature';
}
case KeyType.SECP256K1: {
return 'secp256k1Signature';
}
default: {
throw Error(`unknown type ${keyType}`);
}
}
}
export class Signature extends Enum {
enum;
ed25519Signature;
secp256k1Signature;
constructor(signature) {
const keyName = resolveEnumKeyName(signature.keyType);
super({ [keyName]: signature });
this[keyName] = signature;
this.enum = keyName;
}
get signature() {
return this.ed25519Signature || this.secp256k1Signature;
}
get signatureType() {
return this.signature.keyType;
}
get data() {
return this.signature.data;
}
}
{
"name": "@near-js/transactions",
"version": "1.2.2",
"version": "1.3.0-next.0",
"description": "Functions and data types for transactions on NEAR",
"main": "lib/index.js",
"type": "module",
"keywords": [],

@@ -10,15 +11,17 @@ "author": "",

"dependencies": {
"@noble/hashes": "1.3.3",
"borsh": "1.0.0",
"@noble/hashes": "1.3.3",
"@near-js/crypto": "1.2.4",
"@near-js/signers": "0.1.4",
"@near-js/types": "0.2.1",
"@near-js/utils": "0.2.2"
"@near-js/signers": "0.2.0-next.0",
"@near-js/utils": "0.3.0-next.0",
"@near-js/types": "0.3.0-next.0",
"@near-js/crypto": "1.3.0-next.0"
},
"devDependencies": {
"@types/node": "18.11.18",
"jest": "26.0.1",
"ts-jest": "26.5.6",
"typescript": "4.9.4",
"@near-js/keystores": "0.0.12"
"@jest/globals": "^29.7.0",
"@types/node": "20.0.0",
"jest": "29.7.0",
"ts-jest": "29.1.5",
"typescript": "5.4.5",
"@near-js/keystores": "0.1.0-next.0",
"tsconfig": "0.0.0"
},

@@ -31,8 +34,6 @@ "files": [

"compile": "tsc -p tsconfig.json",
"lint:js": "eslint -c ../../.eslintrc.js.yml test/**/*.js --no-eslintrc",
"lint:js:fix": "eslint -c ../../.eslintrc.js.yml test/**/*.js --no-eslintrc --fix",
"lint:ts": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts --no-eslintrc",
"lint:ts:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts --no-eslintrc --fix",
"test": "jest test"
"lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc",
"lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix",
"test": "jest"
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc