1- import { Address , concatArrays , decodeMsgpack , hash , isValidAddress } from '@algorandfoundation/algokit-common'
1+ import { Address , concatArrays , decodeMsgpack , hash } from '@algorandfoundation/algokit-common'
22import { MultisigAccount } from './multisig'
33import { TransactionSigner } from './signer'
44import { LogicSignature , MultisigSignature , SignedTransaction , encodeSignedTransaction } from './transactions/signed-transaction'
5- import { logicSignatureCodec } from './transactions/signed-transaction-meta'
65import { Transaction } from './transactions/transaction'
7-
8- // base64regex is the regex to test for base64 strings
9- const base64regex = / ^ ( [ 0 - 9 a - z A - Z + / ] { 4 } ) * ( ( [ 0 - 9 a - z A - Z + / ] { 2 } = = ) | ( [ 0 - 9 a - z A - Z + / ] { 3 } = ) ) ? $ /
10-
11- /** sanityCheckProgram performs heuristic program validation:
12- * check if passed in bytes are Algorand address or is B64 encoded, rather than Teal bytes
13- *
14- * @param program - Program bytes to check
15- */
16- export function sanityCheckProgram ( program : Uint8Array ) {
17- if ( ! program || program . length === 0 ) throw new Error ( 'empty program' )
18-
19- const lineBreakOrd = '\n' . charCodeAt ( 0 )
20- const blankSpaceOrd = ' ' . charCodeAt ( 0 )
21- const tildeOrd = '~' . charCodeAt ( 0 )
22-
23- const isPrintable = ( x : number ) => blankSpaceOrd <= x && x <= tildeOrd
24- const isAsciiPrintable = program . every ( ( x : number ) => x === lineBreakOrd || isPrintable ( x ) )
25-
26- if ( isAsciiPrintable ) {
27- const programStr = new TextDecoder ( ) . decode ( program )
28-
29- if ( isValidAddress ( programStr ) ) throw new Error ( 'requesting program bytes, get Algorand address' )
30-
31- if ( base64regex . test ( programStr ) ) throw new Error ( 'program should not be b64 encoded' )
32-
33- throw new Error ( 'program bytes are all ASCII printable characters, not looking like Teal byte code' )
34- }
35- }
6+ import { logicSignatureCodec } from './transactions/signed-transaction-meta'
367
378const PROGRAM_TAG = new TextEncoder ( ) . encode ( 'Program' )
389const MSIG_PROGRAM_TAG = new TextEncoder ( ) . encode ( 'MsigProgram' )
@@ -51,6 +22,20 @@ export class LogicSigAccount {
5122 msig ?: MultisigSignature
5223 lmsig ?: MultisigSignature
5324
25+ static fromSignature ( signature : LogicSignature ) : LogicSigAccount {
26+ const lsigAccount = new LogicSigAccount ( signature . logic , signature . args || [ ] )
27+ lsigAccount . sig = signature . sig
28+ lsigAccount . msig = signature . msig
29+ lsigAccount . lmsig = signature . lmsig
30+ return lsigAccount
31+ }
32+
33+ static fromBytes ( encodedLsig : Uint8Array ) : LogicSigAccount {
34+ const decoded = decodeMsgpack ( encodedLsig )
35+ const lsigSignature = logicSignatureCodec . decode ( decoded , 'msgpack' )
36+ return LogicSigAccount . fromSignature ( lsigSignature )
37+ }
38+
5439 constructor ( program : Uint8Array , programArgs ?: Array < Uint8Array > | null ) {
5540 if ( programArgs && ( ! Array . isArray ( programArgs ) || ! programArgs . every ( ( arg ) => arg . constructor === Uint8Array ) ) ) {
5641 throw new TypeError ( 'Invalid arguments' )
@@ -59,8 +44,6 @@ export class LogicSigAccount {
5944 let args : Uint8Array [ ] = [ ]
6045 if ( programArgs != null ) args = programArgs . map ( ( arg ) => new Uint8Array ( arg ) )
6146
62- sanityCheckProgram ( program )
63-
6447 this . logic = program
6548 this . args = args
6649 }
@@ -133,14 +116,3 @@ export class LogicSigAccount {
133116 return concatArrays ( SIGN_PROGRAM_DATA_PREFIX , this . address ( ) . publicKey , data )
134117 }
135118}
136-
137- /**
138- * Decodes MsgPack bytes into a logic signature.
139- *
140- * @param encodedLogicSignature - The MsgPack encoded logic signature
141- * @returns The decoded LogicSignature or an error if decoding fails.
142- */
143- export function decodeLogicSignature ( encodedLogicSignature : Uint8Array ) : LogicSignature {
144- const decodedData = decodeMsgpack ( encodedLogicSignature )
145- return logicSignatureCodec . decode ( decodedData , 'msgpack' )
146- }
0 commit comments