@actioncodes/protocol 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/base.d.ts +10 -0
- package/dist/adapters/base.d.ts.map +1 -1
- package/dist/adapters/solana/solana.d.ts +7 -1
- package/dist/adapters/solana/solana.d.ts.map +1 -1
- package/dist/adapters/solana/solana.js +39 -0
- package/dist/codegen.d.ts +11 -11
- package/dist/codegen.d.ts.map +1 -1
- package/dist/codegen.js +79 -27
- package/docs/actioncode/classes/ActionCode.md +23 -23
- package/docs/actioncode/interfaces/ActionCodeFields.md +11 -11
- package/docs/actioncode/interfaces/ActionCodeMetadata.md +3 -3
- package/docs/actioncode/interfaces/ActionCodeTransaction.md +4 -4
- package/docs/actioncode/type-aliases/ActionCodeStatus.md +1 -1
- package/docs/adapters/base/classes/BaseChainAdapter.md +38 -10
- package/docs/adapters/solana/solana/classes/SolanaAdapter.md +42 -12
- package/docs/adapters/solana/solana/type-aliases/SolanaTransaction.md +1 -1
- package/docs/codegen/classes/CodeGenerator.md +29 -29
- package/docs/constants/type-aliases/SupportedChain.md +1 -1
- package/docs/constants/variables/CODE_LENGTH.md +1 -1
- package/docs/constants/variables/CODE_TTL.md +1 -1
- package/docs/constants/variables/MAX_PREFIX_LENGTH.md +1 -1
- package/docs/constants/variables/MIN_PREFIX_LENGTH.md +1 -1
- package/docs/constants/variables/PROTOCOL_CODE_PREFIX.md +1 -1
- package/docs/constants/variables/PROTOCOL_PREFIX.md +1 -1
- package/docs/constants/variables/PROTOCOL_VERSION.md +1 -1
- package/docs/constants/variables/SUPPORTED_CHAINS.md +1 -1
- package/docs/meta/classes/ProtocolMetaParser.md +6 -6
- package/docs/meta/interfaces/ProtocolMetaV1.md +7 -7
- package/docs/protocol/classes/ActionCodesProtocol.md +21 -21
- package/docs/protocol/interfaces/ProtocolConfig.md +7 -7
- package/package.json +1 -1
package/dist/adapters/base.d.ts
CHANGED
@@ -65,5 +65,15 @@ export declare abstract class BaseChainAdapter<T = any> {
|
|
65
65
|
* @returns True if the code signature is valid
|
66
66
|
*/
|
67
67
|
abstract verifyCodeSignature(actionCode: ActionCode): boolean;
|
68
|
+
/**
|
69
|
+
* Sign the transaction with the protocol key using a callback approach
|
70
|
+
* This method should be implemented by each chain-specific adapter to handle
|
71
|
+
* the chain's specific signing mechanism asynchronously
|
72
|
+
* @param tx - Chain-specific transaction to sign
|
73
|
+
* @param protocolPrivateKey - Private key or Keypair object depending on the chain
|
74
|
+
* @param signCallback - Callback function that performs the actual signing
|
75
|
+
* @returns Promise that resolves to the signed transaction
|
76
|
+
*/
|
77
|
+
abstract signWithProtocolKey(actionCode: ActionCode, key: any): Promise<ActionCode>;
|
68
78
|
}
|
69
79
|
//# sourceMappingURL=base.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/adapters/base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEzC;;;GAGG;AACH,8BAAsB,gBAAgB,CAAC,CAAC,GAAG,GAAG;IAC1C,QAAQ,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEhC;;;;OAIG;IACH,QAAQ,CAAC,MAAM,CAAC,IAAI,EAAE,cAAc,GAAG,GAAG;IAE1C;;;;OAIG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC,GAAG,cAAc,GAAG,IAAI;IAE7C;;;;;;OAMG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC,EAAE,WAAW,EAAE,MAAM,EAAE,EAAE,cAAc,CAAC,EAAE,MAAM,GAAG,OAAO;IAEjF;;;;;OAKG;IACH,QAAQ,CAAC,kBAAkB,CAAC,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO;IAE3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Ac,GAAE,MAAkB,GAAG,OAAO;IA8B1F;;;;;;OAMG;IACH,SAAS,CAAC,QAAQ,CAAC,4BAA4B,CAAC,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,cAAc,GAAG,OAAO;IAErF;;;;;;OAMG;IACH,uBAAuB,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,GAAE,MAA6B,GAAG,MAAM;IAIvG;;;;;;OAMG;IACH,QAAQ,CAAC,mBAAmB,CAAC,UAAU,EAAE,UAAU,GAAG,OAAO;IAE7D;;;;;;;;OAQG;IACH,QAAQ,CAAC,mBAAmB,CACxB,UAAU,EAAE,UAAU,EACtB,GAAG,EAAE,GAAG,GACT,OAAO,CAAC,UAAU,CAAC;CACzB"}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { Transaction, TransactionInstruction, VersionedTransaction } from '@solana/web3.js';
|
1
|
+
import { Transaction, TransactionInstruction, VersionedTransaction, Keypair } from '@solana/web3.js';
|
2
2
|
import { ProtocolMetaV1 } from '../../meta';
|
3
3
|
import { BaseChainAdapter } from '../base';
|
4
4
|
import { ActionCode } from '../../actioncode';
|
@@ -82,5 +82,11 @@ export declare class SolanaAdapter extends BaseChainAdapter<SolanaTransaction> {
|
|
82
82
|
*/
|
83
83
|
validateFromBase64(base64String: string, authorities: string[], expectedPrefix?: string): boolean;
|
84
84
|
verifyCodeSignature(actionCode: ActionCode): boolean;
|
85
|
+
/**
|
86
|
+
* Sign the transaction with the protocol key using a callback approach
|
87
|
+
* @param signCallback - Callback function that performs the actual signing
|
88
|
+
* @returns Promise that resolves to the signed transaction
|
89
|
+
*/
|
90
|
+
signWithProtocolKey(actionCode: ActionCode, key: Keypair): Promise<ActionCode>;
|
85
91
|
}
|
86
92
|
//# sourceMappingURL=solana.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"solana.d.ts","sourceRoot":"","sources":["../../../src/adapters/solana/solana.ts"],"names":[],"mappings":"AAAA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EAAE,IAAI,EAAE,cAAc,GAAG,OAAO;IA6B5F;;;;OAIG;IACH,gBAAgB,CAAC,YAAY,EAAE,MAAM,GAAG,cAAc,GAAG,IAAI;IAgB7D;;;;;;OAMG;IACH,kBAAkB,CAAC,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,EAAE,cAAc,GAAE,MAAkB,GAAG,OAAO;IAiBrG,mBAAmB,CAAC,UAAU,EAAE,UAAU,GAAG,OAAO;IAa3D;;;;OAIG;IACG,mBAAmB,CACrB,UAAU,EAAE,UAAU,EACtB,GAAG,EAAE,OAAO,GACb,OAAO,CAAC,UAAU,CAAC;CAkCzB"}
|
@@ -199,6 +199,12 @@ class SolanaAdapter extends base_1.BaseChainAdapter {
|
|
199
199
|
if (!decodedMeta) {
|
200
200
|
return false;
|
201
201
|
}
|
202
|
+
if (!meta.iss) {
|
203
|
+
return false;
|
204
|
+
}
|
205
|
+
if (!this.hasIssuerSignature(tx, meta.iss)) {
|
206
|
+
return false;
|
207
|
+
}
|
202
208
|
// Cross-check the decoded meta with the provided meta
|
203
209
|
return (decodedMeta.version === meta.version &&
|
204
210
|
decodedMeta.prefix === meta.prefix &&
|
@@ -265,6 +271,39 @@ class SolanaAdapter extends base_1.BaseChainAdapter {
|
|
265
271
|
return false;
|
266
272
|
}
|
267
273
|
}
|
274
|
+
/**
|
275
|
+
* Sign the transaction with the protocol key using a callback approach
|
276
|
+
* @param signCallback - Callback function that performs the actual signing
|
277
|
+
* @returns Promise that resolves to the signed transaction
|
278
|
+
*/
|
279
|
+
async signWithProtocolKey(actionCode, key) {
|
280
|
+
try {
|
281
|
+
if (!actionCode.transaction?.transaction) {
|
282
|
+
throw new Error('No transaction found');
|
283
|
+
}
|
284
|
+
const tx = web3_js_1.Transaction.from(buffer_1.Buffer.from(actionCode.transaction.transaction, 'base64'));
|
285
|
+
tx.partialSign(key);
|
286
|
+
const meta = this.decode(tx);
|
287
|
+
if (!meta) {
|
288
|
+
throw new Error('Invalid transaction, protocol meta not found');
|
289
|
+
}
|
290
|
+
if (!this.validateTransactionIntegrity(tx, meta)) {
|
291
|
+
throw new Error('Invalid transaction, transaction integrity not valid');
|
292
|
+
}
|
293
|
+
const newActionCode = Object.assign({}, actionCode, {
|
294
|
+
transaction: {
|
295
|
+
...actionCode.transaction,
|
296
|
+
transaction: buffer_1.Buffer.from(tx.serialize({
|
297
|
+
requireAllSignatures: false
|
298
|
+
})).toString('base64'),
|
299
|
+
}
|
300
|
+
});
|
301
|
+
return newActionCode;
|
302
|
+
}
|
303
|
+
catch (error) {
|
304
|
+
throw new Error(`Failed to sign transaction with protocol key: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
305
|
+
}
|
306
|
+
}
|
268
307
|
}
|
269
308
|
exports.SolanaAdapter = SolanaAdapter;
|
270
309
|
SolanaAdapter.MEMO_PROGRAM_ID = spl_memo_1.MEMO_PROGRAM_ID;
|
package/dist/codegen.d.ts
CHANGED
@@ -10,19 +10,19 @@ export declare class CodeGenerator {
|
|
10
10
|
*/
|
11
11
|
static validatePrefix(prefix: string): boolean;
|
12
12
|
/**
|
13
|
-
* Validate generated code format
|
13
|
+
* Validate generated code format (prefix + exactly 8 digits)
|
14
14
|
* @param code - The code to validate
|
15
15
|
* @returns True if code is valid, false otherwise
|
16
16
|
*/
|
17
17
|
static validateCodeFormat(code: string): boolean;
|
18
18
|
/**
|
19
|
-
* Validate that a code
|
20
|
-
* @param code - The code to validate
|
21
|
-
* @returns True if
|
19
|
+
* Validate that the numeric part of a code is exactly 8 digits
|
20
|
+
* @param code - The code to validate (can include prefix)
|
21
|
+
* @returns True if numeric part is valid, false otherwise
|
22
22
|
*/
|
23
23
|
static validateCodeDigits(code: string): boolean;
|
24
24
|
/**
|
25
|
-
* Normalize prefix - convert
|
25
|
+
* Normalize prefix - convert PROTOCOL_CODE_PREFIX to empty string, validate others
|
26
26
|
* @param prefix - The prefix to normalize
|
27
27
|
* @returns Normalized prefix
|
28
28
|
* @throws Error if prefix is invalid
|
@@ -31,7 +31,7 @@ export declare class CodeGenerator {
|
|
31
31
|
/**
|
32
32
|
* Generate a deterministic 8-digit code based on public key, prefix, and timestamp
|
33
33
|
* @param pubkey - Solana wallet public key (base58)
|
34
|
-
* @param prefix - Optional namespace prefix (default:
|
34
|
+
* @param prefix - Optional namespace prefix (default: PROTOCOL_CODE_PREFIX)
|
35
35
|
* @param timestamp - UNIX timestamp in milliseconds (defaults to now)
|
36
36
|
* @returns Object containing code, issuedAt, and expiresAt timestamps
|
37
37
|
* @throws Error if generated code is invalid
|
@@ -44,7 +44,7 @@ export declare class CodeGenerator {
|
|
44
44
|
/**
|
45
45
|
* Derive the full SHA-256 hash for storage or encryption key generation
|
46
46
|
* @param pubkey - Solana wallet public key (base58)
|
47
|
-
* @param prefix - Optional namespace prefix (default:
|
47
|
+
* @param prefix - Optional namespace prefix (default: PROTOCOL_CODE_PREFIX)
|
48
48
|
* @param timestamp - UNIX timestamp in milliseconds (defaults to now)
|
49
49
|
* @returns Full SHA-256 hash string
|
50
50
|
*/
|
@@ -53,16 +53,16 @@ export declare class CodeGenerator {
|
|
53
53
|
* Get the expected code for a given public key and timestamp
|
54
54
|
* @param pubkey - Solana wallet public key (base58)
|
55
55
|
* @param timestamp - UNIX timestamp in milliseconds
|
56
|
-
* @param prefix - Optional namespace prefix (default:
|
57
|
-
* @returns
|
56
|
+
* @param prefix - Optional namespace prefix (default: PROTOCOL_CODE_PREFIX)
|
57
|
+
* @returns Full code string with prefix + 8 digits
|
58
58
|
*/
|
59
59
|
static getExpectedCode(pubkey: string, timestamp: number, prefix?: string): string;
|
60
60
|
/**
|
61
61
|
* Validate if a code matches the expected code for a given public key and timestamp
|
62
|
-
* @param code - The code to validate
|
62
|
+
* @param code - The code to validate (can include prefix)
|
63
63
|
* @param pubkey - Solana wallet public key (base58)
|
64
64
|
* @param timestamp - UNIX timestamp in milliseconds
|
65
|
-
* @param prefix - Optional namespace prefix (default:
|
65
|
+
* @param prefix - Optional namespace prefix (default: PROTOCOL_CODE_PREFIX)
|
66
66
|
* @returns True if code matches expected code and timestamp is valid
|
67
67
|
*/
|
68
68
|
static validateCode(code: string, pubkey: string, timestamp: number, prefix?: string): boolean;
|
package/dist/codegen.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"codegen.d.ts","sourceRoot":"","sources":["../src/codegen.ts"],"names":[],"mappings":"AAGA,qBAAa,aAAa;IACtB,MAAM,CAAC,cAAc,SAAY;IACjC,MAAM,CAAC,WAAW,SAAe;IACjC,MAAM,CAAC,iBAAiB,SAAqB;IAC7C,MAAM,CAAC,iBAAiB,SAAqB;IAE7C;;;;OAIG;IACH,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAM9C;;;;OAIG;IACH,MAAM,CAAC,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;
|
1
|
+
{"version":3,"file":"codegen.d.ts","sourceRoot":"","sources":["../src/codegen.ts"],"names":[],"mappings":"AAGA,qBAAa,aAAa;IACtB,MAAM,CAAC,cAAc,SAAY;IACjC,MAAM,CAAC,WAAW,SAAe;IACjC,MAAM,CAAC,iBAAiB,SAAqB;IAC7C,MAAM,CAAC,iBAAiB,SAAqB;IAE7C;;;;OAIG;IACH,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAM9C;;;;OAIG;IACH,MAAM,CAAC,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAoChD;;;;OAIG;IACH,MAAM,CAAC,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAkChD;;;;;OAKG;IACH,MAAM,CAAC,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IAQ9C;;;;;;;OAOG;IACH,MAAM,CAAC,YAAY,CACf,MAAM,EAAE,MAAM,EACd,MAAM,GAAE,MAA6B,EACrC,SAAS,GAAE,MAAmB,GAC/B;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE;IAiCxD;;;;;;OAMG;IACH,MAAM,CAAC,cAAc,CACjB,MAAM,EAAE,MAAM,EACd,MAAM,GAAE,MAA6B,EACrC,SAAS,CAAC,EAAE,MAAM,GACnB,MAAM;IAQT;;;;;;OAMG;IACH,MAAM,CAAC,eAAe,CAClB,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,EACjB,MAAM,GAAE,MAA6B,GACtC,MAAM;IAIT;;;;;;;OAOG;IACH,MAAM,CAAC,YAAY,CACf,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,EACjB,MAAM,GAAE,MAA6B,GACtC,OAAO;IAgBV;;;;OAIG;IACH,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;CAGtD"}
|
package/dist/codegen.js
CHANGED
@@ -10,59 +10,109 @@ class CodeGenerator {
|
|
10
10
|
* @returns True if prefix is valid, false otherwise
|
11
11
|
*/
|
12
12
|
static validatePrefix(prefix) {
|
13
|
-
if (prefix ===
|
13
|
+
if (prefix === constants_1.PROTOCOL_CODE_PREFIX)
|
14
14
|
return true;
|
15
15
|
if (prefix.length < this.MIN_PREFIX_LENGTH || prefix.length > this.MAX_PREFIX_LENGTH)
|
16
16
|
return false;
|
17
17
|
return /^[A-Za-z]+$/.test(prefix);
|
18
18
|
}
|
19
19
|
/**
|
20
|
-
* Validate generated code format
|
20
|
+
* Validate generated code format (prefix + exactly 8 digits)
|
21
21
|
* @param code - The code to validate
|
22
22
|
* @returns True if code is valid, false otherwise
|
23
23
|
*/
|
24
24
|
static validateCodeFormat(code) {
|
25
25
|
if (!code || typeof code !== 'string')
|
26
26
|
return false;
|
27
|
-
|
27
|
+
// Code must be exactly prefix length + 8 digits
|
28
|
+
if (code.length < this.CODE_DIGITS)
|
28
29
|
return false;
|
29
|
-
|
30
|
+
// Find where the numeric part starts (last 8 characters)
|
31
|
+
const numericPart = code.slice(-this.CODE_DIGITS);
|
32
|
+
const prefixPart = code.slice(0, -this.CODE_DIGITS);
|
33
|
+
// Numeric part must be exactly 8 digits, no more, no less
|
34
|
+
if (numericPart.length !== this.CODE_DIGITS)
|
35
|
+
return false;
|
36
|
+
if (!/^[0-9]{8}$/.test(numericPart))
|
37
|
+
return false;
|
38
|
+
// If there's a prefix part, validate it
|
39
|
+
if (prefixPart.length > 0) {
|
40
|
+
// Normalize the prefix for validation
|
41
|
+
const normalizedPrefix = prefixPart.toUpperCase();
|
42
|
+
if (normalizedPrefix === constants_1.PROTOCOL_CODE_PREFIX) {
|
43
|
+
// DEFAULT prefix is valid but should be empty in normalized form
|
44
|
+
return true;
|
45
|
+
}
|
46
|
+
// Check prefix length and format
|
47
|
+
if (normalizedPrefix.length < this.MIN_PREFIX_LENGTH ||
|
48
|
+
normalizedPrefix.length > this.MAX_PREFIX_LENGTH) {
|
49
|
+
return false;
|
50
|
+
}
|
51
|
+
// Prefix must contain only letters
|
52
|
+
if (!/^[A-Za-z]+$/.test(normalizedPrefix))
|
53
|
+
return false;
|
54
|
+
}
|
55
|
+
return true;
|
30
56
|
}
|
31
57
|
/**
|
32
|
-
* Validate that a code
|
33
|
-
* @param code - The code to validate
|
34
|
-
* @returns True if
|
58
|
+
* Validate that the numeric part of a code is exactly 8 digits
|
59
|
+
* @param code - The code to validate (can include prefix)
|
60
|
+
* @returns True if numeric part is valid, false otherwise
|
35
61
|
*/
|
36
62
|
static validateCodeDigits(code) {
|
37
63
|
if (!code || typeof code !== 'string')
|
38
64
|
return false;
|
39
|
-
if (code.length
|
65
|
+
if (code.length < constants_1.CODE_LENGTH)
|
40
66
|
return false;
|
41
|
-
|
67
|
+
// For codes without prefix, the entire code must be exactly 8 digits
|
68
|
+
if (code.length === constants_1.CODE_LENGTH) {
|
69
|
+
return /^[0-9]{8}$/.test(code);
|
70
|
+
}
|
71
|
+
// For codes with prefix, the total length must be prefix + 8 digits
|
72
|
+
// and the last 8 characters must be digits
|
73
|
+
const numericPart = code.slice(-constants_1.CODE_LENGTH);
|
74
|
+
// Check if the numeric part is exactly 8 digits
|
75
|
+
if (!/^[0-9]{8}$/.test(numericPart))
|
76
|
+
return false;
|
77
|
+
// Check if the prefix part (everything before the last 8 chars) is valid
|
78
|
+
const prefixPart = code.slice(0, -constants_1.CODE_LENGTH);
|
79
|
+
if (prefixPart.length > 0) {
|
80
|
+
// Validate prefix format
|
81
|
+
const normalizedPrefix = prefixPart.toUpperCase();
|
82
|
+
if (normalizedPrefix === constants_1.PROTOCOL_CODE_PREFIX)
|
83
|
+
return true;
|
84
|
+
if (normalizedPrefix.length < this.MIN_PREFIX_LENGTH ||
|
85
|
+
normalizedPrefix.length > this.MAX_PREFIX_LENGTH) {
|
86
|
+
return false;
|
87
|
+
}
|
88
|
+
if (!/^[A-Za-z]+$/.test(normalizedPrefix))
|
89
|
+
return false;
|
90
|
+
}
|
91
|
+
return true;
|
42
92
|
}
|
43
93
|
/**
|
44
|
-
* Normalize prefix - convert
|
94
|
+
* Normalize prefix - convert PROTOCOL_CODE_PREFIX to empty string, validate others
|
45
95
|
* @param prefix - The prefix to normalize
|
46
96
|
* @returns Normalized prefix
|
47
97
|
* @throws Error if prefix is invalid
|
48
98
|
*/
|
49
99
|
static normalizePrefix(prefix) {
|
50
|
-
if (prefix ===
|
100
|
+
if (prefix === constants_1.PROTOCOL_CODE_PREFIX)
|
51
101
|
return "";
|
52
102
|
if (!this.validatePrefix(prefix)) {
|
53
|
-
throw new Error(`Invalid prefix: ${prefix}. Must be 3-12 letters or "
|
103
|
+
throw new Error(`Invalid prefix: ${prefix}. Must be 3-12 letters or "${constants_1.PROTOCOL_CODE_PREFIX}"`);
|
54
104
|
}
|
55
105
|
return prefix.toUpperCase();
|
56
106
|
}
|
57
107
|
/**
|
58
108
|
* Generate a deterministic 8-digit code based on public key, prefix, and timestamp
|
59
109
|
* @param pubkey - Solana wallet public key (base58)
|
60
|
-
* @param prefix - Optional namespace prefix (default:
|
110
|
+
* @param prefix - Optional namespace prefix (default: PROTOCOL_CODE_PREFIX)
|
61
111
|
* @param timestamp - UNIX timestamp in milliseconds (defaults to now)
|
62
112
|
* @returns Object containing code, issuedAt, and expiresAt timestamps
|
63
113
|
* @throws Error if generated code is invalid
|
64
114
|
*/
|
65
|
-
static generateCode(pubkey, prefix =
|
115
|
+
static generateCode(pubkey, prefix = constants_1.PROTOCOL_CODE_PREFIX, timestamp = Date.now()) {
|
66
116
|
const normalizedPrefix = this.normalizePrefix(prefix);
|
67
117
|
const input = `${normalizedPrefix}:${pubkey}:${timestamp}`;
|
68
118
|
const hash = (0, js_sha256_1.sha256)(input);
|
@@ -72,15 +122,17 @@ class CodeGenerator {
|
|
72
122
|
const issuedAt = timestamp;
|
73
123
|
const expiresAt = issuedAt + this.TIME_WINDOW_MS;
|
74
124
|
const generatedCode = code.toString().padStart(this.CODE_DIGITS, "0");
|
125
|
+
// Create the full code with prefix
|
126
|
+
const fullCode = normalizedPrefix + generatedCode;
|
75
127
|
// Validate the generated code
|
76
|
-
if (!this.validateCodeFormat(
|
77
|
-
throw new Error(`Generated code validation failed: ${
|
128
|
+
if (!this.validateCodeFormat(fullCode)) {
|
129
|
+
throw new Error(`Generated code validation failed: ${fullCode}`);
|
78
130
|
}
|
79
|
-
if (!this.validateCodeDigits(
|
80
|
-
throw new Error(`Generated code must be exactly 8 digits: ${
|
131
|
+
if (!this.validateCodeDigits(fullCode)) {
|
132
|
+
throw new Error(`Generated code must be exactly 8 digits: ${fullCode}`);
|
81
133
|
}
|
82
134
|
return {
|
83
|
-
code:
|
135
|
+
code: fullCode,
|
84
136
|
issuedAt,
|
85
137
|
expiresAt
|
86
138
|
};
|
@@ -88,11 +140,11 @@ class CodeGenerator {
|
|
88
140
|
/**
|
89
141
|
* Derive the full SHA-256 hash for storage or encryption key generation
|
90
142
|
* @param pubkey - Solana wallet public key (base58)
|
91
|
-
* @param prefix - Optional namespace prefix (default:
|
143
|
+
* @param prefix - Optional namespace prefix (default: PROTOCOL_CODE_PREFIX)
|
92
144
|
* @param timestamp - UNIX timestamp in milliseconds (defaults to now)
|
93
145
|
* @returns Full SHA-256 hash string
|
94
146
|
*/
|
95
|
-
static deriveCodeHash(pubkey, prefix =
|
147
|
+
static deriveCodeHash(pubkey, prefix = constants_1.PROTOCOL_CODE_PREFIX, timestamp) {
|
96
148
|
const normalizedPrefix = this.normalizePrefix(prefix);
|
97
149
|
const ts = timestamp ?? Date.now();
|
98
150
|
const input = `${normalizedPrefix}:${pubkey}:${ts}`;
|
@@ -103,21 +155,21 @@ class CodeGenerator {
|
|
103
155
|
* Get the expected code for a given public key and timestamp
|
104
156
|
* @param pubkey - Solana wallet public key (base58)
|
105
157
|
* @param timestamp - UNIX timestamp in milliseconds
|
106
|
-
* @param prefix - Optional namespace prefix (default:
|
107
|
-
* @returns
|
158
|
+
* @param prefix - Optional namespace prefix (default: PROTOCOL_CODE_PREFIX)
|
159
|
+
* @returns Full code string with prefix + 8 digits
|
108
160
|
*/
|
109
|
-
static getExpectedCode(pubkey, timestamp, prefix =
|
161
|
+
static getExpectedCode(pubkey, timestamp, prefix = constants_1.PROTOCOL_CODE_PREFIX) {
|
110
162
|
return this.generateCode(pubkey, prefix, timestamp).code;
|
111
163
|
}
|
112
164
|
/**
|
113
165
|
* Validate if a code matches the expected code for a given public key and timestamp
|
114
|
-
* @param code - The code to validate
|
166
|
+
* @param code - The code to validate (can include prefix)
|
115
167
|
* @param pubkey - Solana wallet public key (base58)
|
116
168
|
* @param timestamp - UNIX timestamp in milliseconds
|
117
|
-
* @param prefix - Optional namespace prefix (default:
|
169
|
+
* @param prefix - Optional namespace prefix (default: PROTOCOL_CODE_PREFIX)
|
118
170
|
* @returns True if code matches expected code and timestamp is valid
|
119
171
|
*/
|
120
|
-
static validateCode(code, pubkey, timestamp, prefix =
|
172
|
+
static validateCode(code, pubkey, timestamp, prefix = constants_1.PROTOCOL_CODE_PREFIX) {
|
121
173
|
// First validate the code format
|
122
174
|
if (!this.validateCodeFormat(code)) {
|
123
175
|
return false;
|
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
# Class: ActionCode
|
8
8
|
|
9
|
-
Defined in: [actioncode.ts:32](https://github.com/otaprotocol/actioncodes/blob/
|
9
|
+
Defined in: [actioncode.ts:32](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L32)
|
10
10
|
|
11
11
|
## Constructors
|
12
12
|
|
@@ -14,7 +14,7 @@ Defined in: [actioncode.ts:32](https://github.com/otaprotocol/actioncodes/blob/6
|
|
14
14
|
|
15
15
|
> **new ActionCode**(`fields`): `ActionCode`
|
16
16
|
|
17
|
-
Defined in: [actioncode.ts:33](https://github.com/otaprotocol/actioncodes/blob/
|
17
|
+
Defined in: [actioncode.ts:33](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L33)
|
18
18
|
|
19
19
|
#### Parameters
|
20
20
|
|
@@ -34,7 +34,7 @@ Defined in: [actioncode.ts:33](https://github.com/otaprotocol/actioncodes/blob/6
|
|
34
34
|
|
35
35
|
> **get** **chain**(): `string`
|
36
36
|
|
37
|
-
Defined in: [actioncode.ts:95](https://github.com/otaprotocol/actioncodes/blob/
|
37
|
+
Defined in: [actioncode.ts:95](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L95)
|
38
38
|
|
39
39
|
Get the target chain for this action code
|
40
40
|
|
@@ -52,7 +52,7 @@ Chain identifier (e.g., "solana", "evm")
|
|
52
52
|
|
53
53
|
> **get** **code**(): `string`
|
54
54
|
|
55
|
-
Defined in: [actioncode.ts:111](https://github.com/otaprotocol/actioncodes/blob/
|
55
|
+
Defined in: [actioncode.ts:111](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L111)
|
56
56
|
|
57
57
|
Get the action code string
|
58
58
|
|
@@ -70,7 +70,7 @@ The 8-character action code
|
|
70
70
|
|
71
71
|
> **get** **codeHash**(): `string`
|
72
72
|
|
73
|
-
Defined in: [actioncode.ts:217](https://github.com/otaprotocol/actioncodes/blob/
|
73
|
+
Defined in: [actioncode.ts:217](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L217)
|
74
74
|
|
75
75
|
Get the code hash for this action code
|
76
76
|
it is also used in the protocol meta as the code id
|
@@ -89,7 +89,7 @@ Code hash string
|
|
89
89
|
|
90
90
|
> **get** **description**(): `undefined` \| `string`
|
91
91
|
|
92
|
-
Defined in: [actioncode.ts:151](https://github.com/otaprotocol/actioncodes/blob/
|
92
|
+
Defined in: [actioncode.ts:151](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L151)
|
93
93
|
|
94
94
|
Get a human-readable description of the action
|
95
95
|
|
@@ -107,7 +107,7 @@ Description string or undefined
|
|
107
107
|
|
108
108
|
> **get** **displayString**(): `string`
|
109
109
|
|
110
|
-
Defined in: [actioncode.ts:183](https://github.com/otaprotocol/actioncodes/blob/
|
110
|
+
Defined in: [actioncode.ts:183](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L183)
|
111
111
|
|
112
112
|
Get a human-readable display string for the action code
|
113
113
|
|
@@ -125,7 +125,7 @@ Formatted display string
|
|
125
125
|
|
126
126
|
> **get** **encoded**(): `string`
|
127
127
|
|
128
|
-
Defined in: [actioncode.ts:47](https://github.com/otaprotocol/actioncodes/blob/
|
128
|
+
Defined in: [actioncode.ts:47](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L47)
|
129
129
|
|
130
130
|
##### Returns
|
131
131
|
|
@@ -139,7 +139,7 @@ Defined in: [actioncode.ts:47](https://github.com/otaprotocol/actioncodes/blob/6
|
|
139
139
|
|
140
140
|
> **get** **expired**(): `boolean`
|
141
141
|
|
142
|
-
Defined in: [actioncode.ts:87](https://github.com/otaprotocol/actioncodes/blob/
|
142
|
+
Defined in: [actioncode.ts:87](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L87)
|
143
143
|
|
144
144
|
Check if the action code has expired
|
145
145
|
|
@@ -157,7 +157,7 @@ True if the code has expired
|
|
157
157
|
|
158
158
|
> **get** **json**(): [`ActionCodeFields`](../interfaces/ActionCodeFields.md)
|
159
159
|
|
160
|
-
Defined in: [actioncode.ts:68](https://github.com/otaprotocol/actioncodes/blob/
|
160
|
+
Defined in: [actioncode.ts:68](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L68)
|
161
161
|
|
162
162
|
##### Returns
|
163
163
|
|
@@ -171,7 +171,7 @@ Defined in: [actioncode.ts:68](https://github.com/otaprotocol/actioncodes/blob/6
|
|
171
171
|
|
172
172
|
> **get** **metadata**(): `undefined` \| [`ActionCodeMetadata`](../interfaces/ActionCodeMetadata.md)
|
173
173
|
|
174
|
-
Defined in: [actioncode.ts:143](https://github.com/otaprotocol/actioncodes/blob/
|
174
|
+
Defined in: [actioncode.ts:143](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L143)
|
175
175
|
|
176
176
|
Get metadata associated with this action code
|
177
177
|
|
@@ -189,7 +189,7 @@ Metadata object or undefined
|
|
189
189
|
|
190
190
|
> **get** **params**(): `undefined` \| `Record`\<`string`, `any`\>
|
191
191
|
|
192
|
-
Defined in: [actioncode.ts:159](https://github.com/otaprotocol/actioncodes/blob/
|
192
|
+
Defined in: [actioncode.ts:159](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L159)
|
193
193
|
|
194
194
|
Get parameters associated with this action
|
195
195
|
|
@@ -207,7 +207,7 @@ Parameters object or undefined
|
|
207
207
|
|
208
208
|
> **get** **prefix**(): `string`
|
209
209
|
|
210
|
-
Defined in: [actioncode.ts:119](https://github.com/otaprotocol/actioncodes/blob/
|
210
|
+
Defined in: [actioncode.ts:119](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L119)
|
211
211
|
|
212
212
|
Get the prefix used for this action code
|
213
213
|
|
@@ -225,7 +225,7 @@ Normalized prefix
|
|
225
225
|
|
226
226
|
> **get** **pubkey**(): `string`
|
227
227
|
|
228
|
-
Defined in: [actioncode.ts:127](https://github.com/otaprotocol/actioncodes/blob/
|
228
|
+
Defined in: [actioncode.ts:127](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L127)
|
229
229
|
|
230
230
|
Get the user's public key
|
231
231
|
|
@@ -243,7 +243,7 @@ User's public key
|
|
243
243
|
|
244
244
|
> **get** **remainingTime**(): `number`
|
245
245
|
|
246
|
-
Defined in: [actioncode.ts:78](https://github.com/otaprotocol/actioncodes/blob/
|
246
|
+
Defined in: [actioncode.ts:78](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L78)
|
247
247
|
|
248
248
|
Get remaining time in milliseconds until expiration
|
249
249
|
|
@@ -261,7 +261,7 @@ Remaining time in milliseconds, or 0 if expired
|
|
261
261
|
|
262
262
|
> **get** **remainingTimeString**(): `string`
|
263
263
|
|
264
|
-
Defined in: [actioncode.ts:196](https://github.com/otaprotocol/actioncodes/blob/
|
264
|
+
Defined in: [actioncode.ts:196](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L196)
|
265
265
|
|
266
266
|
Get a formatted time string showing remaining time
|
267
267
|
|
@@ -279,7 +279,7 @@ Human-readable time string (e.g., "1m 30s remaining")
|
|
279
279
|
|
280
280
|
> **get** **signature**(): `string`
|
281
281
|
|
282
|
-
Defined in: [actioncode.ts:175](https://github.com/otaprotocol/actioncodes/blob/
|
282
|
+
Defined in: [actioncode.ts:175](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L175)
|
283
283
|
|
284
284
|
Get the user's signature
|
285
285
|
|
@@ -297,7 +297,7 @@ User's signature string
|
|
297
297
|
|
298
298
|
> **get** **status**(): [`ActionCodeStatus`](../type-aliases/ActionCodeStatus.md)
|
299
299
|
|
300
|
-
Defined in: [actioncode.ts:103](https://github.com/otaprotocol/actioncodes/blob/
|
300
|
+
Defined in: [actioncode.ts:103](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L103)
|
301
301
|
|
302
302
|
Get the current status of the action code
|
303
303
|
|
@@ -315,7 +315,7 @@ Current status
|
|
315
315
|
|
316
316
|
> **get** **timestamp**(): `number`
|
317
317
|
|
318
|
-
Defined in: [actioncode.ts:167](https://github.com/otaprotocol/actioncodes/blob/
|
318
|
+
Defined in: [actioncode.ts:167](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L167)
|
319
319
|
|
320
320
|
Get the timestamp when the code was generated
|
321
321
|
|
@@ -333,7 +333,7 @@ Timestamp in milliseconds
|
|
333
333
|
|
334
334
|
> **get** **transaction**(): `undefined` \| [`ActionCodeTransaction`](../interfaces/ActionCodeTransaction.md)
|
335
335
|
|
336
|
-
Defined in: [actioncode.ts:135](https://github.com/otaprotocol/actioncodes/blob/
|
336
|
+
Defined in: [actioncode.ts:135](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L135)
|
337
337
|
|
338
338
|
Get the transaction data (chain-specific)
|
339
339
|
|
@@ -349,7 +349,7 @@ Transaction data or undefined
|
|
349
349
|
|
350
350
|
> **isValid**(`protocol`): `boolean`
|
351
351
|
|
352
|
-
Defined in: [actioncode.ts:51](https://github.com/otaprotocol/actioncodes/blob/
|
352
|
+
Defined in: [actioncode.ts:51](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L51)
|
353
353
|
|
354
354
|
#### Parameters
|
355
355
|
|
@@ -367,7 +367,7 @@ Defined in: [actioncode.ts:51](https://github.com/otaprotocol/actioncodes/blob/6
|
|
367
367
|
|
368
368
|
> `static` **fromEncoded**(`encoded`): `ActionCode`
|
369
369
|
|
370
|
-
Defined in: [actioncode.ts:42](https://github.com/otaprotocol/actioncodes/blob/
|
370
|
+
Defined in: [actioncode.ts:42](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L42)
|
371
371
|
|
372
372
|
#### Parameters
|
373
373
|
|
@@ -385,7 +385,7 @@ Defined in: [actioncode.ts:42](https://github.com/otaprotocol/actioncodes/blob/6
|
|
385
385
|
|
386
386
|
> `static` **fromPayload**(`input`): `ActionCode`
|
387
387
|
|
388
|
-
Defined in: [actioncode.ts:35](https://github.com/otaprotocol/actioncodes/blob/
|
388
|
+
Defined in: [actioncode.ts:35](https://github.com/otaprotocol/actioncodes/blob/c724b443a380f5f43ae1dd1ddefb6b90efaa0aa5/src/actioncode.ts#L35)
|
389
389
|
|
390
390
|
#### Parameters
|
391
391
|
|