Skip to content

Commit

Permalink
fix(core): anchor proofs use txType instead of version - CDB-2074 (#2565
Browse files Browse the repository at this point in the history
)

* fix(core): anchor proofs use txType instead of version

* fix: typo

* fix: refert changes to v1 CID parsing

Co-authored-by: Joel Thorstensson <[email protected]>
  • Loading branch information
oed and oed authored Nov 25, 2022
1 parent 79825cd commit bed5161
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 128 deletions.
2 changes: 1 addition & 1 deletion packages/common/src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export interface AnchorProof {
blockTimestamp: number
txHash: CID
root: CID
version?: number
txType?: string
}

export interface AnchorCommit {
Expand Down
234 changes: 117 additions & 117 deletions packages/core/src/__tests__/__snapshots__/caip10-link.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,38 @@ Object {
"hash": Uint8Array [
18,
32,
9,
14,
51,
24,
226,
153,
52,
12,
23,
139,
247,
206,
244,
238,
83,
26,
212,
233,
32,
85,
142,
143,
140,
100,
104,
77,
49,
96,
92,
146,
227,
208,
250,
172,
73,
251,
85,
14,
39,
136,
11,
148,
118,
147,
234,
220,
207,
105,
217,
12,
73,
117,
205,
151,
139,
156,
152,
75,
243,
16,
138,
],
"version": 1,
},
Expand Down Expand Up @@ -86,7 +86,7 @@ Object {
],
"version": 1,
},
"version": 1,
"txType": "f(bytes32)",
},
"anchorStatus": 3,
"content": "did:3:bafysdfwefwe",
Expand Down Expand Up @@ -141,38 +141,38 @@ Object {
"hash": Uint8Array [
18,
32,
137,
108,
175,
129,
245,
222,
196,
76,
202,
40,
189,
177,
252,
146,
146,
203,
52,
217,
64,
3,
236,
86,
1,
78,
211,
167,
93,
117,
43,
200,
130,
195,
153,
61,
145,
141,
127,
22,
19,
184,
210,
163,
129,
49,
64,
220,
161,
40,
251,
117,
140,
143,
213,
37,
193,
204,
17,
190,
54,
211,
],
"version": 1,
},
Expand All @@ -185,38 +185,38 @@ Object {
"hash": Uint8Array [
18,
32,
9,
14,
51,
24,
226,
153,
52,
12,
23,
139,
247,
206,
244,
238,
83,
26,
212,
233,
32,
85,
142,
143,
140,
100,
104,
77,
49,
96,
92,
146,
227,
208,
250,
172,
73,
251,
85,
14,
39,
136,
11,
148,
118,
147,
234,
220,
207,
105,
217,
12,
73,
117,
205,
151,
139,
156,
152,
75,
243,
16,
138,
],
"version": 1,
},
Expand All @@ -229,38 +229,38 @@ Object {
"hash": Uint8Array [
18,
32,
66,
255,
10,
137,
219,
36,
117,
166,
34,
187,
50,
186,
184,
17,
60,
210,
23,
199,
127,
126,
40,
54,
123,
237,
242,
136,
58,
66,
224,
109,
24,
123,
133,
235,
139,
46,
89,
71,
21,
105,
115,
59,
93,
95,
182,
94,
3,
49,
116,
106,
19,
26,
200,
235,
65,
11,
249,
173,
11,
24,
176,
54,
],
"version": 1,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { decode } from 'multiformats/hashes/digest'
import * as uint8arrays from 'uint8arrays'

const TEST_CHAIN_ID = '1337'
const V1_PROOF_TYPE = 'f(bytes32)'
const ANCHOR_PROOF: AnchorProof = {
chainId: `eip155:${TEST_CHAIN_ID}`,
blockNumber: 3,
blockTimestamp: 1586784008,
txHash: CID.parse('bagjqcgza7mvdlzewbfbq35peso2atjydg3ekalew5vmze7w2a5cbhmav4rmq'),
root: CID.parse('bafyreic5p7grucmzx363ayxgoywb6d4qf5zjxgbqjixpkokbf5jtmdj5ni'),
version: 1,
txType: V1_PROOF_TYPE,
}
const TX_HASH =
'0x' + uint8arrays.toString(decode(ANCHOR_PROOF.txHash.multihash.bytes).digest, 'base16')
Expand Down
19 changes: 11 additions & 8 deletions packages/core/src/anchor/ethereum/ethereum-anchor-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ const BASE_CHAIN_ID = 'eip155'
const MAX_PROVIDERS_COUNT = 100
const TRANSACTION_CACHE_SIZE = 50
const BLOCK_CACHE_SIZE = 50
const V0_PROOF_TYPE = 'raw'
const V1_PROOF_TYPE = 'f(bytes32)'

const ABI = ['function anchorDagCbor(bytes32)']

const iface = new Interface(ABI)


//TODO (NET-1659): Finalize block numbers and smart contract addresses once CAS is creating smart contract anchors
const BLOCK_THRESHHOLDS = {
'eip155:1': 1000000000, //mainnet
Expand Down Expand Up @@ -80,12 +83,12 @@ const getCidFromV1Transaction = (txResponse: TransactionResponse): CID => {

/**
* Parses the transaction data to recover the CID.
* @param version version of the anchor proof. Version 1 anchor proofs are created using the official anchoring smart contract and must be parsed accordingly
* @param txType transaction type of the anchor proof. Currently support `raw` and `f(bytes32)`
* @param txResponse the retrieved transaction from the ethereum blockchain
* @returns
*/
const getCidFromTransaction = (version: number, txResponse: TransactionResponse): CID => {
if (version === 1) {
const getCidFromTransaction = (txType: string, txResponse: TransactionResponse): CID => {
if (txType === V1_PROOF_TYPE) {
return getCidFromV1Transaction(txResponse)
} else {
return getCidFromV0Transaction(txResponse)
Expand Down Expand Up @@ -210,7 +213,7 @@ export class EthereumAnchorValidator implements AnchorValidator {
const decoded = decode(anchorProof.txHash.multihash.bytes)
const txHash = '0x' + uint8arrays.toString(decoded.digest, 'base16')
const [txResponse, block] = await this._getTransactionAndBlockInfo(anchorProof.chainId, txHash)
const txCid = getCidFromTransaction(anchorProof.version, txResponse)
const txCid = getCidFromTransaction(anchorProof.txType, txResponse)

if (!txCid.equals(anchorProof.root)) {
throw new Error(`The root CID ${anchorProof.root.toString()} is not in the transaction`)
Expand All @@ -228,19 +231,19 @@ export class EthereumAnchorValidator implements AnchorValidator {
)
}

// if the block number is greater than the threshold and the version is 0 or non existent
// if the block number is greater than the threshold and the txType is `raw` or non existent
if (
txResponse.blockNumber > BLOCK_THRESHHOLDS[this._chainId] &&
(anchorProof.version === 0 || !anchorProof.version)
(anchorProof.txType === V0_PROOF_TYPE || !anchorProof.txType)
) {
throw new Error(
`Any anchor proofs created after block ${
BLOCK_THRESHHOLDS[this._chainId]
} must include the version field. AnchorProof blockNumber: ${anchorProof.blockNumber}`
} must include the txType field. AnchorProof blockNumber: ${anchorProof.blockNumber}`
)
}

if (anchorProof.version === 1 && txResponse.to !== ANCHOR_CONTRACT_ADDRESSES[this._chainId]) {
if (anchorProof.txType === V1_PROOF_TYPE && txResponse.to !== ANCHOR_CONTRACT_ADDRESSES[this._chainId]) {
throw new Error(
`Anchor was created using address ${
txResponse.to
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/anchor/memory/in-memory-anchor-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { sha256 as hasher } from 'multiformats/hashes/sha2'
const DID_MATCHER =
'^(did:([a-zA-Z0-9_]+):([a-zA-Z0-9_.-]+(:[a-zA-Z0-9_.-]+)*)((;[a-zA-Z0-9_.:%-]+=[a-zA-Z0-9_.:%-]*)*)(/[^#?]*)?)([?][^#]*)?(#.*)?'
const CHAIN_ID = 'inmemory:12345'
const V1_PROOF_TYPE = 'f(bytes32)'

class Candidate {
constructor(readonly cid: CID, readonly streamId?: StreamID, readonly log?: CID[]) {}
Expand Down Expand Up @@ -323,7 +324,7 @@ export class InMemoryAnchorService implements AnchorService, AnchorValidator {
txHash: CID.parse(SAMPLE_ETH_TX_HASH),
root: leaf.cid,
//TODO (NET-1657): Update the InMemoryAnchorService to mirror the behavior of the contract-based anchoring system
version: 1,
txType: V1_PROOF_TYPE,
}
const proof = await this.#dispatcher.storeCommit(proofData)
const commit = { proof, path: '', prev: leaf.cid, id: leaf.streamId.cid }
Expand Down

0 comments on commit bed5161

Please sign in to comment.