|
| 1 | +import { genericHash } from '../nacl/naclWrappers'; |
| 2 | +import { TransactionType } from '../types/transactions/base'; |
| 3 | +import { ABIType, ABITupleType } from './abi_type'; |
| 4 | + |
| 5 | +export function abiTypeIsTransaction(type: string): type is TransactionType { |
| 6 | + return type === 'txn' || Object.keys(TransactionType).includes(type); |
| 7 | +} |
| 8 | + |
| 9 | +function parseMethodSignature( |
| 10 | + signature: string |
| 11 | +): { name: string; args: string[]; returns: string } { |
| 12 | + const argsStart = signature.indexOf('('); |
| 13 | + if (argsStart === -1) { |
| 14 | + throw new Error(`Invalid method signature: ${signature}`); |
| 15 | + } |
| 16 | + |
| 17 | + let argsEnd = -1; |
| 18 | + let depth = 0; |
| 19 | + for (let i = argsStart; i < signature.length; i++) { |
| 20 | + const char = signature[i]; |
| 21 | + |
| 22 | + if (char === '(') { |
| 23 | + depth += 1; |
| 24 | + } else if (char === ')') { |
| 25 | + if (depth === 0) { |
| 26 | + // unpaired parenthesis |
| 27 | + break; |
| 28 | + } |
| 29 | + |
| 30 | + depth -= 1; |
| 31 | + if (depth === 0) { |
| 32 | + argsEnd = i; |
| 33 | + break; |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + if (argsEnd === -1) { |
| 39 | + throw new Error(`Invalid method signature: ${signature}`); |
| 40 | + } |
| 41 | + |
| 42 | + return { |
| 43 | + name: signature.slice(0, argsStart), |
| 44 | + args: ABITupleType.parseTupleContent( |
| 45 | + signature.slice(argsStart + 1, argsEnd) |
| 46 | + ), |
| 47 | + returns: signature.slice(argsEnd + 1), |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +export interface ABIMethodParams { |
| 52 | + name: string; |
| 53 | + desc?: string; |
| 54 | + args: Array<{ type: string; name?: string; desc?: string }>; |
| 55 | + returns: { type: string; desc?: string }; |
| 56 | +} |
| 57 | + |
| 58 | +export type ABIArgumentType = ABIType | TransactionType; |
| 59 | + |
| 60 | +export type ABIReturnType = ABIType | 'void'; |
| 61 | + |
| 62 | +export class ABIMethod { |
| 63 | + public readonly name: string; |
| 64 | + public readonly description?: string; |
| 65 | + public readonly args: Array<{ |
| 66 | + type: ABIArgumentType; |
| 67 | + name?: string; |
| 68 | + description?: string; |
| 69 | + }>; |
| 70 | + |
| 71 | + public readonly returns: { type: ABIReturnType; description?: string }; |
| 72 | + |
| 73 | + constructor(params: ABIMethodParams) { |
| 74 | + if ( |
| 75 | + typeof params.name !== 'string' || |
| 76 | + typeof params.returns !== 'object' || |
| 77 | + !Array.isArray(params.args) |
| 78 | + ) { |
| 79 | + throw new Error('Invalid ABIMethod parameters'); |
| 80 | + } |
| 81 | + |
| 82 | + this.name = params.name; |
| 83 | + this.description = params.desc; |
| 84 | + this.args = params.args.map(({ type, name, desc }) => { |
| 85 | + if (abiTypeIsTransaction(type)) { |
| 86 | + return { |
| 87 | + type, |
| 88 | + name, |
| 89 | + description: desc, |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + return { |
| 94 | + type: ABIType.from(type), |
| 95 | + name, |
| 96 | + description: desc, |
| 97 | + }; |
| 98 | + }); |
| 99 | + this.returns = { |
| 100 | + type: |
| 101 | + params.returns.type === 'void' |
| 102 | + ? params.returns.type |
| 103 | + : ABIType.from(params.returns.type), |
| 104 | + description: params.returns.desc, |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + getSignature(): string { |
| 109 | + const args = this.args.map((arg) => arg.type.toString()).join(','); |
| 110 | + const returns = this.returns.type.toString(); |
| 111 | + return `${this.name}(${args})${returns}`; |
| 112 | + } |
| 113 | + |
| 114 | + getSelector(): Uint8Array { |
| 115 | + const hash = genericHash(this.getSignature()); |
| 116 | + return new Uint8Array(hash.slice(0, 4)); |
| 117 | + } |
| 118 | + |
| 119 | + txnCount(): number { |
| 120 | + let count = 1; |
| 121 | + for (const arg of this.args) { |
| 122 | + if (typeof arg.type === 'string' && abiTypeIsTransaction(arg.type)) { |
| 123 | + count += 1; |
| 124 | + } |
| 125 | + } |
| 126 | + return count; |
| 127 | + } |
| 128 | + |
| 129 | + toJSON(): ABIMethodParams { |
| 130 | + return { |
| 131 | + name: this.name, |
| 132 | + desc: this.description, |
| 133 | + args: this.args.map(({ type, name, description }) => ({ |
| 134 | + type: type.toString(), |
| 135 | + name, |
| 136 | + desc: description, |
| 137 | + })), |
| 138 | + returns: { |
| 139 | + type: this.returns.type.toString(), |
| 140 | + desc: this.returns.description, |
| 141 | + }, |
| 142 | + }; |
| 143 | + } |
| 144 | + |
| 145 | + static fromSignature(signature: string): ABIMethod { |
| 146 | + const { name, args, returns } = parseMethodSignature(signature); |
| 147 | + |
| 148 | + return new ABIMethod({ |
| 149 | + name, |
| 150 | + args: args.map((arg) => ({ type: arg })), |
| 151 | + returns: { type: returns }, |
| 152 | + }); |
| 153 | + } |
| 154 | +} |
0 commit comments