@concordium/common-sdk
Advanced tools
Comparing version
@@ -208,2 +208,3 @@ import { Buffer } from 'buffer/'; | ||
* @param {T | T[]} input - Input for for contract function. | ||
* @param {HexString} [blockHash] - The hash of the block to perform the invocation of. Defaults to the latest finalized block on chain. | ||
* | ||
@@ -210,0 +211,0 @@ * @throws If the query could not be invoked successfully. |
@@ -208,2 +208,3 @@ "use strict"; | ||
* @param {T | T[]} input - Input for for contract function. | ||
* @param {HexString} [blockHash] - The hash of the block to perform the invocation of. Defaults to the latest finalized block on chain. | ||
* | ||
@@ -210,0 +211,0 @@ * @throws If the query could not be invoked successfully. |
@@ -169,2 +169,19 @@ /** | ||
/** | ||
* Sends an account transaction, with an already serialized payload, to the node to be | ||
* put in a block on the chain. | ||
* | ||
* Note that a transaction can still fail even if it was accepted by the node. | ||
* To keep track of the transaction use getTransactionStatus. | ||
* | ||
* In general, { @link ConcordiumGRPCClient.sendAccountTransaction } is the recommended | ||
* method to send account transactions, as this does not require the caller to serialize the payload themselves. | ||
* | ||
* @param header the transactionheader to send to the node | ||
* @param energyAmount the amount of energy allotted for the transaction | ||
* @param payload the payload serialized to a buffer | ||
* @param signature the signatures on the signing digest of the transaction | ||
* @returns The transaction hash as a byte array | ||
*/ | ||
sendRawAccountTransaction(header: v1.AccountTransactionHeader, energyAmount: bigint, payload: Buffer, signature: v1.AccountTransactionSignature): Promise<HexString>; | ||
/** | ||
* Sends a credential deployment transaction, for creating a new account, | ||
@@ -171,0 +188,0 @@ * to the node to be put in a block on the chain. |
@@ -265,20 +265,39 @@ "use strict"; | ||
async sendAccountTransaction(transaction, signature) { | ||
const accountTransactionHandler = (0, accountTransactions_1.getAccountTransactionHandler)(transaction.type); | ||
const rawPayload = (0, serialization_1.serializeAccountTransactionPayload)(transaction); | ||
const transactionSignature = translate.accountTransactionSignatureToV2(signature); | ||
// Energy cost | ||
const accountTransactionHandler = (0, accountTransactions_1.getAccountTransactionHandler)(transaction.type); | ||
const baseEnergyCost = accountTransactionHandler.getBaseEnergyCost(transaction.payload); | ||
const energyCost = (0, energyCost_1.calculateEnergyCost)((0, util_1.countSignatures)(signature), BigInt(rawPayload.length), baseEnergyCost); | ||
return this.sendRawAccountTransaction(transaction.header, energyCost, rawPayload, signature); | ||
} | ||
/** | ||
* Sends an account transaction, with an already serialized payload, to the node to be | ||
* put in a block on the chain. | ||
* | ||
* Note that a transaction can still fail even if it was accepted by the node. | ||
* To keep track of the transaction use getTransactionStatus. | ||
* | ||
* In general, { @link ConcordiumGRPCClient.sendAccountTransaction } is the recommended | ||
* method to send account transactions, as this does not require the caller to serialize the payload themselves. | ||
* | ||
* @param header the transactionheader to send to the node | ||
* @param energyAmount the amount of energy allotted for the transaction | ||
* @param payload the payload serialized to a buffer | ||
* @param signature the signatures on the signing digest of the transaction | ||
* @returns The transaction hash as a byte array | ||
*/ | ||
async sendRawAccountTransaction(header, energyAmount, payload, signature) { | ||
const transactionSignature = translate.accountTransactionSignatureToV2(signature); | ||
// Put together sendBlockItemRequest | ||
const header = { | ||
sender: { value: transaction.header.sender.decodedAddress }, | ||
sequenceNumber: { value: transaction.header.nonce }, | ||
energyAmount: { value: energyCost }, | ||
expiry: { value: transaction.header.expiry.expiryEpochSeconds }, | ||
const convertedHeader = { | ||
sender: { value: header.sender.decodedAddress }, | ||
sequenceNumber: { value: header.nonce }, | ||
energyAmount: { value: energyAmount }, | ||
expiry: { value: header.expiry.expiryEpochSeconds }, | ||
}; | ||
const accountTransaction = { | ||
signature: transactionSignature, | ||
header: header, | ||
header: convertedHeader, | ||
payload: { | ||
payload: { oneofKind: 'rawPayload', rawPayload: rawPayload }, | ||
payload: { oneofKind: 'rawPayload', rawPayload: payload }, | ||
}, | ||
@@ -285,0 +304,0 @@ }; |
@@ -44,3 +44,3 @@ "use strict"; | ||
useNativeBigInt: true, | ||
}).stringify(this, replaceDateWithTimeStampAttribute); | ||
}).stringify(this); | ||
} | ||
@@ -52,3 +52,3 @@ static fromString(json) { | ||
useNativeBigInt: true, | ||
}).parse(json, reviveDateFromTimeStampAttribute); | ||
}).parse(json); | ||
return new VerifiablePresentation(parsed.presentationContext, parsed.proof, parsed.type, parsed.verifiableCredential); | ||
@@ -55,0 +55,0 @@ } |
import { ContractAddress, CryptographicParameters } from './types'; | ||
import { AttributeType } from './web3ProofTypes'; | ||
import { AttributeType, StatementAttributeType, TimestampAttribute } from './web3ProofTypes'; | ||
export declare type VerifyWeb3IdCredentialSignatureInput = { | ||
@@ -27,1 +27,21 @@ globalContext: CryptographicParameters; | ||
export declare function isStringAttributeInRange(value: string, lower: string, upper: string): boolean; | ||
/** | ||
* Converts a timestamp attribute to a Date. | ||
* @param attribute the timestamp attribute | ||
* @returns a Date representing the timestamp | ||
*/ | ||
export declare function timestampToDate(attribute: TimestampAttribute): Date; | ||
/** | ||
* Converts a Date to a timestamp attribute. | ||
* @param value the date to convert to an attribute | ||
* @returns the timestamp attribute for the provided date | ||
*/ | ||
export declare function dateToTimestampAttribute(value: Date): TimestampAttribute; | ||
/** | ||
* Converts a statement attribute to an attribute. Statement attributes allow | ||
* for Date which are mapped into timestamp attributes. All other attribute | ||
* types are mapped one-to-one. | ||
* @param statementAttribute the statement attribute to map | ||
* @returns the mapped attribute type | ||
*/ | ||
export declare function statementAttributeTypeToAttributeType(statementAttribute: StatementAttributeType): AttributeType; |
@@ -26,6 +26,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isStringAttributeInRange = exports.compareStringAttributes = exports.verifyWeb3IdCredentialSignature = void 0; | ||
exports.statementAttributeTypeToAttributeType = exports.dateToTimestampAttribute = exports.timestampToDate = exports.isStringAttributeInRange = exports.compareStringAttributes = exports.verifyWeb3IdCredentialSignature = void 0; | ||
const wasm = __importStar(require("@concordium/rust-bindings")); | ||
const json_bigint_1 = require("json-bigint"); | ||
const VerifiablePresentation_1 = require("./types/VerifiablePresentation"); | ||
/** | ||
@@ -36,3 +35,3 @@ * Verifies that the given signature is correct for the given values/randomness/holder/issuerPublicKey/issuerContract | ||
// Use json-bigint stringify to ensure we can handle bigints | ||
return wasm.verifyWeb3IdCredentialSignature((0, json_bigint_1.stringify)(input, VerifiablePresentation_1.replaceDateWithTimeStampAttribute)); | ||
return wasm.verifyWeb3IdCredentialSignature((0, json_bigint_1.stringify)(input)); | ||
} | ||
@@ -62,1 +61,36 @@ exports.verifyWeb3IdCredentialSignature = verifyWeb3IdCredentialSignature; | ||
exports.isStringAttributeInRange = isStringAttributeInRange; | ||
/** | ||
* Converts a timestamp attribute to a Date. | ||
* @param attribute the timestamp attribute | ||
* @returns a Date representing the timestamp | ||
*/ | ||
function timestampToDate(attribute) { | ||
return new Date(Date.parse(attribute.timestamp)); | ||
} | ||
exports.timestampToDate = timestampToDate; | ||
/** | ||
* Converts a Date to a timestamp attribute. | ||
* @param value the date to convert to an attribute | ||
* @returns the timestamp attribute for the provided date | ||
*/ | ||
function dateToTimestampAttribute(value) { | ||
return { | ||
type: 'date-time', | ||
timestamp: value.toISOString(), | ||
}; | ||
} | ||
exports.dateToTimestampAttribute = dateToTimestampAttribute; | ||
/** | ||
* Converts a statement attribute to an attribute. Statement attributes allow | ||
* for Date which are mapped into timestamp attributes. All other attribute | ||
* types are mapped one-to-one. | ||
* @param statementAttribute the statement attribute to map | ||
* @returns the mapped attribute type | ||
*/ | ||
function statementAttributeTypeToAttributeType(statementAttribute) { | ||
if (statementAttribute instanceof Date) { | ||
return dateToTimestampAttribute(statementAttribute); | ||
} | ||
return statementAttribute; | ||
} | ||
exports.statementAttributeTypeToAttributeType = statementAttributeTypeToAttributeType; |
import { AttributeList, ContractAddress, HexString, Network } from './types'; | ||
import { AtomicStatementV2, CredentialStatements, CredentialSchemaSubject, Web3IdProofInput, AccountCommitmentInput, Web3IssuerCommitmentInput, CredentialStatement, CredentialSubject, AttributeType } from './web3ProofTypes'; | ||
import { AtomicStatementV2, CredentialStatements, CredentialSchemaSubject, Web3IdProofInput, AccountCommitmentInput, Web3IssuerCommitmentInput, CredentialStatement, CredentialSubject, AttributeType, StatementAttributeType } from './web3ProofTypes'; | ||
import { StatementBuilder } from './commonProofTypes'; | ||
@@ -26,3 +26,3 @@ import { ConcordiumHdWallet } from './HdWallet'; | ||
/** | ||
* If checkConstraints is true, this checks whether the given statement may be added to the statement being built. | ||
* This checks whether the given statement may be added to the statement being built. | ||
* If the statement breaks any rules, this will throw an error. | ||
@@ -38,3 +38,3 @@ */ | ||
*/ | ||
addRange(attribute: string, lower: AttributeType, upper: AttributeType): this; | ||
addRange(attribute: string, lower: StatementAttributeType, upper: StatementAttributeType): this; | ||
/** | ||
@@ -46,3 +46,3 @@ * Add to the statement, that the given attribute should be one of the values in the given set. | ||
*/ | ||
addMembership(attribute: string, set: AttributeType[]): this; | ||
addMembership(attribute: string, set: StatementAttributeType[]): this; | ||
/** | ||
@@ -54,3 +54,3 @@ * Add to the statement, that the given attribute should _not_ be one of the values in the given set. | ||
*/ | ||
addNonMembership(attribute: string, set: AttributeType[]): this; | ||
addNonMembership(attribute: string, set: StatementAttributeType[]): this; | ||
/** | ||
@@ -105,3 +105,3 @@ * Add to the statement, that the given attribute should be revealed. | ||
} | ||
declare type InternalBuilder = StatementBuilder<AttributeType, string>; | ||
declare type InternalBuilder = StatementBuilder<StatementAttributeType, string>; | ||
export declare class Web3StatementBuilder { | ||
@@ -108,0 +108,0 @@ private statements; |
@@ -65,3 +65,3 @@ "use strict"; | ||
}; | ||
function isTimeStampAttribute(properties) { | ||
function isTimestampAttributeSchemaProperty(properties) { | ||
return (properties && | ||
@@ -82,3 +82,4 @@ properties.type === 'object' && | ||
function validateTimestampAttribute(value) { | ||
return value instanceof Date && isValidTimestampAttribute(value); | ||
return ((0, web3ProofTypes_1.isTimestampAttribute)(value) && | ||
isValidTimestampAttribute((0, web3IdHelpers_1.timestampToDate)(value))); | ||
} | ||
@@ -107,3 +108,3 @@ function validateStringAttribute(value) { | ||
}; | ||
if (isTimeStampAttribute(properties)) { | ||
if (isTimestampAttributeSchemaProperty(properties)) { | ||
checkRange('timestamp', validateTimestampAttribute, 'Date', TIMESTAMP_VALID_VALUES); | ||
@@ -120,5 +121,7 @@ } | ||
if ((properties?.type === 'integer' && statement.upper < statement.lower) || | ||
(isTimeStampAttribute(properties) && | ||
statement.upper.getTime() < | ||
statement.lower.getTime()) || | ||
(isTimestampAttributeSchemaProperty(properties) && | ||
(0, web3ProofTypes_1.isTimestampAttribute)(statement.lower) && | ||
(0, web3ProofTypes_1.isTimestampAttribute)(statement.upper) && | ||
(0, web3IdHelpers_1.timestampToDate)(statement.upper).getTime() < | ||
(0, web3IdHelpers_1.timestampToDate)(statement.lower).getTime()) || | ||
(properties?.type === 'string' && | ||
@@ -142,3 +145,3 @@ (0, web3IdHelpers_1.compareStringAttributes)(statement.lower, statement.upper) > 0)) { | ||
}; | ||
if (isTimeStampAttribute(properties)) { | ||
if (isTimestampAttributeSchemaProperty(properties)) { | ||
checkSet('date-time', validateTimestampAttribute, 'Date', TIMESTAMP_VALID_VALUES); | ||
@@ -231,3 +234,3 @@ } | ||
/** | ||
* If checkConstraints is true, this checks whether the given statement may be added to the statement being built. | ||
* This checks whether the given statement may be added to the statement being built. | ||
* If the statement breaks any rules, this will throw an error. | ||
@@ -251,4 +254,4 @@ */ | ||
attributeTag: attribute, | ||
lower, | ||
upper, | ||
lower: (0, web3IdHelpers_1.statementAttributeTypeToAttributeType)(lower), | ||
upper: (0, web3IdHelpers_1.statementAttributeTypeToAttributeType)(upper), | ||
}; | ||
@@ -269,3 +272,3 @@ this.check(statement); | ||
attributeTag: attribute, | ||
set, | ||
set: set.map(web3IdHelpers_1.statementAttributeTypeToAttributeType), | ||
}; | ||
@@ -286,3 +289,3 @@ this.check(statement); | ||
attributeTag: attribute, | ||
set, | ||
set: set.map(web3IdHelpers_1.statementAttributeTypeToAttributeType), | ||
}; | ||
@@ -395,3 +398,3 @@ this.check(statement); | ||
// Use json-bigint stringify to ensure we can handle bigints | ||
wasm.createWeb3IdProof((0, json_bigint_1.stringify)(input, VerifiablePresentation_1.replaceDateWithTimeStampAttribute))); | ||
wasm.createWeb3IdProof((0, json_bigint_1.stringify)(input))); | ||
return s; | ||
@@ -492,7 +495,8 @@ } | ||
} | ||
if (value instanceof Date && | ||
lower instanceof Date && | ||
upper instanceof Date) { | ||
return (lower.getTime() <= value.getTime() && | ||
upper.getTime() > value.getTime()); | ||
if ((0, web3ProofTypes_1.isTimestampAttribute)(value) && | ||
(0, web3ProofTypes_1.isTimestampAttribute)(lower) && | ||
(0, web3ProofTypes_1.isTimestampAttribute)(upper)) { | ||
return ((0, web3IdHelpers_1.timestampToDate)(lower).getTime() <= | ||
(0, web3IdHelpers_1.timestampToDate)(value).getTime() && | ||
(0, web3IdHelpers_1.timestampToDate)(upper).getTime() > (0, web3IdHelpers_1.timestampToDate)(value).getTime()); | ||
} | ||
@@ -509,6 +513,8 @@ // Mismatch in types. | ||
} | ||
if (value instanceof Date) { | ||
if ((0, web3ProofTypes_1.isTimestampAttribute)(value)) { | ||
return set | ||
.map((date) => (date instanceof Date ? date.getTime() : undefined)) | ||
.includes(value.getTime()); | ||
.map((timestamp) => (0, web3ProofTypes_1.isTimestampAttribute)(timestamp) | ||
? (0, web3IdHelpers_1.timestampToDate)(timestamp).getTime() | ||
: undefined) | ||
.includes((0, web3IdHelpers_1.timestampToDate)(value).getTime()); | ||
} | ||
@@ -515,0 +521,0 @@ return false; |
import { GenericAtomicStatement, GenericRevealStatement, GenericMembershipStatement, GenericNonMembershipStatement, GenericRangeStatement } from './commonProofTypes'; | ||
import { ContractAddress, CryptographicParameters } from './types'; | ||
export declare type AttributeType = string | bigint | Date; | ||
export declare type TimestampAttribute = { | ||
type: 'date-time'; | ||
timestamp: string; | ||
}; | ||
export declare type AttributeType = string | bigint | TimestampAttribute; | ||
export declare type StatementAttributeType = AttributeType | Date; | ||
export declare function isTimestampAttribute(attribute: AttributeType): attribute is TimestampAttribute; | ||
export declare type AccountCommitmentInput = { | ||
@@ -5,0 +11,0 @@ type: 'account'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isVerifiableCredentialRequestStatement = exports.isVerifiableCredentialStatement = exports.isAccountCredentialStatement = exports.IDENTITY_SUBJECT_SCHEMA = void 0; | ||
exports.isVerifiableCredentialRequestStatement = exports.isVerifiableCredentialStatement = exports.isAccountCredentialStatement = exports.IDENTITY_SUBJECT_SCHEMA = exports.isTimestampAttribute = void 0; | ||
function isTimestampAttribute(attribute) { | ||
return (attribute.type === 'date-time' && | ||
typeof attribute.timestamp === 'string'); | ||
} | ||
exports.isTimestampAttribute = isTimestampAttribute; | ||
exports.IDENTITY_SUBJECT_SCHEMA = { | ||
@@ -5,0 +10,0 @@ type: 'object', |
{ | ||
"name": "@concordium/common-sdk", | ||
"version": "9.2.1", | ||
"version": "9.3.0", | ||
"license": "Apache-2.0", | ||
@@ -5,0 +5,0 @@ "engines": { |
1476705
0.4%33781
0.32%